Re: sizeof float in Xcode debugger
Re: sizeof float in Xcode debugger
- Subject: Re: sizeof float in Xcode debugger
- From: Greg Guerin <email@hidden>
- Date: Mon, 23 Jun 2008 15:36:33 -0700
Michael McLaughlin wrote:
For instance, a float which, in memory, is
3F4D53D0
in both debuggers is rendered in the CodeWarrior debugger as
0.802060
but shows up in Xcode (GDB) as
0.802060127
Subsequent computations seem to indicate that this difference
propagates
throughout the rest of the program.
I doubt that a float could really have nine sig. figs. as Xcode
indicates
and wonder if some garbage might be getting included somehow.
You seem to be confusing "sig. figs." (i.e. significant figures, or
significant decimal digits), with significant bits.
The binary representation, i.e. the actual bits of the float value,
are exact. They represent an exact number in base-2. The base-10
(decimal) representation may not be exactly the same as the exact
binary representation. Since the binary representation has a limited
size, there is a point beyond which decimal digits are no longer
significant, i.e. the digits given have no effect on the bits of the
float.
The GDB representation could simply be an artifact of how GDB
displays float values. Conversion from float to double (which would
be exact in base-2), and then expanding that double value to decimal
representation (which would not be exact), and presenting 9 decimal
digits, will ensure all significant BITS are correct, with the
minimum number of significant decimal DIGITS. (See URL below.)
Example in Java:
FloatA.java
---
public class FloatA
{
public static void main( String[] args )
{
int floatBits = 0x3F4D53D0;
float floater = Float.intBitsToFloat( floatBits );
System.out.println( " floater: " + floater + ", " + ((double)
floater) );
long doubleBits = Double.doubleToRawLongBits( (double) floater );
System.out.println( " doubleBits: 0x" + Long.toHexString
( doubleBits ) );
}
}
Shell commands and output:
javac FloatA.java
java FloatA
floater: 0.8020601, 0.8020601272583008
doubleBits: 0x3fe9aa7a00000000
Note the run of 0's in the LSB's of doubleBits. Also note the 4 LS
bits of your original float value.
If you don't like Java, I'm sure someone can contribute the
equivalent C version.
For further info:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
and find "single precision numbers will be printed" on that page.
-- GG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden