Python Float Arithmetic

What is the output of this code snippet?

 

x = 5 * 3.8 - 1
print(x)

 

This puzzle has only one challenge. But this challenge is so hard that only 7% of all Finxters can overcome it: floating point operators.

I guess that most Finxters think that we are only looking for the result of the computation here. But the purpose of solving Python puzzles is to understand code in a precise and deep manner. Deep understanding tells you that the float 3.80 causes the interpreter to perform floating point arithmetic. Thus, the result is not an integer, i.e., 18, but a float, i.e., 18.0.

These kinds of mistakes seem to be negligible but they can have important effects on your code base. For example, I spent a whole day debugging my code only to find out that I assumed float division but the interpreter performed integer division. So if you did this puzzle wrong, be grateful for the lesson and go on.


Are you a master coder?
Test your skills now!

Related Video

Solution

18.0

 

Leave a Comment

Your email address will not be published. Required fields are marked *