-1

I'm currently wondering why the following operation 20 / (10.0 / 6) results in 12 (double) when using C++. 10.0 is a double and 20 and 6 are integers. (10.0 / 6) results in 1.66 because 6 is first turned into a double. But why is 20 / 1.66 then 12 and not 12.05?

Sample program:

#include <iostream>
using namespace std;
int main()
{
  cout << "20 / (10.0 / 6): " << (20 / (10.0 / 6)) << endl;
  return 0;
}

Sample session (gcc, cygwin, Windows 10 - 64 bit):

$ echo -e '#include <iostream>
> using namespace std;
> int main()
> {
>   cout << "20 / (10.0 / 6): " << (20 / (10.0 / 6)) << endl;
>   return 0;
> } 
> ' > test-div.cc

$ g++ -o test-div test-div.cc

$ ./test-div.exe 
20 / (10.0 / 6): 12
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
machinery
  • 5,972
  • 12
  • 67
  • 118
  • 2
    How do you *check* the result? Are you assigning to an `int` variable? – Some programmer dude Mar 15 '17 at 14:19
  • 6
    `(10.0/6)` is actually `1.6666666666666667` (on most systems). With higher precision, you can see that the result of the division is actually `11.99999999999999976` which rounds to `12.0` when stored in a `double`. – François Andrieux Mar 15 '17 at 14:22
  • 1
    Please post sample code to demonstrate your research in this. Why do you think that the result is 12? – BusyProgrammer Mar 15 '17 at 14:25
  • @machinery I made a simple sample and can confirm 12. (cygwin, gcc) The Windows 7 calculator computes 12 also. – Scheff's Cat Mar 15 '17 at 15:27
  • 1
    If we're talking about real numbers, `20/(10/6)` is approximately `20*(6/10)` which is about the same as `(20/10)*6` give or take a smidget. Sometimes you need to forget about programming. – n. m. could be an AI Mar 15 '17 at 15:30
  • @machinery Btw. I did it on paper with fractions. For me, 12 sounds not bad - it's the exact result I got. – Scheff's Cat Mar 15 '17 at 15:44
  • the promotion rule is the same regardless of double, float, int, short or char – phuclv Mar 15 '17 at 15:54
  • Possible duplicate of [What are the rules governing C++ single and double precision mixed calculations?](http://stackoverflow.com/questions/4239770/what-are-the-rules-governing-c-single-and-double-precision-mixed-calculations) – phuclv Mar 15 '17 at 15:54
  • [Implicit type conversion rules in C++ operators](http://stackoverflow.com/q/5563000/995714) – phuclv Mar 15 '17 at 15:55

1 Answers1

3

First thing is 12 is true value of expression:

                    20      20 * 6      120
20 / (10.0 / 6) = ------ = -------- = ------ = 12
                    10        10        10
                   ----
                    6

Second thing is floating point has finite precision. Double has 52 bits of mantissa which is close to 16 decimal digits.

So decimal value of 10/6 close to 1.6666666666666667. But internally it is binary number - something close to 1.6666666666666667406815349750104360282421112060547 if we could represent binary in decimal directly - what we can not.

Above equation evaluates so close to 12 that is what is being returned - even as double.

Anty
  • 1,486
  • 1
  • 9
  • 12