[Bug c/53603] New: abs() doesn't warn if argument is double, not even with -Wall -Wextra

gcc at richardneill dot org gcc-bugzilla@gcc.gnu.org
Thu Jun 7 13:13:00 GMT 2012


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 )



More information about the Gcc-bugs mailing list