Sub topic 5
Integers and floats
This chapter assumes you can run Python code and use print(). It also assumes you know that a name refers to an object, as covered earlier in the tutorial. Here you will learn how the two main number types behave, so you can pick the right one and recognize their common failures.
Two number types
An int is a whole number with no fractional part. A float is a number stored in a fixed-size binary format that can hold fractions. A literal with a decimal point or an exponent is always a float. That includes 1e3, which is 1000.0, not 1000.
import sys
n = 2 ** 100
print(n)
print(type(n))
print(1_000_000)
print(0xFF, 0b1010, 0o17)
print(type(1e3), 1e3)
print(sys.float_info.max)
print(sys.float_info.epsilon)The code prints these values:
1267650600228229401496703205376is2 ** 100.<class 'int'>is its type.1000000comes from the literal1_000_000.255 10 15come from the hexadecimal, binary, and octal literals.<class 'float'> 1000.0shows that1e3is a float.1.7976931348623157e+308is the largest float.2.220446049250313e-16is the float epsilon, the gap between 1.0 and the next float above it.
Underscores in a literal are ignored. Use them to make long numbers readable.
How each type is stored
Python integers have no fixed size. They grow as needed, limited only by memory, so integer arithmetic never overflows. A float is a 64-bit IEEE 754 double. It gives about 15 to 17 significant decimal digits, and its range tops out near 1.8e308.
This difference matters at the edges. An integer can be exact at any size, but a float can only be exact up to a limit.
big = 10 ** 400
try:
float(big)
except OverflowError as e:
print("OverflowError:", e)
print(1e308 * 10)
print(float("nan") == float("nan"))
print(float(2 ** 53 + 1))The output is:
OverflowError: int too large to convert to float
inf
False
9007199254740992.0Four things to note:
- Converting an int that is too large raises
OverflowError. - Float arithmetic that overflows does not raise. It silently gives
inf. nan("not a number") is never equal to anything, including itself. Usemath.isnan()to test for it.- Above
2 ** 53, a float can no longer represent every whole number.2 ** 53 + 1becomes9007199254740992.0, so the extra 1 is lost.
Mixing the two types
When an expression mixes an int and a float, Python converts the int to a float first, so the result is a float. When both operands are int, the result stays an int for +, -, *, and ** with a non-negative exponent. Division with / is the exception and always returns a float. The next chapter covers it along with the other operators.
The flow is simple enough to state in text. Two ints give an int. Any float in the expression gives a float. A float can never turn back into an int on its own.
Common errors
- Using a float for exact whole numbers. IDs, counts, and large values belong in an
int. A float silently rounds them once they pass2 ** 53. - Expecting `10 -1
to be an int.** A negative exponent produces a float (0.1`). - Testing
x == float("nan"). This is alwaysFalse. Usemath.isnan(x). - Assuming float overflow raises an error. It gives
inf. Check for it withmath.isinf()if it matters. - Writing a leading zero.
007is aSyntaxErrorin Python 3. Write7, or0o7for octal.
Exercise
What are the type and value of 4 * 2.5, and what is the type of 2 ** 200?
Show answer
4 * 2.5 is 10.0, a float, because mixing an int with a float produces a float. 2 ** 200 is an int, because Python integers grow as large as needed.