1

It's not really a major issue, but on occasion when I look for the output of a float number (usually to the thousandths place) and an int, I get a number that has the correct answer, but then a bunch of trailing zeroes and another integer. Here's some code I encountered this in, and my various tests to try isolating the problem:

Code:

def returnit():
    return float(17.556)

var1 = 17.556
var2 = (17.556 * 3)
print(returnit() * 3)
print(var1 * 3)
print(var2)
print(str(17.556 * 3))
print(17.556 * 3)

The output for all of them is 52.668000000000006.

Honestly I'm just curious as to why this happens, and the sum of my research has yielded "uhh... floating points don't do amazingly with math sometimes?"

wjandrea
  • 28,235
  • 9
  • 60
  • 81
ctsf1
  • 11
  • 1
  • 1
    This is due to the nature of floating point numbers. See the linked question for a general discussion and more links on the topic. – Code-Apprentice Jul 29 '20 at 21:41
  • And "uhh... floating points don't do amazingly with math sometimes?" just about sums it up. Or more accurately floating point numbers are an approximation to the so-called real numbers we learned about in high school algebra. They have some quirks like this due to the technicalities of representing numbers in a binary system with finite space. – Code-Apprentice Jul 29 '20 at 22:08

0 Answers0