Velocity Reviews > Java > How to display a "double" in all its precision???

# How to display a "double" in all its precision???

CS Imam
Guest
Posts: n/a

 08-07-2006
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

EJP
Guest
Posts: n/a

 08-07-2006
CS Imam wrote:

> 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???

There *is no more* precision. A double has 53 bits of binary precision
which is about 16 decimal digits. The 40 decimal digits you're expecting
would take 133 bits.

Maybe you're getting confused between 40 bits of binary precision and 40
decimal digits?

CS Imam
Guest
Posts: n/a

 08-07-2006
Thank you for your reply, but here is the output to clarify what I am
seeing. As you will see, the binary representation shows that the
double is more than capable of representing the numbers in question
(1/2^i where i goes from 1 to 30). As you said, the double gives 52
bits of precision. However, when displaying the number in decimal, Java
appears to be unable to display it correctly - why does it properly
display in base 2, but not in base 10?

I hope I was clearer this time - please pardon me if not! But thanks

For i = 1: 1.5000000000000000000000000000000000000000
11111111111000000000000000000000000000000000000000 000000000000

For i = 2: 1.2500000000000000000000000000000000000000
11111111110100000000000000000000000000000000000000 000000000000

For i = 3: 1.1250000000000000000000000000000000000000
11111111110010000000000000000000000000000000000000 000000000000

For i = 4: 1.0625000000000000000000000000000000000000
11111111110001000000000000000000000000000000000000 000000000000

For i = 5: 1.0312500000000000000000000000000000000000
11111111110000100000000000000000000000000000000000 000000000000

For i = 6: 1.0156250000000000000000000000000000000000
11111111110000010000000000000000000000000000000000 000000000000

For i = 7: 1.0078125000000000000000000000000000000000
11111111110000001000000000000000000000000000000000 000000000000

For i = 8: 1.0039062500000000000000000000000000000000
11111111110000000100000000000000000000000000000000 000000000000

For i = 9: 1.0019531250000000000000000000000000000000
11111111110000000010000000000000000000000000000000 000000000000

For i = 10: 1.0009765625000000000000000000000000000000
11111111110000000001000000000000000000000000000000 000000000000

For i = 11: 1.0004882812500000000000000000000000000000
11111111110000000000100000000000000000000000000000 000000000000

For i = 12: 1.0002441406250000000000000000000000000000
11111111110000000000010000000000000000000000000000 000000000000

For i = 13: 1.0001220703125000000000000000000000000000
11111111110000000000001000000000000000000000000000 000000000000

For i = 14: 1.0000610351562500000000000000000000000000
11111111110000000000000100000000000000000000000000 000000000000

For i = 15: 1.0000305175781250000000000000000000000000
11111111110000000000000010000000000000000000000000 000000000000

For i = 16: 1.0000152587890625000000000000000000000000
11111111110000000000000001000000000000000000000000 000000000000

For i = 17: 1.0000076293945312000000000000000000000000
11111111110000000000000000100000000000000000000000 000000000000

For i = 18: 1.0000038146972656000000000000000000000000
11111111110000000000000000010000000000000000000000 000000000000

For i = 19: 1.0000019073486328000000000000000000000000
11111111110000000000000000001000000000000000000000 000000000000

For i = 20: 1.0000009536743164000000000000000000000000
11111111110000000000000000000100000000000000000000 000000000000

For i = 21: 1.0000004768371582000000000000000000000000
11111111110000000000000000000010000000000000000000 000000000000

For i = 22: 1.0000002384185790000000000000000000000000
11111111110000000000000000000001000000000000000000 000000000000

For i = 23: 1.0000001192092896000000000000000000000000
11111111110000000000000000000000100000000000000000 000000000000

For i = 24: 1.0000000596046448000000000000000000000000
11111111110000000000000000000000010000000000000000 000000000000

For i = 25: 1.0000000298023224000000000000000000000000
11111111110000000000000000000000001000000000000000 000000000000

For i = 26: 1.0000000149011612000000000000000000000000
11111111110000000000000000000000000100000000000000 000000000000

For i = 27: 1.0000000074505806000000000000000000000000
11111111110000000000000000000000000010000000000000 000000000000

For i = 28: 1.0000000037252903000000000000000000000000
11111111110000000000000000000000000001000000000000 000000000000

For i = 29: 1.0000000018626451000000000000000000000000
11111111110000000000000000000000000000100000000000 000000000000

For i = 30: 1.0000000009313226000000000000000000000000
11111111110000000000000000000000000000010000000000 000000000000

-----------------

EJP wrote:
> CS Imam wrote:
>
> > 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???

>
> There *is no more* precision. A double has 53 bits of binary precision
> which is about 16 decimal digits. The 40 decimal digits you're expecting
> would take 133 bits.
>
> Maybe you're getting confused between 40 bits of binary precision and 40
> decimal digits?

Patricia Shanahan
Guest
Posts: n/a

 08-07-2006
CS Imam wrote:
> 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???

Double.toString, used implicitly in conversion of x to a String,
produces the shortest string that, when converted back to double, will
produce the original number.

BigDecimal is the easiest way I know to get all the digits:

System.out.println(new BigDecimal(x));

Patricia

EJP
Guest
Posts: n/a

 08-07-2006
CS Imam wrote:

> Thank you for your reply, but here is the output to clarify what I am
> seeing. As you will see, the binary representation shows that the
> double is more than capable of representing the numbers in question
> (1/2^i where i goes from 1 to 30). As you said, the double gives 52
> bits of precision. However, when displaying the number in decimal, Java
> appears to be unable to display it correctly - why does it properly
> display in base 2, but not in base 10?
>
> I hope I was clearer this time - please pardon me if not! But thanks
> for your help...
>
> For i = 1: 1.5000000000000000000000000000000000000000
> 11111111111000000000000000000000000000000000000000 000000000000
>
> For i = 2: 1.2500000000000000000000000000000000000000
> 11111111110100000000000000000000000000000000000000 000000000000
>
> For i = 3: 1.1250000000000000000000000000000000000000
> 11111111110010000000000000000000000000000000000000 000000000000
>
> For i = 4: 1.0625000000000000000000000000000000000000
> 11111111110001000000000000000000000000000000000000 000000000000
>
> For i = 5: 1.0312500000000000000000000000000000000000
> 11111111110000100000000000000000000000000000000000 000000000000
>
> For i = 6: 1.0156250000000000000000000000000000000000
> 11111111110000010000000000000000000000000000000000 000000000000
>
> For i = 7: 1.0078125000000000000000000000000000000000
> 11111111110000001000000000000000000000000000000000 000000000000
>
> For i = 8: 1.0039062500000000000000000000000000000000
> 11111111110000000100000000000000000000000000000000 000000000000
>
> For i = 9: 1.0019531250000000000000000000000000000000
> 11111111110000000010000000000000000000000000000000 000000000000
>
> For i = 10: 1.0009765625000000000000000000000000000000
> 11111111110000000001000000000000000000000000000000 000000000000
>
> For i = 11: 1.0004882812500000000000000000000000000000
> 11111111110000000000100000000000000000000000000000 000000000000
>
> For i = 12: 1.0002441406250000000000000000000000000000
> 11111111110000000000010000000000000000000000000000 000000000000
>
> For i = 13: 1.0001220703125000000000000000000000000000
> 11111111110000000000001000000000000000000000000000 000000000000
>
> For i = 14: 1.0000610351562500000000000000000000000000
> 11111111110000000000000100000000000000000000000000 000000000000
>
> For i = 15: 1.0000305175781250000000000000000000000000
> 11111111110000000000000010000000000000000000000000 000000000000
>
> For i = 16: 1.0000152587890625000000000000000000000000
> 11111111110000000000000001000000000000000000000000 000000000000
>
> For i = 17: 1.0000076293945312000000000000000000000000
> 11111111110000000000000000100000000000000000000000 000000000000
>
> For i = 18: 1.0000038146972656000000000000000000000000
> 11111111110000000000000000010000000000000000000000 000000000000
>
> For i = 19: 1.0000019073486328000000000000000000000000
> 11111111110000000000000000001000000000000000000000 000000000000
>
> For i = 20: 1.0000009536743164000000000000000000000000
> 11111111110000000000000000000100000000000000000000 000000000000
>
> For i = 21: 1.0000004768371582000000000000000000000000
> 11111111110000000000000000000010000000000000000000 000000000000
>
> For i = 22: 1.0000002384185790000000000000000000000000
> 11111111110000000000000000000001000000000000000000 000000000000
>
> For i = 23: 1.0000001192092896000000000000000000000000
> 11111111110000000000000000000000100000000000000000 000000000000
>
> For i = 24: 1.0000000596046448000000000000000000000000
> 11111111110000000000000000000000010000000000000000 000000000000
>
> For i = 25: 1.0000000298023224000000000000000000000000
> 11111111110000000000000000000000001000000000000000 000000000000
>
> For i = 26: 1.0000000149011612000000000000000000000000
> 11111111110000000000000000000000000100000000000000 000000000000
>
> For i = 27: 1.0000000074505806000000000000000000000000
> 11111111110000000000000000000000000010000000000000 000000000000
>
> For i = 28: 1.0000000037252903000000000000000000000000
> 11111111110000000000000000000000000001000000000000 000000000000
>
> For i = 29: 1.0000000018626451000000000000000000000000
> 11111111110000000000000000000000000000100000000000 000000000000
>
> For i = 30: 1.0000000009313226000000000000000000000000
> 11111111110000000000000000000000000000010000000000 000000000000
>
> -----------------
>
> EJP wrote:
>
>>CS Imam wrote:
>>
>>
>>>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???

No, 16 decimal places really *is* the precision. The *binary* bits
change beyond 16 because the radix is binary not decimal. The 16th
decimal digit expresses a lot more precision than the 16th binary digit.
You need to understant that. In fact I don't understand what you are
expecting to see. The last decimal number printed is 1.0000000009313226
and the last binary number printed converts precisely back to that.
Nothing is being lost. You can't get 40 decimal digits out of 53 bits,
you can only get 16.

Patricia Shanahan
Guest
Posts: n/a

 08-07-2006
EJP wrote:
> CS Imam wrote:
>
>> Thank you for your reply, but here is the output to clarify what I am
>> seeing. As you will see, the binary representation shows that the
>> double is more than capable of representing the numbers in question
>> (1/2^i where i goes from 1 to 30). As you said, the double gives 52
>> bits of precision. However, when displaying the number in decimal, Java
>> appears to be unable to display it correctly - why does it properly
>> display in base 2, but not in base 10?
>>
>> I hope I was clearer this time - please pardon me if not! But thanks
>> for your help...
>>
>> For i = 1: 1.5000000000000000000000000000000000000000
>> 11111111111000000000000000000000000000000000000000 000000000000
>>
>> For i = 2: 1.2500000000000000000000000000000000000000
>> 11111111110100000000000000000000000000000000000000 000000000000
>>
>> For i = 3: 1.1250000000000000000000000000000000000000
>> 11111111110010000000000000000000000000000000000000 000000000000
>>
>> For i = 4: 1.0625000000000000000000000000000000000000
>> 11111111110001000000000000000000000000000000000000 000000000000
>>
>> For i = 5: 1.0312500000000000000000000000000000000000
>> 11111111110000100000000000000000000000000000000000 000000000000
>>
>> For i = 6: 1.0156250000000000000000000000000000000000
>> 11111111110000010000000000000000000000000000000000 000000000000
>>
>> For i = 7: 1.0078125000000000000000000000000000000000
>> 11111111110000001000000000000000000000000000000000 000000000000
>>
>> For i = 8: 1.0039062500000000000000000000000000000000
>> 11111111110000000100000000000000000000000000000000 000000000000
>>
>> For i = 9: 1.0019531250000000000000000000000000000000
>> 11111111110000000010000000000000000000000000000000 000000000000
>>
>> For i = 10: 1.0009765625000000000000000000000000000000
>> 11111111110000000001000000000000000000000000000000 000000000000
>>
>> For i = 11: 1.0004882812500000000000000000000000000000
>> 11111111110000000000100000000000000000000000000000 000000000000
>>
>> For i = 12: 1.0002441406250000000000000000000000000000
>> 11111111110000000000010000000000000000000000000000 000000000000
>>
>> For i = 13: 1.0001220703125000000000000000000000000000
>> 11111111110000000000001000000000000000000000000000 000000000000
>>
>> For i = 14: 1.0000610351562500000000000000000000000000
>> 11111111110000000000000100000000000000000000000000 000000000000
>>
>> For i = 15: 1.0000305175781250000000000000000000000000
>> 11111111110000000000000010000000000000000000000000 000000000000
>>
>> For i = 16: 1.0000152587890625000000000000000000000000
>> 11111111110000000000000001000000000000000000000000 000000000000
>>
>> For i = 17: 1.0000076293945312000000000000000000000000
>> 11111111110000000000000000100000000000000000000000 000000000000
>>
>> For i = 18: 1.0000038146972656000000000000000000000000
>> 11111111110000000000000000010000000000000000000000 000000000000
>>
>> For i = 19: 1.0000019073486328000000000000000000000000
>> 11111111110000000000000000001000000000000000000000 000000000000
>>
>> For i = 20: 1.0000009536743164000000000000000000000000
>> 11111111110000000000000000000100000000000000000000 000000000000
>>
>> For i = 21: 1.0000004768371582000000000000000000000000
>> 11111111110000000000000000000010000000000000000000 000000000000
>>
>> For i = 22: 1.0000002384185790000000000000000000000000
>> 11111111110000000000000000000001000000000000000000 000000000000
>>
>> For i = 23: 1.0000001192092896000000000000000000000000
>> 11111111110000000000000000000000100000000000000000 000000000000
>>
>> For i = 24: 1.0000000596046448000000000000000000000000
>> 11111111110000000000000000000000010000000000000000 000000000000
>>
>> For i = 25: 1.0000000298023224000000000000000000000000
>> 11111111110000000000000000000000001000000000000000 000000000000
>>
>> For i = 26: 1.0000000149011612000000000000000000000000
>> 11111111110000000000000000000000000100000000000000 000000000000
>>
>> For i = 27: 1.0000000074505806000000000000000000000000
>> 11111111110000000000000000000000000010000000000000 000000000000
>>
>> For i = 28: 1.0000000037252903000000000000000000000000
>> 11111111110000000000000000000000000001000000000000 000000000000
>>
>> For i = 29: 1.0000000018626451000000000000000000000000
>> 11111111110000000000000000000000000000100000000000 000000000000
>>
>> For i = 30: 1.0000000009313226000000000000000000000000
>> 11111111110000000000000000000000000000010000000000 000000000000
>>
>> -----------------
>>
>> EJP wrote:
>>
>>> CS Imam wrote:
>>>
>>>
>>>> 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???

>
> No, 16 decimal places really *is* the precision. The *binary* bits
> change beyond 16 because the radix is binary not decimal. The 16th
> decimal digit expresses a lot more precision than the 16th binary digit.
> You need to understant that. In fact I don't understand what you are
> expecting to see. The last decimal number printed is 1.0000000009313226
> and the last binary number printed converts precisely back to that.
> Nothing is being lost. You can't get 40 decimal digits out of 53 bits,
> you can only get 16.

For any number that can be expressed as a terminating binary fraction,
including any number that is representable in Java double, there is a
unique decimal fraction that is EXACTLY equal to it, not just a
close-enough approximation. I believe that is the answer the OP is
looking for.

(In another message, I suggested getting it via BigDecimal).

Patricia

CS Imam
Guest
Posts: n/a

 08-07-2006
Thanks again for replying, you and Patricia both. The BigDecimal DID
work, and that is great to solve the problem at hand.

But I'm really interested in understanding my... misunderstanding I
suppose.

I am not looking for 40 places of decimal precision; I only used
"%.40d" as an overkill to see "all the numbers". I *am* aware that
doubles are supposed to give only 15 digits of decimal precison
approximately. However, what I find puzzling is that in binary, we are
supposed to get 52 (not 53 as far as I know) bits of precision. So here
is my misunderstanding: I see the bits changing in binary. And yet when
they are converted into decimal through the "prints" (and Patricia
pointed out that the problem is really in "toString"), the decimal
equivalent is NOT precise. And this does not make sense to me. If the
underlying raw number in binary IS precise, then the converted decimal
number should be precise as well, right?

When you wrote:

> The last decimal number printed is 1.0000000009313226
> and the last binary number printed converts precisely back to that.
> Nothing is being lost. You can't get 40 decimal digits out of 53 bits,
> you can only get 16.

Actually as far as I know, something IS being lost. The last binary
number, if you convert it to decimal, should be:

1.000000000931322574615478515625

In binary, the underlying bits are as follows:

11111111110000000000000000000000000000010000000000 000000000000

So again, if the underlying bits are precisely expressing some number
within the 52 bits of accuracy, why does converting it to a decimal
representation fail?

I really apologize if I am not seeing something that you are
explaining!

thanks, and sorry again.

EJP wrote:
> CS Imam wrote:
>
> > Thank you for your reply, but here is the output to clarify what I am
> > seeing. As you will see, the binary representation shows that the
> > double is more than capable of representing the numbers in question
> > (1/2^i where i goes from 1 to 30). As you said, the double gives 52
> > bits of precision. However, when displaying the number in decimal, Java
> > appears to be unable to display it correctly - why does it properly
> > display in base 2, but not in base 10?
> >
> > I hope I was clearer this time - please pardon me if not! But thanks
> > for your help...
> >
> > For i = 1: 1.5000000000000000000000000000000000000000
> > 11111111111000000000000000000000000000000000000000 000000000000
> >
> > For i = 2: 1.2500000000000000000000000000000000000000
> > 11111111110100000000000000000000000000000000000000 000000000000
> >
> > For i = 3: 1.1250000000000000000000000000000000000000
> > 11111111110010000000000000000000000000000000000000 000000000000
> >
> > For i = 4: 1.0625000000000000000000000000000000000000
> > 11111111110001000000000000000000000000000000000000 000000000000
> >
> > For i = 5: 1.0312500000000000000000000000000000000000
> > 11111111110000100000000000000000000000000000000000 000000000000
> >
> > For i = 6: 1.0156250000000000000000000000000000000000
> > 11111111110000010000000000000000000000000000000000 000000000000
> >
> > For i = 7: 1.0078125000000000000000000000000000000000
> > 11111111110000001000000000000000000000000000000000 000000000000
> >
> > For i = 8: 1.0039062500000000000000000000000000000000
> > 11111111110000000100000000000000000000000000000000 000000000000
> >
> > For i = 9: 1.0019531250000000000000000000000000000000
> > 11111111110000000010000000000000000000000000000000 000000000000
> >
> > For i = 10: 1.0009765625000000000000000000000000000000
> > 11111111110000000001000000000000000000000000000000 000000000000
> >
> > For i = 11: 1.0004882812500000000000000000000000000000
> > 11111111110000000000100000000000000000000000000000 000000000000
> >
> > For i = 12: 1.0002441406250000000000000000000000000000
> > 11111111110000000000010000000000000000000000000000 000000000000
> >
> > For i = 13: 1.0001220703125000000000000000000000000000
> > 11111111110000000000001000000000000000000000000000 000000000000
> >
> > For i = 14: 1.0000610351562500000000000000000000000000
> > 11111111110000000000000100000000000000000000000000 000000000000
> >
> > For i = 15: 1.0000305175781250000000000000000000000000
> > 11111111110000000000000010000000000000000000000000 000000000000
> >
> > For i = 16: 1.0000152587890625000000000000000000000000
> > 11111111110000000000000001000000000000000000000000 000000000000
> >
> > For i = 17: 1.0000076293945312000000000000000000000000
> > 11111111110000000000000000100000000000000000000000 000000000000
> >
> > For i = 18: 1.0000038146972656000000000000000000000000
> > 11111111110000000000000000010000000000000000000000 000000000000
> >
> > For i = 19: 1.0000019073486328000000000000000000000000
> > 11111111110000000000000000001000000000000000000000 000000000000
> >
> > For i = 20: 1.0000009536743164000000000000000000000000
> > 11111111110000000000000000000100000000000000000000 000000000000
> >
> > For i = 21: 1.0000004768371582000000000000000000000000
> > 11111111110000000000000000000010000000000000000000 000000000000
> >
> > For i = 22: 1.0000002384185790000000000000000000000000
> > 11111111110000000000000000000001000000000000000000 000000000000
> >
> > For i = 23: 1.0000001192092896000000000000000000000000
> > 11111111110000000000000000000000100000000000000000 000000000000
> >
> > For i = 24: 1.0000000596046448000000000000000000000000
> > 11111111110000000000000000000000010000000000000000 000000000000
> >
> > For i = 25: 1.0000000298023224000000000000000000000000
> > 11111111110000000000000000000000001000000000000000 000000000000
> >
> > For i = 26: 1.0000000149011612000000000000000000000000
> > 11111111110000000000000000000000000100000000000000 000000000000
> >
> > For i = 27: 1.0000000074505806000000000000000000000000
> > 11111111110000000000000000000000000010000000000000 000000000000
> >
> > For i = 28: 1.0000000037252903000000000000000000000000
> > 11111111110000000000000000000000000001000000000000 000000000000
> >
> > For i = 29: 1.0000000018626451000000000000000000000000
> > 11111111110000000000000000000000000000100000000000 000000000000
> >
> > For i = 30: 1.0000000009313226000000000000000000000000
> > 11111111110000000000000000000000000000010000000000 000000000000
> >
> > -----------------
> >
> > EJP wrote:
> >
> >>CS Imam wrote:
> >>
> >>
> >>>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???

>
> No, 16 decimal places really *is* the precision. The *binary* bits
> change beyond 16 because the radix is binary not decimal. The 16th
> decimal digit expresses a lot more precision than the 16th binary digit.
> You need to understant that. In fact I don't understand what you are
> expecting to see. The last decimal number printed is 1.0000000009313226
> and the last binary number printed converts precisely back to that.
> Nothing is being lost. You can't get 40 decimal digits out of 53 bits,
> you can only get 16.

Chris Uppal
Guest
Posts: n/a

 08-07-2006
CS Imam wrote:

> However, what I find puzzling is that in binary, we are
> supposed to get 52 (not 53 as far as I know) bits of precision. So here
> is my misunderstanding: I see the bits changing in binary. And yet when
> they are converted into decimal through the "prints" (and Patricia
> pointed out that the problem is really in "toString"), the decimal
> equivalent is NOT precise. And this does not make sense to me. If the
> underlying raw number in binary IS precise, then the converted decimal
> number should be precise as well, right?

I think what you may be missing is that there is a /range/ of precise decimal
numbers which would all have the same representation as a double. So, although
any given double converts exactly into precisely one arbitrary-precision
decimal number, that number is not the only one which the double value may be
"trying" to represent.

The string representation has to /choose/ one value from the infinite set of
arbitrary-precision decimal numbers which the double value might be intended
to represent. One option would be to chose the unique element which was
exactly equal to the double, but that's not the only possible design. In fact
(and defensibly, IMO, although it would be nice to have a choice) the element
which is chosen is the one with the fewest digits -- otherwise, for instance,
0.1D would print out as:
0.100000000000000005551115123125782702118158340454 1015625
which is certainly precise, but is probably not what most programmers (or
users) would wish to see.

-- chris

hiwa
Guest
Posts: n/a

 08-07-2006
This is the funniest post of this summer.
The OP reminds me of some type of elderly people who stubbonly believe
in an impossible.
Thanks for a big laugh.

Seriously:
Learn and study IEEE 754 bit-array-representation of 64 bit FPN.

Patricia Shanahan
Guest
Posts: n/a

 08-07-2006
hiwa wrote:
> This is the funniest post of this summer.
> The OP reminds me of some type of elderly people who stubbonly believe
> in an impossible.

Section 3.2.2 Double of ANSI/IEEE Std 754-1985 begins "A 64-bit double
format number X is divided as shown in in Fig. 2. The value v of X is
inferred from its constituent fields thus:" followed by a series of cases.

The relevant case is "(3) If 0 < e < 2047, then ..." followed by a
formula for the value of a finite, normalized, non-zero double number.
Using "^" for exponentiation, and "*" for multiplication, and "." for
the centered dot, it is equivalent to:

(-1)^s * 2^(e-1023 *(1 "." f) where s is the sign bit, e is the
exponent, and f is the fraction.

I interpreted the base article as requesting a print out, in decimal, of
that value. My BigDecimal suggestion, which does exactly that, worked
for the OP, so that seems to be the correct interpretation.

Why do you consider this to be impossible? Or do you disagree with my
interpretation?

> Thanks for a big laugh.

Although I find IEEE 754 floating point arithmetic both interesting and
useful, I'm completely missing its humor. I know explanations can
sometimes kill a joke, but perhaps you could explain what is so funny?

> Seriously:
> Learn and study IEEE 754 bit-array-representation of 64 bit FPN.
>

I've read the standard from cover to cover a couple of times, and reread
individual sections far more often. I've looked at many, many doubles as
bit patterns. To me, the OP's request seemed quite reasonable. Indeed,
it is something I've needed for myself when working on understanding
some of the subtleties of rounding, so I already had a solution. What am
I missing?

Patricia