Python Floating Point Numbers

Today we’re going to talk about something that might make you want to throw your computer out the window floating point numbers in Python. These are decimal numbers with a fractional part (like 3.14 or 0.5) and they can be tricky little buggers because they don’t always behave like regular numbers.

First, how Python handles floating point numbers. By default, Python uses IEEE-754 “double precision” floating point arithmetic. This means that each number is represented by 64 bits 1 bit for the sign (positive or negative), 8 bits for the exponent (which determines the size of the number), and 52 bits for the mantissa (the actual decimal part).

This might sound great, but there are a few things to be aware of. First, floating point numbers aren’t always exact they can only represent certain values within their range. For example, if we try to calculate 0.1 + 0.2 in Python using float arithmetic, we get an answer that is slightly different from what you might expect (it’s actually closer to 0.3 than 0.3). This is because the decimal point can only be represented with a certain amount of precision usually around 6-7 digits.

Another thing to watch out for is “rounding errors”. These occur when we perform multiple calculations using floating point numbers, and the final result isn’t exactly what we expect. For example:

# The decimal point can only be represented with a certain amount of precision, usually around 6-7 digits.

# Another thing to watch out for is "rounding errors". These occur when we perform multiple calculations using floating point numbers, and the final result isn't exactly what we expect. For example:

# The original script:
python
> (0.1 + 0.2) * 10
9.8
> ((0.1 + 0.2) * 10) 3.5
6.3
> round(6.3, 2)
6.30


# The corrected script with annotations:
python
# The decimal point can only be represented with a certain amount of precision, usually around 6-7 digits.

# Another thing to watch out for is "rounding errors". These occur when we perform multiple calculations using floating point numbers, and the final result isn't exactly what we expect. For example:

# The corrected script:
# The first calculation is incorrect because of the precision limitation of floating point numbers. 
# The correct result should be 3.0, not 3.0000000000000004.
(0.1 + 0.2) * 10 # 3.0000000000000004

# The second calculation is incorrect because of a missing operator between the two expressions.
# The correct result should be 6.3, not 6.300000000000001.
((0.1 + 0.2) * 10) 3.5 # 6.300000000000001

# The third calculation is correct because the round() function rounds the given number to the specified number of digits.
# The correct result should be 6.3, not 6.30.
round(6.3, 2) # 6.30

In this example, we first add 0.1 and 0.2 to get a result of around 0.3 (due to the decimal point precision). We then multiply that by 10 to get 3.5, but when we subtract 3.5 from our original result (which is still around 0.3), we end up with an answer that’s slightly different than what you might expect it’s actually closer to 6.299999999999998.

So, how can we avoid these issues? Well, one option is to use the decimal module in Python (which provides arbitrary-precision arithmetic). This allows us to perform calculations with much greater precision than regular floating point numbers:

# Import the decimal module to use arbitrary-precision arithmetic
from decimal import *

# Set the precision of the decimal context to 100 digits
getcontext().prec = 100

# Perform the calculation using decimal numbers to avoid precision issues
result = (Decimal('0.1') + Decimal('0.2')) * Decimal('10') * Decimal('3.5')

# Round the result to 2 decimal places
round(result, 2)

# The result should now be 3.5, avoiding any precision issues with regular floating point numbers.

In this example, we’re using the decimal module to perform our calculations with much greater precision (up to 100 digits). This allows us to avoid any issues with floating point numbers and get a more accurate result.

Remember, they can be tricky little buggers, but with the right tools and techniques, we can work around their limitations and perform calculations with much greater precision.

SICORPS