-
June 23rd, 2009, 09:52 AM
#1
A Rounding Problem
Hey there!
Have a look at the code below:
Code:
#include <iostream>
#include <cmath>
int main()
{
double num = log10(8);
double den = log10(2);
int n = num/den;
int m = (double)3;
double lf = num/den;
std::cout << n << ' ' << m << ' ' << lf << std::endl;
return 0;
}
This prints "2 3 3". Obviously the number (num/den) is *almost* 3 but not exactly 3 (because both logarithms are irrational and cannot be represented in decimal form). The interesting part here is that the double (3.) and the variable "lf" are represented in the same way in the memory! They're completely equivalent bit by bit (you can check this out, but I already did, so trust me). Also sizeof(num/den) is 8, which makes it double, rather than long double. Now tell me why 'n' differs from 'm'.
Is the compiler evaluating 'n' from the extensive result of the division (num/den), rather then from the resulting double? Or is there any other explanation?
-
June 23rd, 2009, 10:37 AM
#2
Re: A Rounding Problem
Is the compiler evaluating 'n' from the extensive result of the division (num/den), rather then from the resulting double? Or is there any other explanation?
actually, there are no guarantees (mandated by the standard) on how an expression involving floating point numbers is precisely translated by the compiler. See the precise/fast/strict in your compiler doc (in VC is the /fp compiler option).
see also http://www.nccs.nasa.gov/images/Floa...onsistency.pdf
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|