Bug 20017 - Rounding doubles differs between calculated and hard coded values
Summary: Rounding doubles differs between calculated and hard coded values
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 7.2.3
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-05-23 14:59 UTC by Josh Hogan
Modified: 2014-05-25 10:52 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
RoundingTest project (4.14 KB, application/ms-download)
2014-05-23 14:59 UTC, Josh Hogan
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description Josh Hogan 2014-05-23 14:59:22 UTC
Created attachment 6870 [details]
RoundingTest project

I've encountered a strange problem with rounding using Math.Round's double signatures.

Hard coded doubles seem to round correctly, while "calculated" doubles do not.

I had a little trouble illustrating the problem clearly, but I've attached a test project.

I'd be happy to provide additional information if necessary.
Comment 1 Sebastien Pouliot 2014-05-23 16:15:18 UTC
The joy of IEEE floating point values…

Your calculated result is not what you think it is. You can use the"R" (roundtrip) to see the exact value (that the CPU computed), e.g.

	double discounted = price - (price * 0.10);
	Console.WriteLine("10% off   = {0}", discounted.ToString ("R"));

and you'll get:

	10% off   = 13.454999999999998

and that (real value) will round down to 13.45.

To avoid this you might want to use the Decimal type for any financial calculations.
Comment 2 Josh Hogan 2014-05-23 17:06:03 UTC
*headdesk*

So obvious in hindsight.  Decimal it is!

This probably isn't the right forum for discussion, but why doesn't the debugger show me 13.454999999999998?

Thanks.
Comment 3 Sebastien Pouliot 2014-05-25 10:52:58 UTC
By default debuggers (both XS and VS) use `ToString` to show the content of the value.

.NET set this format value for float/double to a fixed point format (not the the "R" roundtrip format) likely because it's easier to read that way. It works wel (most of the time) but you can get weird results some time.