-2

I was writing a program to check and print if a number is integer or float and it usually works great. But when I try some value like 2.0000000000000001 I get the message it is an integer, why?

Code:

import math

number = float(input("Enter a number: "))

if number == round(number):
    print("Integer number")
else:
    print("Float number")
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 2
    What do you see if you print this float number? And what do you know about *round-off error*? – Scott Hunter Dec 02 '19 at 19:53
  • 1
    Related, at the very least: https://stackoverflow.com/q/588004/10077 – Fred Larson Dec 02 '19 at 19:55
  • The question is natural enough, so I don't think that the downvotes are deserved. – John Coleman Dec 02 '19 at 19:56
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – zvone Dec 02 '19 at 20:01
  • For testing for float vs. int input, wouldn't be more reliable to use: s = input("Enter a number: "); if '.' in s: print('number is float') else: print('numer is int'). – DarrylG Dec 02 '19 at 20:05
  • 1
    Darryl, what about inputs like `4e-2` or `13e4`. Both are definitely floats – SyntaxVoid Dec 02 '19 at 20:08
  • As an aside, you seem to have a fundamental misunderstanding of what a "float" is. Floating point is a way to represent *real numbers*. So, mathematical *integers* may be represented as floats as well. It is a *data type*. Both the `int` and the `float` data type can represent mathematical integers. My point is simply to keep the computing concept separate from the pure mathematics concept. – juanpa.arrivillaga Dec 02 '19 at 20:26

3 Answers3

1

In a float, a portion of the storage bits is used to specify the number and the remaining bits are used to determine the power. My guess is that the amount of bits needed to create the number 20000000000000001 is higher thant the available amount.

Anteino
  • 1,044
  • 7
  • 28
1

You aren't checking whether it is float, or integer, you are checking whether the number is equal to round(number), and due to the imprecision in the storage of floating point numbers, they WILL be equal for such small differences. If you want to check whether it is an integer or float, try this:

number = float(input("Enter a number: "))

if type(number) == int:
    print("Integer number")
elif type(number) == float:
    print("Float number")

Of course this will always print "Float number", since number is a float, doesn't matter if your input was 2 or 2.5 (2 will be stored as 2.0).

Boba0514
  • 181
  • 1
  • 3
  • 15
0

If you print out the floating number " 2.0000000000000001" you'll see that you'll get 2.0. This is because in Python, you can't truly achieve floating precision, as described in https://docs.python.org/3/tutorial/floatingpoint.html

Therefore, in your conditional statement, rounding 2.0 will get you 2, which is equal to 2.0.

It's still a floating point, because if you do type(number), you'll see that it belongs to a class of float.

alex067
  • 3,159
  • 1
  • 11
  • 17
  • 3
    It is incorrect to say that _"in Python, you can't truly achieve floating precision"_. Python gives you IEEE-754 standard floating point numbers, the exact same as other languages, like C or Java and exactly the same as what your CPU supports. And those floating point numbers have a limitation on their precision, by design. – zvone Dec 02 '19 at 19:59