Numbers in computers

TME 310 - Computational Physical Modeling

Lorne Arnold, PhD, PE

University of Washington Tacoma

Why float?

Python’s integer type is int. That makes sense.

What about float for numbers with decimals?

float is short for floating point. In a float, the decimal place is not always in the same position (it floats around)

# Floats where the decimal point is between the "ones" and "tenths" place
a = 1.0
b = 0.01
# Very large or very small floats will have decimals moved and use scientific notation:
c = 11000000000000000000.0
d = 0.0000000000000000022
print(f"a: {a}; b: {b} \n")
print(f"c: {c}; d: {d}")
a: 1.0; b: 0.01 

c: 1.1e+19; d: 2.2e-18

Why decimal places float

Floating point numbers have a maximum (and minimum) size. Using scientific notation allows the decimal place to be moved and unused places to be ignored.

By default, Python’s floats use 64 bits and are limited to 15 to 17 decimal places.

So, floats have

  1. limits on precision

  2. variable limits on precision

Precision limitations

If we try to assign more precision to a float than it can hold, information will be lost. Consider the code below:

big = 1230000000.0
med = 456.0
sml = 0.0000000789
print(f"big + med: {big + med} <--- data preserved\n")
print(f"med + small: {med + sml} <--- data preserved\n")
print(f"big + small: {big + sml} <--- data lost!")
big + med: 1230000456.0 <--- data preserved

med + small: 456.0000000789 <--- data preserved

big + small: 1230000000.0 <--- data lost!

Variable precision

Floating point precision is variable (15 to 17 base-10 places) because they’re stored as binary (base-2) numbers.

This can lead to some unexpected behaviors:

x = 1.1
y = 2.2
z = x + y
print(f"z: {z}")
z: 3.3000000000000003

Compare with caution

This can be especially important when using logical statements to control scripts.

x = 1.0
while x != 0:
    x -= 0.1
    print(f"The value of x is now {x}")
print("I've exited the loop!")
The value of x is now 0.9
The value of x is now 0.8
The value of x is now 0.7000000000000001
The value of x is now 0.6000000000000001
The value of x is now 0.5000000000000001
The value of x is now 0.40000000000000013
The value of x is now 0.30000000000000016
The value of x is now 0.20000000000000015
The value of x is now 0.10000000000000014
The value of x is now 1.3877787807814457e-16
The value of x is now -0.09999999999999987
... You'll need to interrupt Python to get out of an infitinte loop!

With looser conditions

The “fix” for this example is simple: use looser conditions.

x = 1.0
while x > 0:
    x -= 0.1
    print(f"The value of x is now {x}")
print("I've exited the loop!")
The value of x is now 0.9
The value of x is now 0.8
The value of x is now 0.7000000000000001
The value of x is now 0.6000000000000001
The value of x is now 0.5000000000000001
The value of x is now 0.40000000000000013
The value of x is now 0.30000000000000016
The value of x is now 0.20000000000000015
The value of x is now 0.10000000000000014
The value of x is now 1.3877787807814457e-16
The value of x is now -0.09999999999999987
I've exited the loop!