www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/20/18:47:02

Date: Sun, 20 Nov 94 15:13:50 -0500
From: dj AT stealth DOT ctron DOT com (DJ Delorie)
To: dolan AT fnoc DOT navy DOT mil
Cc: davis AT amy DOT tch DOT harvard DOT edu, djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: printf or floating point error?????

>   long i = 10 ;
>   float x , y , z ;
>   double w ;
> 
>   printf( "i=%8.8XH, x=%8.8XH, y=%8.8XH, z=%8.8XH\n", i, x, y, z );
> -------------------------------8<---cut here--->8-------------------------------
> long is: 4 bytes, float is: 4 bytes, double is: 8 bytes.
> i=0000000AH, x=40240000H, y=00000000H, z=40240000H
> i=10, x=10, y=10, z=1.4013e-44
> -------------------------------8<---cut here--->8-------------------------------
> 
> Huh?
> 
> Variables x and z hold the same bit pattern, but x and y give the same results?

ANSI states that floats are promoted to double when passed to a
function with a formal prototype type of "...".  Thus, you are passing
8 bytes per float, not 4, and taking up two %X per float instead of
just one.  You need to use this:

	*(int *)(&x)

to pass only the four bytes of the float variable itself.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019