473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

atof really puzzles me

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.
Nov 17 '05 #1
10 1356

"Neo The One" <Ne*******@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 17 '05 #2
Neo The One wrote:
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
Nov 17 '05 #3

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...

"Neo The One" <Ne*******@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
Can anyone explain to me that why the flowling code

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

produces this result:

12345678901234567000.000000
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
Nov 17 '05 #4
Neo The One wrote:
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"

--
Ben
http://bschwehn.de
Nov 17 '05 #5
Neo The One wrote:
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.

Nov 17 '05 #6
Then what is the meaning of its 1.7e+308 range if it can't express integers
like that?

"Ben Schwehn" wrote:
Neo The One wrote:
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"

--
Ben
http://bschwehn.de

Nov 17 '05 #7
On Thu, 23 Sep 2004 03:45:12 -0700, "Neo The One"
<Ne*******@discussions.microsoft.com> wrote:
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
Nov 17 '05 #8
Neo The One wrote:
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)
Google for "what every programmer should know about floating point
arithmetic"


should be:

Google for "what every computer scientist should know about floating
point arithmetic". do it if you want to know all the details.
--
Ben
http://bschwehn.de
Nov 17 '05 #9
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
"Neo The One" wrote:
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.

Nov 17 '05 #10
So double cannot express large integers like 12345678901234567890 or its
close 12345678901234567899.999999999999999 ?
"Andy Capon" wrote:
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
"Neo The One" wrote:
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.

Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sreekanth | last post by:
Hello, Am trying to convert a string to float. Am using atof() for that purpose. But the return value for atof is same for the string "0.0" and for some invalid input "Invalid". Can any body...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
20
by: Trond Valen | last post by:
Hi! Stupid atof, it returns 0.0 when it tries to parse something like "fish". So I don't know whether the number was really 0 or a string that couldn't be parsed. Is there a better way to do...
21
by: oksuresh | last post by:
Hi talents, I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 But I would like to have...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
14
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) {...
5
by: lcw1964 | last post by:
Greetings again, I will burden the group with yet another tenderfoot question, but since conscientious googling hasn't yield a lucid answer I thought I would risk the shortcut of asking here...
2
by: allexander | last post by:
Hello ! I need to convert a string like string number = "1235646.5678" to any floating point number x (double x or float x) I have used earlier the function atof()
62
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.