7

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

asked Sep 3, 2010 at 9:16
5
  • How is this related to C# or any specific data type (or even programming)? Looks like pure math to me. Commented Sep 3, 2010 at 9:20
  • 2
    @Fredrik Mörk: No, it's all about precision. Commented Sep 3, 2010 at 9:21
  • 1
    @Fredrik Mörk: Because it has to do with the languages level of precision on non-integer arithmetic Commented Sep 3, 2010 at 9:21
  • @jgauffin and @BeRecursive: of course! :) Commented Sep 3, 2010 at 9:23
  • Actually the math is quite trivial and it has something to do with the datatype. Imagine things like 3.0 * (1.0/3.0) == 1.0 etc. Mathematically correct, in C# might not hold exactly depending on the datatype. Commented Sep 3, 2010 at 9:23

3 Answers 3

14

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.

answered Sep 3, 2010 at 9:24
Sign up to request clarification or add additional context in comments.

5 Comments

or the other way around, I also have the feeling it's like that.
It gets even weirder when a=c=double.MaxValue. Then both are false.
Great example, sure. But actually we deal with micro-numbers, not with values around double.MaxValue. Please read EDIT for more information.
@levanovd: 1 << 53 is way smaller than double.MaxValue. There's no overflow going on here.
@levanovd: I've provided another example which fits into your limits.
6

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
answered Sep 3, 2010 at 9:27

Comments

2

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

FLOATING-POINT FALLACIES

In particular, search for Properties of Relations

answered Sep 3, 2010 at 9:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.