C++ multiplying fractions/returning negative numbers

#include <iostream>
using namespace std;

void temperature_conversion(double fahrenheit_temperature) {
    
    double celsius_temperature{(fahrenheit_temperature-32)*(5/9)};
    cout << celsius_temperature;
}

int main() {

    double example{(33) * (5/6)}; 
    cout << example << endl; // returns 0?
    
    temperature_conversion(-1); //returns -0?
    return 0;
}

What is going on here? Why is it returning 0 and -0? I suspect it has something to do with multiplying by fractions?

5/9 is 0, therefore -33 * 0 is -0 (-33 coming from -1 - 32, if the input minus 32 was ≥ 0, then you would end up with 0 instead)

Integer division and floating point division will not produce the same results: the former will give you the quotient. 5 and 9 are integers, so the operation has to output an integer – it can’t produce 0.55555… because that isn’t an integer. You need to specify that numbers used in the division are not integers

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.