Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# double precision problem

Imagine that a - b < c (a, b, c are C# doubles). Is it guaranteed that a < b + c?

Thanks!

EDIT
Let's say that the arithmetical overflow doesn't 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

like image 998
levanovd Avatar asked Sep 03 '10 09:09

levanovd


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

No. Suppose a = c, a very large number, and b is a very small number. It's possible that a - b has a representation less than a, but a + b is so close to a (and bigger) that it still ends up being most precisely representable as a.

Here's 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's another example, which 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 just get 1 back due to the limitations of double precision.

like image 83
Jon Skeet Avatar answered Oct 11 '22 08:10

Jon Skeet


no 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
like image 6
Tim Carter Avatar answered Oct 11 '22 06:10

Tim Carter


This link speaks about floating-point arithmetic properties, and could be very interesting:

FLOATING-POINT FALLACIES

In particular, search for Properties of Relations

like image 2
digEmAll Avatar answered Oct 11 '22 08:10

digEmAll