Precision Lost?

M

Man Seesaw

Hi all,

I've encounter this problem that puzzles me a lot.
Here is the code:

double x = 12.93;
cout << "*100, int = " << static_cast<int>(x * 100.0) << endl;
x *= 100.0;
cout << "*=100, int = " << static_cast<int>(x) << endl;

And it gives me:
*100, int = 1292
*=100, int = 1293

Any explanations?
Thanks in advance.

Best regards,
MS
 
P

Paulo da Silva

Em 18-12-2010 18:51, Man Seesaw escreveu:
Hi all,

I've encounter this problem that puzzles me a lot.
Here is the code:

double x = 12.93;
cout << "*100, int = " << static_cast<int>(x * 100.0) << endl;
x *= 100.0;
cout << "*=100, int = " << static_cast<int>(x) << endl;

And it gives me:
*100, int = 1292
*=100, int = 1293

Any explanations?
Thanks in advance.
Your FPU uses more internal bits to perform computations than those
needed to store a double. Probably on one case the result was converted
directly from an internal register and on the other from a previously
stored value.
For details you have to look at the assembly code produced and/or how
your fpu represents doubles.
Perhaps changing the degree of your compiler optimization should produce
different values.

BTW, what compiler/processor are you using? I have tried it with
gcc-4.4.4 (-O0,-O2,-O3) in a core2 and always got 1293.

hth.
 
G

Geoff

Hi all,

I've encounter this problem that puzzles me a lot.
Here is the code:

double x = 12.93;
cout << "*100, int = " << static_cast<int>(x * 100.0) << endl;
x *= 100.0;
cout << "*=100, int = " << static_cast<int>(x) << endl;

And it gives me:
*100, int = 1292
*=100, int = 1293

Any explanations?
Thanks in advance.

Best regards,
MS

With what compiler? What version? In what environment?
The implementations I tried it with all yield 1293 in both cases.
 
M

Man Seesaw

BTW, what compiler/processor are you using? I have tried it with
gcc-4.4.4 (-O0,-O2,-O3) in a core2 and always got 1293.

hth.

My compiler is g++ (QP MinGW32) 4.4.3
And my processor is Intel(R) Pentium(R) D CPU 2.66GHz, 2660 Mhz, 2
Core(s), 2 Logical Processor(s)
 
M

Man Seesaw

Thank you all for your help very much.
At least I've got some idea about this kind of stuff.
 
R

Ruslan Mullakhmetov

of weird rounding errors. My crystal ball tells me you are using Intel
computer which has 80-bit internal FP registers and a compiler with 64-
bit doubles. Then the exact result becomes the question of optimizations
the compiler has applied to your code. I bet the optimizations in your
usage cases are different, study the assembler code to verify that.

Good crystal ball :) Where did you bought one?

BR, RM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top