public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* floating point warnings, and gcc 4 warnings in general
@ 2006-07-12 14:45 Scott Lipcon
  2006-07-13  7:53 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Scott Lipcon @ 2006-07-12 14:45 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2315 bytes --]

Hi all,

 I'm trying to figure out how to get gcc to warn on a specific problem
that we found in our source code.   I've attached a simple test
program that has a few places where floating point precision can
potentially be lost or cause problems.   Specifically:
    1)   Line 12,  returns a double as a float without an explicit
cast - loss of precision.  Would like a warning here.
    2)  Line 19,  same problem, but with fixed point - returns an int
as a short, without a cast - loss of precision, would like a warning
    3)  Line 31,  subtracts a float from a double (the constant is
treated as a double, the variable is explicitly cast as a float.   In
this particular case, as can be seen in the printf on line 34,  pi/2 -
pi/2 does not equal zero.   This seems like it might be harder to
check for, but I'd love to see a warning here as well.

 Note that the method on line 15, where I return a double as an int
does have a warning, even without any -W... options:

 tryFloat.C: In member function `int Test::getDI() const':
 tryFloat.C:15: warning: return to `int' from `const double'
 tryFloat.C:15: warning: argument to `int' from `const double'

 The man page on gcc 3.4 and above seem to imply that -Wextra should
have checks for these floating point precision problems - however I've
tried gcc 3.3.2, 3.4.x, 4.0.0, and 4.1.1, and no matter how many -W
options I add, -pedantic, -ansi, etc, to make the compile as strict as
possible, I can't get warnings for these floating point problems.
Can anyone point out how to get warnings, or explain why these aren't
candidates for a warning?

 Finally, and probably unrelated,  when I used gcc 4.0.0 and 4.1.1, on
RedHat Enterprise 4 (ES), the warnings I do get are formatted badly -
ie, the warning above (from 3.3.2) shows up as:

 tryFloat.C: In member function â:
 tryFloat.C:15: warning: converting to â from â

 I don't know what the â character is, but thats clearly not very
useful.   The gcc 4.1.1 is a freshly compiled install - I compiled it
with RedHat's gcc 3.4.5 using:

 Configured with: /usr/src/gcc-4.1.1/configure
--prefix=/usr/local/gcc-4.1.1 --disable-shared --enable-threads=posix
--enable-lang=c,c++

 and make bootstrap.   Any ideas on this one?


 Thanks very much,

Scott Lipcon

[-- Attachment #2: tryFloat.C --]
[-- Type: application/octet-stream, Size: 960 bytes --]

#include <stdio.h>

#define M_PI 3.14159265358979323846

class Test
{
  public:
    Test() : _d(0.0), _i(0) {}

    void setD(const double &d) { _d = d; }
    double getD() const { return _d; }
    float getDF() const { return _d; }

    // warning: return to `int' from `const double'
    int getDI() const { return _d; }

    void setI(const int &i) { _i = i; }
    int getI() const { return _i; }
    short getIS() const { return _i; }

  private:
    double _d;
    int _i;
};

int main(int /*argc*/, char** /*argv*/)
{
  double d = (M_PI / 2.0) - 0.0;

  double doubleRes = (M_PI / 2.0) - d;
  float floatRes = (M_PI / 2.0) - (float)d;

  printf("double: %.45f\n", doubleRes);
  printf("float: %.45f\n", floatRes);

  Test t;
  t.setD(M_PI);
  printf("double: %.45f\n", t.getD());
  printf("float: %.45f\n", t.getDF());
  printf("int: %d\n", t.getDI());

  t.setI(66000);
  printf("int: %d\n", t.getI());
  printf("short: %d\n", t.getIS());
  return 0;
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: floating point warnings, and gcc 4 warnings in general
  2006-07-12 14:45 floating point warnings, and gcc 4 warnings in general Scott Lipcon
@ 2006-07-13  7:53 ` Ian Lance Taylor
  2006-07-31 17:35   ` Manuel López-Ibáñez
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Lance Taylor @ 2006-07-13  7:53 UTC (permalink / raw)
  To: Scott Lipcon; +Cc: lopezibanez, gcc-help

"Scott Lipcon" <slipcon@gmail.com> writes:

>  I'm trying to figure out how to get gcc to warn on a specific problem
> that we found in our source code.   I've attached a simple test
> program that has a few places where floating point precision can
> potentially be lost or cause problems.   Specifically:
>     1)   Line 12,  returns a double as a float without an explicit
> cast - loss of precision.  Would like a warning here.
>     2)  Line 19,  same problem, but with fixed point - returns an int
> as a short, without a cast - loss of precision, would like a warning
>     3)  Line 31,  subtracts a float from a double (the constant is
> treated as a double, the variable is explicitly cast as a float.   In
> this particular case, as can be seen in the printf on line 34,  pi/2 -
> pi/2 does not equal zero.   This seems like it might be harder to
> check for, but I'd love to see a warning here as well.

Manuel López-Ibáñez is working on warnings along these lines as part
of a Google Summer of Code project.  See
    http://gcc.gnu.org/wiki/Wcoercion

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: floating point warnings, and gcc 4 warnings in general
  2006-07-13  7:53 ` Ian Lance Taylor
@ 2006-07-31 17:35   ` Manuel López-Ibáñez
  0 siblings, 0 replies; 3+ messages in thread
From: Manuel López-Ibáñez @ 2006-07-31 17:35 UTC (permalink / raw)
  To: Scott Lipcon; +Cc: Ian Lance Taylor, gcc-help

Hi Scott, sorry for the late reply.

My current code gives the following warnings for your testcase (all of
them plus one bonus extra warning):

tryFloat.C:12: warning: coercion to 'float' from 'const double' may
alter its value
  int Test::getDI() const
tryFloat.C:15: warning: coercion to 'int' from 'const double' may
alter its value
  void Test::setI(const int&) int Test::getI() const short int
Test::getIS() const
tryFloat.C:19: warning: coercion to 'short int' from 'const int' may
alter its value
 int main(int, char**)
tryFloat.C:31: warning: coercion to 'float' from 'double' may alter its value

You can get the patches from http://gcc.gnu.org/wiki/Wcoercion#Download

It would be great if you can test the patches or provide more
testcases or add something to the documentation (
http://gcc.gnu.org/wiki/Wcoercion ). Any feedback is welcome.

Happy coding!

Manuel.


On 13 Jul 2006 00:53:31 -0700, Ian Lance Taylor <iant@google.com> wrote:
> "Scott Lipcon" <slipcon@gmail.com> writes:
>
> >  I'm trying to figure out how to get gcc to warn on a specific problem
> > that we found in our source code.   I've attached a simple test
> > program that has a few places where floating point precision can
> > potentially be lost or cause problems.   Specifically:
> >     1)   Line 12,  returns a double as a float without an explicit
> > cast - loss of precision.  Would like a warning here.
> >     2)  Line 19,  same problem, but with fixed point - returns an int
> > as a short, without a cast - loss of precision, would like a warning
> >     3)  Line 31,  subtracts a float from a double (the constant is
> > treated as a double, the variable is explicitly cast as a float.   In
> > this particular case, as can be seen in the printf on line 34,  pi/2 -
> > pi/2 does not equal zero.   This seems like it might be harder to
> > check for, but I'd love to see a warning here as well.
>
> Manuel López-Ibáñez is working on warnings along these lines as part
> of a Google Summer of Code project.  See
>     http://gcc.gnu.org/wiki/Wcoercion
>
> Ian
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-07-31 17:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-12 14:45 floating point warnings, and gcc 4 warnings in general Scott Lipcon
2006-07-13  7:53 ` Ian Lance Taylor
2006-07-31 17:35   ` Manuel López-Ibáñez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).