gcc 4.4.2 : float multiplication 4097*4097 seems to be off by 1

Tim Prince n8tm@aol.com
Fri Nov 12 06:42:00 GMT 2010


On 11/11/2010 9:57 PM, 0throot wrote:
> In one of my programs, I found a wierd problem with float multiplication. Any
> insights you can provide will be very helpful.
>
> The program is as follows,
>
> #include<stdio.h>
> int main(void) { float a=4097, b=4097, c=0; c=a*b; printf("%12.2f != %d\n",
> c, 4097*4097); return 0; }
>
> The output i get is,
>
> 16785408.00 != 16785409
>
> I never thought using float over int could have such adverse effects.
>
> Is this the correct behavior ? or am i doing something wrong here ?
>
> Following are the system details,
>
> gcc version 4.4.2 20091027 (Red Hat 4.4.2-7) (GCC)
> Fedora release 12 (Constantine)
> Linux lap.local 2.6.31.5-127.fc12.i686.PAE #1 SMP Sat Nov 7 21:25:57 EST
> 2009 i686 i686 i386 GNU/Linux
>
> 0/
As you've demonstrated, float values > 1/FLT_EPSILON can represent only 
even values.  If you have implemented the nextafterf() function with the 
usual meaning, you will see that nextafterf(4097*4097) - 4097*4097 is an 
even number.  Of course, if you're used to compiling with x87, 
implicitly promoting everything in register to wider types, you may be 
used to different results, possibly different between -O0 and -O1.

-- 
Tim Prince



More information about the Gcc-help mailing list