100

Let:

double d = 0.1;
float f = 0.1;

should the expression

(f > d)

return true or false?

Empirically, the answer is true. However, I expected it to be false.

As 0.1 cannot be perfectly represented in binary, while double has 15 to 16 decimal digits of precision, and float has only 7. So, they both are less than 0.1, while the double is more close to 0.1.

I need an exact explanation for the true.

16
  • 63
    Less precision doesn't mean lesser number.
    – zakinster
    Commented Oct 10, 2013 at 9:43
  • 8
    Why did you expect the reverse?
    – user207421
    Commented Oct 10, 2013 at 9:47
  • 11
    The only given thing is, double precision will be CLOSER to the wanted value. It could be smaller, it could be bigger.
    – SinisterMJ
    Commented Oct 10, 2013 at 11:27
  • 9
    @heshameraqi: The answers (especially Kenny's) are great in showing what happens. I believe you could benefit from reading: What Every Computer Scientist Should Know About Floating-Point Arithmetic Commented Oct 10, 2013 at 12:58
  • 8
    Suppose you are computing 1/9 in 3-digit decimal and 6-digit decimal. 0.111 < 0.111111, right? Now suppose you are computing 6/9. 0.667 > 0.666667, right? You can't have it that 6/9 in three digit decimal is 0.666 because that is not the closest 3-digit decimal to 6/9! Commented Oct 10, 2013 at 13:26

7 Answers 7

173

I'd say the answer depends on the rounding mode when converting the double to float. float has 24 binary bits of precision, and double has 53. In binary, 0.1 is:

0.1₁₀ = 0.0001100110011001100110011001100110011001100110011…₂
             ^        ^         ^   ^ 
             1       10        20  24

So if we round up at the 24th digit, we'll get

0.1₁₀ ~ 0.000110011001100110011001101

which is greater than the exact value and the more precise approximation at 53 digits.

5
  • 15
    precise answer...! Commented Oct 10, 2013 at 9:48
  • Logical Explanation. What do you think about Sven's answer though? Commented Oct 10, 2013 at 10:21
  • in other words 0.2d > 0.2f Commented Oct 10, 2013 at 12:27
  • 1
    @ratchetfreak ... or not... depends if the language implements IEEE 754, in which platform it is run... Commented Oct 10, 2013 at 12:29
  • 2
    @HeshamERAQI Both answers are correct - I guess this one makes it easier to see why this happens. For both explanations though, it is critical to take into account how the result is rounded at the cut-off point. Like other comments have said, IEEE754, the de-facto standard on FP representations, does it this way, but in theory there could be FP implementations that simply truncated instead of rounding.
    – us2012
    Commented Oct 10, 2013 at 14:18
55

The number 0.1 will be rounded to the closest floating-point representation with the given precision. This approximation might be either greater than or less than 0.1, so without looking at the actual values, you can't predict whether the single precision or double precision approximation is greater.

Here's what the double precision value gets rounded to (using a Python interpreter):

>>> "%.55f" % 0.1
'0.1000000000000000055511151231257827021181583404541015625'

And here's the single precision value:

>>> "%.55f" % numpy.float32("0.1")
'0.1000000014901161193847656250000000000000000000000000000'

So you can see that the single precision approximation is greater.

3
  • 4
    But this will depend on the implementation. I suspect that your results are for IEEE double and float, since that is by far the most commonly used today, but you might get different results on an IBM or a Unisys mainframe. Commented Oct 10, 2013 at 9:56
  • 3
    @JamesKanze: This is on a x86 machine. Python uses native floating-point operations, and the x86 implementation (more or less) conforms to IEEE-754. Commented Oct 10, 2013 at 10:03
  • That's more or less what I assumed (IEEE-745, at least): I think almost all machines for which Python is implemented use IEEE-754. My point is just that the corresponding values may be different on another architecture (like some of the mainframes, which don't use IEEE). Commented Oct 10, 2013 at 10:59
19

If you convert .1 to binary you get:

0.000110011001100110011001100110011001100110011001100...

repeating forever

Mapping to data types, you get:

float(.1)  = %.00011001100110011001101
                                     ^--- note rounding
double(.1) = %.0001100110011001100110011001100110011001100110011010

Convert that to base 10:

float(.1)  = .10000002384185791015625
double(.1) = .100000000000000088817841970012523233890533447265625

This was taken from an article written by Bruce Dawson. it can be found here:
Doubles are not floats, so don’t compare them

1
  • The title of your inked page give conclusion, so I added that within your link. Commented Oct 11, 2013 at 11:31
5

Since it can't be exactly represented, comparing 1/10 in base 2 is like comparing 1/7 in base 10.

1/7 = 0.142857142857... but comparing at different base 10 precisions (3 versus 6 decimal places) we have 0.143 > 0.142857.

5

I think Eric Lippert's comment on the question is actually the clearest explanation, so I'll repost it as an answer:

Suppose you are computing 1/9 in 3-digit decimal and 6-digit decimal. 0.111 < 0.111111, right?

Now suppose you are computing 6/9. 0.667 > 0.666667, right?

You can't have it that 6/9 in three digit decimal is 0.666 because that is not the closest 3-digit decimal to 6/9!

1
  • I Agree, this is the clearest explanation. Commented Oct 17, 2013 at 8:52
1

Just to add to the other answers talking about IEEE-754 and x86: the issue is even more complicated than they make it seem. There is not "one" representation of 0.1 in IEEE-754 - there are two. Either rounding the last digit down or up would be valid. This difference can and does actually occur, because x86 does not use 64-bits for its internal floating-point computations; it actually uses 80-bits! This is called double extended-precision.

So, even among just x86 compilers, it sometimes happen that the same number is represented two different ways, because some computes its binary representation with 64-bits, while others use 80.


In fact, it can happen even with the same compiler, even on the same machine!

#include <iostream>
#include <cmath>

void foo(double x, double y)
{
  if (std::cos(x) != std::cos(y)) {
    std::cout << "Huh?!?\n";  //← you might end up here when x == y!!
  }
}

int main()
{
  foo(1.0, 1.0);
  return 0;
}

See Why is cos(x) != cos(y) even though x == y? for more info.

3
  • A much better description of the issues you are trying to refer to can be found in the article arxiv.org/abs/cs/0701192 by David Monniaux. The only drawback of that article is that it describes the situation before Joseph S. Myers decided to fix the situation for GCC: gcc.gnu.org/ml/gcc-patches/2008-11/msg00105.html . His post is a good explanation of how a compiler with FLT_EVAL_METHOD>0 should work like. With such a compiler, the same computation produces the same results. cos(1.0) is cos(1.0). It may not be what you'd get with FLT_EVAL_METHOD=0, but it's always the same. Commented Oct 10, 2013 at 17:08
  • Some x86 compilers in some modes may use the SSE hardware even for scalar math, so intermediate precision will be 32 or 64 bits rather than the 80 bits of the x87 ALU. Commented Oct 11, 2013 at 16:22
  • 1
    I once got mad with a piece of code deemed to take the largest value computed by a function. Because of these unexpected rounding artifacts, the code was returning the first largest value instead of the last largest value (in case of ties). Took me a while to realize that x > x can hold !
    – user1196549
    Commented Oct 16, 2013 at 6:38
1

The rank of double is greater than that of float in conversions. By doing a logical comparison, f is cast to double and maybe the implementation you are using is giving inconsistent results. If you suffix f so the compiler registers it as a float, then you get 0.00 which is false in double type. Unsuffixed floating types are double.

#include <stdio.h>
#include <float.h>

int main()
{
     double d = 0.1;
     float f = 0.1f;
     printf("%f\n", (f > d));

     return 0;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.