1

When I wrap 5/9 in parenthesis, the result is 0, but when I don't put in the parenthesis, the result is correct. Why is this?

#include <iostream>

int main()
{
    double temperature_farenheit = 195;
    std::cout << "Enter the temperature in Farenheit: ";
    std::cin >> temperature_farenheit;
    double temperature_celsius;
    temperature_celsius = (temperature_farenheit - 32) * (5 / 9);
    std::cout << "The temperature in Celsius is: " << temperature_celsius;
    return 0;
}

Output:

Enter the temperature in Farenheit: 47
The temperature in Celsius is: 0

#include <iostream>

int main()
{
    double temperature_farenheit = 195;
    std::cout << "Enter the temperature in Farenheit: ";
    std::cin >> temperature_farenheit;
    double temperature_celsius;
    temperature_celsius = (temperature_farenheit - 32) * 5 / 9;
    std::cout << "The temperature in Celsius is: " << temperature_celsius;
    return 0;
}

Output:

Enter the temperature in Farenheit: 47
The temperature in Celsius is: 8.33333

I did a quick example in Python just to test the result, and the output is correct, but I don't know why the result is incorrect in C++.

temperature_farenheit = 47
temperature_celsius = (temperature_farenheit-32)*(5/9)
print(temperature_celsius)

Output:

8.333333333333334

C++ is incorrect, but why?

#include <iostream>

int main()
{
    double temperature_farenheit = 47;
    double temperature_celsius = (temperature_farenheit - 32) * (5 / 9);
    std::cout << "47F is equal in Celsius to : " << temperature_celsius;
    return 0;
}

Output:

47F is equal in Celsius to : 0

I tried putting the fraction at the beginning or at the end. Mathematically both are correct, but for some reason C++ is reporting 0 as a result when I put in parenthesis.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Please, don't abuse tags - `python` tag removed – buran Mar 31 '23 at 19:06
  • 6
    `5/9==0` because integer division. Put a dot after the `5.` or `9.`. – Something Something Mar 31 '23 at 19:07
  • Sometimes you want to do integer division, one integer divided by another, with the result also an integer. In C++ you use `/` for this, no doubt in python you use something else. Don't expect C++ to be like python, C++ is it's own thing. – john Mar 31 '23 at 19:55
  • The reason for the difference without brackets is in that case left to right associativity means that the multiplication happens first so you are not dividing one integer by another, so in that case you do not get integer division. – john Mar 31 '23 at 19:58

4 Answers4

2

Integer division 5/9 == 0. Just put a dot after the 5 or the 9 to make the fraction a floating point number.

#include <iostream>

int main()
{
    double temperature_farenheit;
    std::cout << "Enter the temperature in Farenheit: ";
    std::cin >> temperature_farenheit;
    double temperature_celsius;
    temperature_celsius = (temperature_farenheit - 32) * (5. / 9);
    std::cout << "The temperature in Celsius is: " << temperature_celsius;
    return 0;
}

Produces

Enter the temperature in Farenheit: 70
The temperature in Celsius is: 22.2222

Also you do not need to initialize temperature_fahrenheit if you are going to read it from cin.

Godbolt: https://godbolt.org/z/as8fjvsGT

Something Something
  • 3,999
  • 1
  • 6
  • 21
2

In C++, division of two integers results in an integer result. If at least one of the parameters is double the result is double. So:

(47.0-32) * (5 / 9)
   15.0 *   (  0  )
         0

But:

(47.0-32) * 5 / 9
   15.0   * 5 / 9      // evaluated left-to-right without parentheses
   75.0       / 9
      8.333333

In (5/9) if you make one of the constants double, e.g. (5./9) you'll get the double answer.

In Python 3.x, operator / is always a float result and // is an integral result. The result type of // is int if both operands are type int; otherwise float, but still integral:

>>> 75 / 9
8.333333333333334
>>> 75 // 9
8
>>> 75. // 9
8.0
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

The arithmetic operations are evaluated from left to right, and for each two operands of an operation the compiler determines the common type of the operands of the operation. Due to the usual arithmetic conversions, an operand of an integer type is converted to the type double if the other operand has the type double.

From the C++17 Standard (8 Expressions):

11 Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

(11.1) — If either operand is of scoped enumeration type (10.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.

(11.2) — If either operand is of type long double, the other shall be converted to long double.

(11.3) — Otherwise, if either operand is double, the other shall be converted to double.

...

So, if you will write, for example:

temperature_celsius = (temperature_farenheit - 32) * 5 / 9;

then the compiler parses this statement like:

temperature_celsius = ( (temperature_farenheit - 32) * 5 ) / 9;

As a result, the integer constant 5 is converted to the type double because the left operand (temperature_farenheit - 32) has the type double, by the same reason because temperature_farenheit has the type double.

If you use parentheses then the expression in the parentheses (5 / 9) is evaluated first using integer arithmetic, and its result is equal to the integer 0 because the common type of the operands is int.

To force the compiler to evaluate the expression in the parentheses as an expression of the type double, you could write for example (5.0 / 9). In this case, the first operand has the type double, and according to the provided quote from the C++17 Standard the second operand 9 will be converted to the type double.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

5 and 9 are literals of type int.

When you use the parenthesis around 5 / 9:

temperature_celsius = (temperature_farenheit - 32) * (5 / 9);

You are performing integer division. 5 divided by 9 is 0.555..., but the decimal gets truncated off, leaving the result as just 0. And any value multiplied by 0 results in 0.

If you omit the parenthesis around 5 / 9:

temperature_celsius = (temperature_farenheit - 32) * 5 / 9;

Now you are performing floating-point division, because the * and / operators have the same precedence, but are evaluated from left to right, so the compiler behaves as-if you had written it like this:

temperature_celsius = ((temperature_farenheit - 32) * 5) / 9;

Thus, the multiplication is evaluated before the division. First the result of (temperature_farenheit - 32) (a double) is multiplied by 5, yielding a double result, and then that result is divided by 9, yielding a final double result that has its decimal intact.

The reason you don't have this issue in Python is because it doesn't have separate / operators for integer and floating-point arithmetic. The output of the / operator is always a floating-point. For integer division, you have to use the // operator instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770