unexpected behaviour of 'atof' and 'strtod'
-
I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62
-
I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62
Member 3611002 wrote:
what do i do...
Read here.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Member 3611002 wrote:
what do i do...
Read here.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Looks like it will take weeks to get through that doc [:)] Can you please explain in simple words what is the cause and resolution of this behavior if I may request so ?
cagespear wrote:
Looks like it will take weeks to get through that doc
So just search for another one. They're plentiful. Here for example.
cagespear wrote:
Can you please explain in simple words what is the cause...
Computers are binary (i.e., base-2) machines, thus they cannot exactly represent floating-point numbers as you might think.
cagespear wrote:
...and resolution of this behavior if I may request so ?
Epsilon.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62
From the C++ FAQ Lite: Why is floating point so inaccurate? Why doesn't this print 0.43?[^]
--Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen
-
I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62
There are two problems with real numbers on computers. 1. Not every real number have an exact binary representation. For example 0.1 when represented in binary is 0.0001100110011001100110011001100110011001100110011001101 now when you convert it back to real number you will get 0.10000000000000001. Note you can try this calculation by hand and this inaccuracy has got nothing to do with how we represent real numbers in computer. 2. There are uncountably infinite real numbers but computers can only represent and manipulate finite numbers (Well at least in hardware). This means that not every real number can be expressed in computer. Hope this makes it clear. -Saurabh
-
I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62
Simply, this is related with binary representation of floating point numbers.
Member 3611002 wrote:
The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62
This is normal and there is nothing to do for a double type. if you format that number back using format string %.2f, you will get 37.62 again. I'll try to comment on, shortly. As you may know, double is a 64 bit floating point number. There are some special formats (standards defined by IEEE in this case) to store it in memory. It consists of 3 parts; sign (1), exponent (11), significand (52 bits). There are also special rules for storing exponent and significand. Most important one, exponent part is two's exponent, not ten's. Moreover, some operations (i.e. rounding and normalization) have been applied to a floating point number after every operation. After all, it is stored as nearest possible value fitting into these rules.