Bug 8747 - Math.Abs(0) is greater than double.Epsilon when run on a real device under iOS
Summary: Math.Abs(0) is greater than double.Epsilon when run on a real device under iOS
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-12-04 11:13 UTC by Andrew
Modified: 2012-12-07 16:27 UTC (History)
5 users (show)

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

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 GitHub or Developer Community 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:

Description Andrew 2012-12-04 11:13:39 UTC
Math.Abs seems to spoils zero double value, when executed on a real iOS device. In the same time, it's ok if run under simulator.

Initially, I wanted to compare two doubles with (Math.Abs(arg1 - arg2) < double.Epsilon), but get a different result than I expected.

Consider the following example:

const double zero = 0;
Console.WriteLine(zero < double.Epsilon);
Console.WriteLine(Math.Abs(0) < double.Epsilon);

It's output is:


I expected both lines will be "True".
Same problem with float.

I wasn't able to google an explanation or a workaround to this, so I'm filing a bug.

Mono 2.10.9 (package 210090011)
Monotouch is 6.0.6
iOS 6.0 (10A403)
Comment 1 Zoltan Varga 2012-12-04 19:57:27 UTC
The first case only prints true because the compiler optimizes the comparison away, this also prints false:

		public static double Zero () {
			return 0.0;

		static void foo () {
			Console.WriteLine (Zero () < double.Epsilon);

I'm not sure that this is really a bug, ARM has strange floating point semantics sometimes. Use
<= double.Epsilon instead, that works.
Comment 2 Andrew 2012-12-05 03:40:12 UTC
>>The first case only prints true because the compiler optimizes the 
>>comparison away, this also prints false:
Agree, it's a minimized example, in a real code it's Math.Abs(arg1 - arg2) < double.epsilon, where arg1 and arg2 are not constant and passed from a calling code (values are taken an from external system).

>>Use<= double.Epsilon instead, that works.
Thanks for your workaround, but I don't think it's acceptable in a long term.

The comparison with epsilon is used to compare two floats, which cannot be compared with a == operator. Generally the code looks like Math.Abs(a - b) < double.Epsilon and returns true if the subtraction result is less than a minimum possible double value that is significant. If I replace < with <=, this will mean I consider the doubles equal, even though there is a significant difference between them. That's a definitely wrong way.

I found this issue by running unit tests of old code, running on Windows for many years, and some tests are failed, but were expected to be all passed. That's why I think it a bug, because right now I have to put a #if iOS to the comparison code and use a different way to compare floats than on other systems. Perhaps the same have to be done for other platforms.

If it's some fancy ARM nuance, I guess that double.Epsilon should have other than on windows value, making abs(a - b) < epsilon work properly on Arm architecture.

Comment 3 Zoltan Varga 2012-12-05 06:10:03 UTC
See the 'Platform Notes' section here:

On ARM systems, the value of the Epsilon constant is too small to be detected, so it equates to zero. You can define an alternative epsilon value that equals 2.2250738585072014E-308 instead.


Because Epsilon defines the minimum expression of a positive value whose range is near zero, the margin of difference between two similar values must be greater than Epsilon. Typically, it is many times greater than Epsilon. Because of this, we recommend that you do not use Epsilon when comparing Double values for equality.
Comment 4 Andrew 2012-12-05 06:33:20 UTC
Ok. So I need to add ARM detection code and use a custom epsilon value.

Comment 5 Zoltan Varga 2012-12-07 16:27:41 UTC
There is no need to add ARM detection code, just define your own epsilon value.