------- Comment #19 from rob1weld at aol dot com  2007-06-24 05:01 -------
So here we have it:

#include <stdio.h>
int abs(int);
double fabs(double);
int main()
{
  printf("%f  %f  %f  %f\n", abs(1234.5678), fabs(1234.5678),
         abs((int)1234.5678), abs((int)(1234.5678)));
  printf("%f  %f  %f  %f\n", (float)abs(1234.5678), (float)fabs(1234.5678),
         (float)abs((int)1234.5678), (float)abs((int)(1234.5678)));
  return 0;
}


# /opt/gcc-4_3-build-2/gcc/xgcc -Wall -B/opt/gcc-4_3-build-2/gcc/ -o
math_test_11 math_test_11.c
math_test_11.c: In function 'main':
math_test_11.c:9: warning: format '%f' expects type 'double', but argument 2
has type 'int'
math_test_11.c:9: warning: format '%f' expects type 'double', but argument 4
has type 'int'
math_test_11.c:9: warning: format '%f' expects type 'double', but argument 5
has type 'int'


# ./math_test_11
639356810344963382486834444601597699265113107384544848778287582042881171854556760809962494834238640539694738043963701847575050181760552125660054155932857305858816958002965674142423095065121202781270370782793943786455040.000000
 
0.000000  -0.008386  -0.000000
1234.000000  1234.567749  1234.000000  1234.000000


Conclusion:

Printf can't figure out it's own casts (even for consts) and you _must_ use the
cast (float) before any variables that are to be printed with format "%f".

Since GCC can 'see' the "%f" at compile time, _If_ gcc put in the "(float)"
cast (fix all other types too) for you then the printf statement would produce
the correct result and provide the missing piece to what Andrew claims in
comment 4 "abs converts the float/double to an integer type".

A simple modification to GCC could read the format and cast the variable. This
could be integrated with ssp to avoid reading and writing (sprintf()) to the
stack. This does not seem like an expensive mod. 


-- 


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

Reply via email to