C
CS Imam
Hello,
Here is a code fragment that is very simple... but I can't get it to
work!
public static void main(String[] args)
{
for (int i = 1; i <= 30 ; i++)
{
double x = Math.pow(2, i);
x = 1 + 1 / x;
System.out.printf("For i = %d: %.40f%n", i, x);
System.out.println(
Long.toBinaryString(Double.doubleToLongBits(x)) );
System.out.println();
}
}
All this code is supposed to do is print out the fractions 1+1/2,
1+1/4, 1+1/8, etc. When one prints out the raw bits (see
doubleToLongBits), the code is clearly working.
But on the regular printf("For i...etc"), at i=17 and above, the
numbers get frozen at 16 digits displayed after the decimal point (the
precision). But it's not really the precision, because the bits ARE
changing correctly. What gives???
Help!
- not a stunningly gorgeous woman who would marry you if you solve this
problem
Here is a code fragment that is very simple... but I can't get it to
work!
public static void main(String[] args)
{
for (int i = 1; i <= 30 ; i++)
{
double x = Math.pow(2, i);
x = 1 + 1 / x;
System.out.printf("For i = %d: %.40f%n", i, x);
System.out.println(
Long.toBinaryString(Double.doubleToLongBits(x)) );
System.out.println();
}
}
All this code is supposed to do is print out the fractions 1+1/2,
1+1/4, 1+1/8, etc. When one prints out the raw bits (see
doubleToLongBits), the code is clearly working.
But on the regular printf("For i...etc"), at i=17 and above, the
numbers get frozen at 16 digits displayed after the decimal point (the
precision). But it's not really the precision, because the bits ARE
changing correctly. What gives???
Help!
- not a stunningly gorgeous woman who would marry you if you solve this
problem