|
-
October 4th, 2010, 06:49 PM
#4
Re: double to int issue. losing a penny
Take a look at this.
http://www.parashift.com/c++-faq-lit...html#faq-29.16
It seems annoying that the iomanip doesn't include anything to truncate but I think that it is impossible to implement due to problems across platforms with floating point representations. If I manually truncate in the following way I still get a strange response, consistent with that explanation. This code will give you what you want (xxxx.36) but if you step with the debugger you'll see that numDollars is now xxxx.35999999997 or something to that effect. It simply isn't possible to truncate the way you think you should be able to because in the computer it is not possible to represent 0.36 perfectly.
double numDollars = numEuros / numEurosPerDollar;
numDollars = floor(numDollars*100.0) / 100.0;
cout << "\n\n" << numEuros << " euros will convert to " << numDollars << " US dollars.\n\n";
// prints 0.36 as I expect but in the debugger value is 0.35999999999999999
double value(.36);
std::cout << "value : " << value << std::endl;
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
|