wxString ToDouble precision Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
AnjaStemme
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Apr 24, 2008 6:48 am

wxString ToDouble precision

Post by AnjaStemme » Thu Apr 24, 2008 6:58 am

Hi,

I am using the unicode version of wxWidgets 2.8.7 on Linux. The "ToDouble" conversion method shows a strange behavior:

wxString test ("1.0");
double testd;
test.ToDouble(&testd);

----> testd will have a value of 1
wxString test ("1.1");
double testd;
test.ToDouble(&testd);

----> testd will have a value of 1.10000000000000001

Has anybody observed this behavior before? Is it a bug or a feature ?

Thanks for comments
Anja

AnjaStemme
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Apr 24, 2008 6:48 am

Post by AnjaStemme » Thu Apr 24, 2008 7:44 am

Actually this seems to be a plain c++ float/double precision issue:

float a;

a = 0.1;

------> results to a = 0.100000001

double c;

c= 0.1;

------> results to a = 0.10000000000000001

double e;

e = 100.0/1000.0;

------> results to a = 0.10000000000000001


Has anybody a hint how to get rid of this imprecise last ''1''? Why does it appear?

Thanks!
Anja

S.Volkenandt
Knows some wx things
Knows some wx things
Posts: 26
Joined: Tue Nov 14, 2006 1:51 pm
Location: Duesseldorf, Germany

Post by S.Volkenandt » Thu Apr 24, 2008 7:52 am

AnjaStemme wrote:Actually this seems to be a plain c++ float/double precision issue:
Actually, it's the C(++) floating point precision issue. Floating point numbers just are not precise. If you can't store 1.1 in a double, then a double simply can't represent 1.1 exactly. You will have to live with that or use a fixed-point- or arbitrary-precision arithmetic library.

AnjaStemme
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Apr 24, 2008 6:48 am

Post by AnjaStemme » Thu Apr 24, 2008 8:12 am

Thanks for the hint!
However, I did experience this problem on Windows, so it cannot be a principle representation issue with respect to floating point numbers?

Anja

S.Volkenandt
Knows some wx things
Knows some wx things
Posts: 26
Joined: Tue Nov 14, 2006 1:51 pm
Location: Duesseldorf, Germany

Post by S.Volkenandt » Thu Apr 24, 2008 9:19 am

You mean you didn't experience this problem on windows?

Naturally it depends on the precision of the datatype itself. But mathematically, floating point numbers are stored in terms of Mantisse * 2 ^ Exponent (IEEE754) where both Mantisse and Exponent are integers of a limited width.

So, to store 1.1 exactly, you'd have to solve the equation

Code: Select all

m*2^e = 1.1
where m € N and e € N. Which is not possible I guess.

When using floating point numbers, one usually rounds when outputting the value (so, your value rounded to, say, 4 digits is 1.1000, which is 1.1, again).

AnjaStemme
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Apr 24, 2008 6:48 am

Post by AnjaStemme » Thu Apr 24, 2008 10:58 am

Yes, sorry, I meant I DID NOT experience corresponding problems on Windows.

However, the basic problem was a comparison of two doubles. I have no idea why this did not lead to any problems on Windows. For Linux I used now the simple solution: convert it to integer.

Thanks for your help!!!
Anja

Post Reply