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
wxString ToDouble precision Topic is solved

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

 In need of some credit
 Posts: 9
 Joined: Thu Apr 24, 2008 6:48 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
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

 Knows some wx things
 Posts: 26
 Joined: Tue Nov 14, 2006 1:51 pm
 Location: Duesseldorf, Germany
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 fixedpoint or arbitraryprecision arithmetic library.AnjaStemme wrote:Actually this seems to be a plain c++ float/double precision issue:

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

 Knows some wx things
 Posts: 26
 Joined: Tue Nov 14, 2006 1:51 pm
 Location: Duesseldorf, Germany
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).
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
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).

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