I am new to programming. Why are these codes output have different values? I already search and cannot find the related explain or may be I cannot understand their meanings.
Code = Result
print(1 + 2) = 3
print(1 + 2.2) = 3.1
print(1.1 + 2.2) = 3.3000000000000003
Hmmm that’s odd. I can not replicate the second result as mine calculates correctly as a float.
The third result is really odd. I know you get odd results with integer division but I’ve not seen this before with addition. Following thread for insight. Perhaps @zedshaw can enlighten us…
return a + b
Execution completed in 5ms.
So that’s correct but the final one is probably surprising. Honestly that’s just because computer suck at floating point math by default. If you want very accurate floating point then you use: