C # double precision problem

Imagine a - b <c (a, b, c - C # double). Guaranteed that a <b + c ?

Thanks!

EDIT
Say that arithmetic overflow does not occur, unlike the following example:

double a = 1L << 53; double b = 1; double c = a; Console.WriteLine(a - b < c); // Prints True Console.WriteLine(a < b + c); // Prints False 

Imagine that Math.Abs ​​(a) 1.0 && Math.Abs ​​(b) 1.0 && Math.Abs ​​(c) 1.0

+7
double c # precision
source share
3 answers

Not. Suppose a = c, a very large number, and b is a very small number. It is possible that a - b has a representation less than a , but a + b so close to a (and more) that it is still most accurately represented as a .

Here is an example:

 double a = 1L << 53; double b = 1; double c = a; Console.WriteLine(a - b < c); // Prints True Console.WriteLine(a < b + c); // Prints False 

EDIT:

Here is another example that matches your edited question:

 double a = 1.0; double b = 1.0 / (1L << 53); double c = a; Console.WriteLine(a - b < c); // Prints True Console.WriteLine(a < b + c); // Prints False 

In other words, when we subtract a very small number from 1, we get a result less than 1. When we add the same number to 1, we simply get 1 back due to double precision restrictions.

+14
source share

not always:

  double a = double.MaxValue; double b = double.MaxValue; double c = 0.1; Console.WriteLine(a - b < c); // True Console.WriteLine(a < b + c); // False 
+6
source share

This link talks about arithmetic floating point properties and can be very interesting:

FLOATING-POINT FILES

In particular, the search for Relationship Properties

+2
source share

All Articles