atof really puzzles me

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone explain to me that why the flowling code

printf("%f\n", atof("12345678901234567890"));

produces this result:

12345678901234567000.000000

Is it that printf cannot handle double data correctly or that I am doing
right things?

Great thanks.
 
Neo The One said:
Can anyone explain to me that why the flowling code

printf("%f\n", atof("12345678901234567890"));

produces this result:

12345678901234567000.000000

Is it that printf cannot handle double data correctly or that I am doing
right things?

Great thanks.

I can replicate the behavior by storing the result of the atof() in a
double, then displaying the content using cout, so I don't think printf()
has anything to do with it. Beyond that, I'm at a loss. Hopefully one of the
experts here will satisfy both our curiosities.
 
Neo said:
Can anyone explain to me that why the flowling code

printf("%f\n", atof("12345678901234567890"));

produces this result:

12345678901234567000.000000

Is it that printf cannot handle double data correctly or that I am
doing right things?

double only has 17 digits of accuracy, you're trying to parse 19 significant
digits.

-cd
 
Peter van der Goes said:
I can replicate the behavior by storing the result of the atof() in a
double, then displaying the content using cout, so I don't think printf()
has anything to do with it. Beyond that, I'm at a loss. Hopefully one of the
experts here will satisfy both our curiosities.

I'm not an expert, but off hand I'd say the result is correct within the
specified 16 bits of precision for an IEEE754 double.

Jeff F
 
Neo said:
Can anyone explain to me that why the flowling code

printf("%f\n", atof("12345678901234567890"));

produces this result:

12345678901234567000.000000

Is it that printf cannot handle double data correctly or that I am doing
right things?


I blieve this has todo with the fact that IEEE standard double uses 53
bits as a mantissa (plus 11 exponent) which would at the very maximum
allow for exact integer values in the range of [-4.503.599.627.370.495;
4.503.599.627.370.495].

12.345.678.901.234.567.890 is clearly outside this range.

Google for "what every programmer should know about floating point
arithmetic"
 
Neo said:
Can anyone explain to me that why the flowling code

printf("%f\n", atof("12345678901234567890"));

produces this result:

12345678901234567000.000000

Is it that printf cannot handle double data correctly or that I am doing
right things?

Great thanks.

A double have 1 bit size, 11 bits exponent and 52 bits mantissa.
Considering one hidden bit for mantissa we have 53 bits of information.
2^53 is close to 9*10^15, which is approximately 16 usable digits in a
double. 17 digits that we see in your example are pretty close to it.
 
Then what is the meaning of its 1.7e+308 range if it can't express integers
like that?

Ben Schwehn said:
Neo said:
Can anyone explain to me that why the flowling code

printf("%f\n", atof("12345678901234567890"));

produces this result:

12345678901234567000.000000

Is it that printf cannot handle double data correctly or that I am doing
right things?


I blieve this has todo with the fact that IEEE standard double uses 53
bits as a mantissa (plus 11 exponent) which would at the very maximum
allow for exact integer values in the range of [-4.503.599.627.370.495;
4.503.599.627.370.495].

12.345.678.901.234.567.890 is clearly outside this range.

Google for "what every programmer should know about floating point
arithmetic"
 
Can anyone explain to me that why the flowling code

printf("%f\n", atof("12345678901234567890"));

produces this result:

12345678901234567000.000000

Is it that printf cannot handle double data correctly or that I am doing
right things?

You are missing the fact that double has finite precision. To be
precise, it has 53 bits of mantissa, which equates to a minimum of 15
digits of precision.

If you want an arbitrary precision number, you'll have to use an
appropriate library. See http://www.oonumerics.org/oon/ for
suggestions.

Tom
 
Neo said:
Then what is the meaning of its 1.7e+308 range if it can't express integers
like that?

you can (because of the exponent), but not precisely.
You can't even express 0.1 precisely using floating point numbers.
(using base 2 exponent)

should be:

Google for "what every computer scientist should know about floating
point arithmetic". do it if you want to know all the details.
 
Its basically a problem with doubles and nothing to do with atof.

You are just running out of precision, try the following

double d = 12345678901234567890;
double d2 = 12345678901234567000;
if (d == d2)
printf("Whoops\n");

Cheers

Andy
 
So double cannot express large integers like 12345678901234567890 or its
close 12345678901234567899.999999999999999 ?
 
Back
Top