Page **1** of **1**

### wxString ToDouble precision

Posted: **Thu Apr 24, 2008 6:58 am**

by **AnjaStemme**

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

Posted: **Thu Apr 24, 2008 7:44 am**

by **AnjaStemme**

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

Posted: **Thu Apr 24, 2008 7:52 am**

by **S.Volkenandt**

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.

Posted: **Thu Apr 24, 2008 8:12 am**

by **AnjaStemme**

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

Posted: **Thu Apr 24, 2008 9:19 am**

by **S.Volkenandt**

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

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).

Posted: **Thu Apr 24, 2008 10:58 am**

by **AnjaStemme**

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