Programming/Java
Java reals IEEE754
halatha
2011. 5. 5. 05:53
http://people.uncw.edu/tompkinsj/133/numbers/Reals.htm
float
| 1bit sign | 8 bit exponent | 23 bit mantissa |
double
| 1bit sign | 11 bit exponent | 52 bit mantissa |
sign: 0 + 1 -
exponent: exponent - 127 for float, exponent - 1023 for double
mantissa: normalized 1-plus fraction
print bits of 1.0D 00111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
(0000 00000000 00000000 00000000 00000000 00000000 00000000 + 1) * 2 ^ (0111111 1111 - 1023) = 1.0
float
| 1bit sign | 8 bit exponent | 23 bit mantissa |
double
| 1bit sign | 11 bit exponent | 52 bit mantissa |
sign: 0 + 1 -
exponent: exponent - 127 for float, exponent - 1023 for double
mantissa: normalized 1-plus fraction
print bits of 1.0D 00111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
(0000 00000000 00000000 00000000 00000000 00000000 00000000 + 1) * 2 ^ (0111111 1111 - 1023) = 1.0
public class TestIEEE754 { public static void printBits(final long l) { for ( int i = 63; i >= 0; --i ) { final long bit = 0x1L << i; System.out.print((l & bit) != 0 ? "1" : "0"); if ( 0 == i % 8 ) System.out.print(" "); } System.out.println(); } // http://people.uncw.edu/tompkinsj/133/numbers/Reals.htm public static void main(final String[] args) { final long l = 1L; System.out.println("Double.longBitsToDouble(" + l + ")\t" + Double.longBitsToDouble(l)); System.out.print("print bits of " + l + "L\t"); printBits(l); System.out.println("Double.longBitsToDouble(" + l + ")\t" + Double.longBitsToDouble(l)); final double d = 1.0D; System.out.println("\nDouble.toHexString(" + d + ")\t" + Double.toHexString(d)); System.out.println("Double.doubleToLongBits(" + d + ")\t" + Double.doubleToLongBits(d)); System.out.print("print bits of " + d + "D\t"); printBits(Double.doubleToLongBits(d)); System.out.println("Double.doubleToRawLongBits(" + d + ")\t" + Double.doubleToRawLongBits(d)); System.out.print("print bits of " + d + "D\t"); printBits(Double.doubleToRawLongBits(d)); } }