How do i also use multiplication and addition in one line?

Serial.print(F("Temperature: "));
    Serial.print(event.temperature + 32);
    Serial.println(F("°F"));

I'm trying to change the temperature to Fahrenheit and I only have the +32 and cannot multiply by 9/5 (1.8)

F = C * 9/5 + 32
F = 1.8 * 9 / 5 + 32 - order of precedence will make the calculations correct
F = 35.24

Easy math: "double C plus 30"

[edit] See post #3

Serial.print(F("Temperature: "));
    Serial.print(event.temperature * 9.0 / 5.0 + 32.0);
    Serial.println(F("°F"));

careful on integer math! (9/5 could end up being 1)

1 Like

sorry @xfpd you were right the first time - I spoke too fast and did not take into account the first operator was *

In the expression F = C * 9/5 + 32 , the multiplication and division operators have the same precedence level and are evaluated from left to right due to left-to-right associativity.

Therefore, C * 9 is computed first, then the result is divided by 5, and finally, 32 is added.

So if C is a float, then the math will be correct as C * 9 will be a float

if C is an int, then it's a challenge and you'll need indeed to throw those .0 in the math operations

1 Like

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