public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53603] New: abs() doesn't warn if argument is double, not even with -Wall -Wextra
@ 2012-06-07 13:13 gcc at richardneill dot org
  2012-06-07 13:55 ` [Bug c/53603] " dominiq at lps dot ens.fr
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gcc at richardneill dot org @ 2012-06-07 13:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53603

             Bug #: 53603
           Summary: abs() doesn't warn if argument is double, not even
                    with -Wall -Wextra
    Classification: Unclassified
           Product: gcc
           Version: 4.6.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: gcc@richardneill.org


I think that using abs() with an argument which isn't an int, should to trigger
a warning... but it doesn't. Not even with -Wextra.

//Compile with Wall -Wextra
//I think that gcc could spot that abs() casts x to int, 
//and it could warn me that I did something stupid. 

#include <stdio.h>
#include <stdlib.h>
int main(){
        double x=44.33;
        double y;
        y = abs (x);
        printf ("y is %f\n", y);
        //get 44.000000 , expect 44.330000
        return (0);
}


Please do consider trapping this: I spent 3 hours finding a bug caused by this
one: yes, it was my fault for writing sqrt(abs(a_double)) rather than
sqrt(fabs(a_double)), but the compiler could have warned me. This type of error
is quite insidious, because it can cause subtle errors in mathematical programs
that only occur sometimes!

Imho, gcc should warn, unless I'd written:
     y = abs( (int)x )


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

* [Bug c/53603] abs() doesn't warn if argument is double, not even with -Wall -Wextra
  2012-06-07 13:13 [Bug c/53603] New: abs() doesn't warn if argument is double, not even with -Wall -Wextra gcc at richardneill dot org
@ 2012-06-07 13:55 ` dominiq at lps dot ens.fr
  2012-06-07 14:03 ` manu at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: dominiq at lps dot ens.fr @ 2012-06-07 13:55 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53603

--- Comment #1 from Dominique d'Humieres <dominiq at lps dot ens.fr> 2012-06-07 13:55:05 UTC ---
Use -Wconversion:

[macbook] f90/bug% gcc48 -Wconversion pr53603.c
pr53603.c: In function 'main':
pr53603.c:6:9: warning: conversion to 'int' from 'double' may alter its value
[-Wconversion]
         y = abs (x);
         ^

although I'ld expect the caret under x. For 4.4 to 4.7 one gets

pr53603.c: In function 'main':
pr53603.c:6: warning: conversion to 'int' from 'double' may alter its value


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

* [Bug c/53603] abs() doesn't warn if argument is double, not even with -Wall -Wextra
  2012-06-07 13:13 [Bug c/53603] New: abs() doesn't warn if argument is double, not even with -Wall -Wextra gcc at richardneill dot org
  2012-06-07 13:55 ` [Bug c/53603] " dominiq at lps dot ens.fr
@ 2012-06-07 14:03 ` manu at gcc dot gnu.org
  2012-06-07 14:41 ` gcc at richardneill dot org
  2012-06-07 14:56 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: manu at gcc dot gnu.org @ 2012-06-07 14:03 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53603

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |manu at gcc dot gnu.org
         Resolution|                            |DUPLICATE

--- Comment #2 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-06-07 14:03:45 UTC ---
It warns with -Wconversion, but that brings other warnings. See also:

http://gcc.gnu.org/wiki/NewWconversion#Frequently_Asked_Questions

We could have finer grained options such as Wconversion-float-to-int
Wconversion-precission-loss Wconversion-64-to-32, but it isn't clear how far we
should go: There will be always some people that want a warning in a specific
case and not in another very specific case.

Also, Wconversion currently warns in too many common cases to be enabled by
-Wextra: See PR38522, PR39170, PR40752, PR53277, etc.

*** This bug has been marked as a duplicate of bug 53001 ***


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

* [Bug c/53603] abs() doesn't warn if argument is double, not even with -Wall -Wextra
  2012-06-07 13:13 [Bug c/53603] New: abs() doesn't warn if argument is double, not even with -Wall -Wextra gcc at richardneill dot org
  2012-06-07 13:55 ` [Bug c/53603] " dominiq at lps dot ens.fr
  2012-06-07 14:03 ` manu at gcc dot gnu.org
@ 2012-06-07 14:41 ` gcc at richardneill dot org
  2012-06-07 14:56 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: gcc at richardneill dot org @ 2012-06-07 14:41 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53603

--- Comment #3 from Richard Neill <gcc at richardneill dot org> 2012-06-07 14:41:10 UTC ---
Thanks for your explanation. I didn't know about -Wconversion existing
independently of -Wextra. 

Might I suggest a couple of things that arise:

1. It seems a bit strange to me that there are warnings that are not enabled by 
-Wall -Wextra. The man page lists so many different types of warning: perhaps
it would be useful to mention (under -Wextra) which ones it does *not* enable?
Or maybe it would be worthwhile adding something like "-Wkitchensink" to mean:
yes, really do turn on all the warnings (except maybe for pedantic). 

2. Even -Wconversion misses the following:

  int a=7; int b=4;
  double y;  
  y = a/b;

I know why: a/b is a perfectly sensible integer division, and then y is being
assigned the integer value of 1, which is also a loss-freel conversion.
But, what I mean by "y=a/b" is "y is the floating point number obtained by
dividing a and b", not "do integer division of a/b and then promote to a
double"

3. As you say, -Wconversion is too noisy to include by default. But perhaps
there could be a distinction between straightforward assignment: 

   int a; 
   unsigned int b;
   a=b; 

and function calls with mismatched parameters:

   int a; 
   double d;
   int myfunction(int x){...}  ; 
   a = myfunction(d);

I'd suggest that when the latter happens, it's most probably a bug, whereas the
former is (often) deliberate. This might allow for some subsets of -Wconversion
to be included within -Wextra ? 

Thanks for your time.


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

* [Bug c/53603] abs() doesn't warn if argument is double, not even with -Wall -Wextra
  2012-06-07 13:13 [Bug c/53603] New: abs() doesn't warn if argument is double, not even with -Wall -Wextra gcc at richardneill dot org
                   ` (2 preceding siblings ...)
  2012-06-07 14:41 ` gcc at richardneill dot org
@ 2012-06-07 14:56 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-06-07 14:56 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53603

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-06-07 14:56:25 UTC ---
(In reply to comment #3)
> 1. It seems a bit strange to me that there are warnings that are not enabled by 
> -Wall -Wextra. The man page lists so many different types of warning: perhaps
> it would be useful to mention (under -Wextra) which ones it does *not* enable?

Such a list would almost certainly get out of date when warning options are
added (there are already several places that need to be updated for a new
warning) and if it's not complete it's not very useful.  However, if you want
to compile the list and propose a doc patch please send it to
gcc-patches@gcc.gnu.org

> Or maybe it would be worthwhile adding something like "-Wkitchensink" to mean:
> yes, really do turn on all the warnings (except maybe for pedantic). 

See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31573


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

end of thread, other threads:[~2012-06-07 14:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-07 13:13 [Bug c/53603] New: abs() doesn't warn if argument is double, not even with -Wall -Wextra gcc at richardneill dot org
2012-06-07 13:55 ` [Bug c/53603] " dominiq at lps dot ens.fr
2012-06-07 14:03 ` manu at gcc dot gnu.org
2012-06-07 14:41 ` gcc at richardneill dot org
2012-06-07 14:56 ` redi at gcc dot gnu.org

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).