Somenath Sinha February 2016

Why does integer division truncation not occur here?

I was going through the examples in K&R, and stumbled upon this bit of code:

celcius=5 * (fahr-32) / 9;

The author says that we can't use 5/9 since integer division truncation will lead to a value of 0.

The program however, outputs 17 as answer when fahr=0. By my calculations, (0-32)/9 should lead to -3 (due to truncation) and then -3*5 = -15, and NOT -17. Why does this happen?

Answers


gnasher729 February 2016

(0 - 32) is first multiplied by 5, giving -160. -160 / 9 = -17.

Post Status

Asked in February 2016
Viewed 3,430 times
Voted 8
Answered 1 times

Search




Leave an answer