+ 1

int x=10, float y=3.3, int ans = x * y, ans is 32, why not 33 ?

4th Jan 2018, 10:57 AM
cc122
4 Respuestas
+ 2
real numbers in computers aren't always exact x*y can be 32.9999999996 after casting to int it removes fractional part and result can be 32
4th Jan 2018, 11:02 AM
michal
+ 1
i don't think there is any way you should use some round function to get correct result
4th Jan 2018, 4:48 PM
michal
0
thank you very much. similar thing was happening with y = 3.1,3.6 (ans was 30 and 35) but not with 3.2, 3.4.(ans was 32 and 34). Is there is a way to figure out the product without using the computer ? I mean is there is a set of rules that govern the casting (like the rules for rounding off a fractional number with more decimal digits to a fractional number with less decimal digits as in mathematics )? thank you for listening the question.
4th Jan 2018, 11:26 AM
cc122
0
okay, thank you.
5th Jan 2018, 6:26 AM
cc122