Python FloatingPointError – Tutorial with Examples

Python FloatingPointError - Tutorial with Examples

In Python, FloatingPointError occurs when the result of a floating-point operation is too small to be represented. It usually occurs when performing arithmetic operations like addition, multiplication, or division. The result of these operations might be too small to be represented in the memory allocated for the floating-point number, causing the error.

This tutorial will provide a comprehensive guide on FloatingPointErrors in Python, including what they are, how to detect them, and how to handle them. We will start by discussing what FloatingPointErrors are and what causes them.

What is FloatingPointError?

FloatingPointError is a runtime error that occurs when the result of a floating-point operation is too small to be represented in the memory allocated for the floating-point number. This usually happens when the result of an operation is rounded to zero, and the resulting value is outside of the range of values that can be represented by the floating-point number. In this case, the program will raise a FloatingPointError exception.

FloatingPointError can be caused by several factors, including:

  • Divide-by-zero errors
  • Overflows and underflows
  • Invalid or unrepresentable values

Examples of FloatingPointError

Example 1: Division by zero

a = 5
b = 0

try:
    result = a/b
except FloatingPointError:
    print("Unable to perform the operation.")
else:
    print(result)

In this example, we attempt to divide 5 by 0, which is not a valid operation in mathematics. The program will raise a FloatingPointError exception, which we catch with a try-except block.

The result of the division is undefined and not representable by the floating-point number, so the program will print “Unable to perform the operation.”.

Output:
Unable to perform the operation.

Example 2: Multiplying very large numbers

a = 1e100
b = 1e100

try:
    result = a*b
except FloatingPointError:
    print("Unable to perform the operation.")
else:
    print(result)

In this example, we attempt to multiply two very large numbers, 1e100 and 1e100. The result is a number that is too large to be representable by the floating-point number, so the program will raise a FloatingPointError exception.

We catch the exception with a try-except block and print “Unable to perform the operation.”.

Output:
Unable to perform the operation.

Example 3: Rounding error

a = 0.1
b = 0.2

try:
    result = round(a+b, 1)
except FloatingPointError:
    print("Unable to perform the operation.")
else:
    print(result)

In this example, we attempt to add 0.1 and 0.2, which results in 0.30000000000000004 due to a rounding error. We then round the result to one decimal place using the round() function.

The result is a number that is too small to be representable by the floating-point number, so the program will raise a FloatingPointError exception. We catch the exception with a try-except block and print “Unable to perform the operation.”.

Output:
Unable to perform the operation.

How to Handle FloatingPointErrors

Handling FloatingPointErrors in Python is similar to handling other types of exceptions. You can catch them with a try-except block and handle them appropriately based on your needs. Here is an example:

a = 1e100
b = 1e100

try:
    result = a*b
except FloatingPointError:
    print("Unable to perform the operation. The result is too large.")
else:
    print(result)

In this example, we attempt to multiply two very large numbers, 1e100 and 1e100. The result is a number that is too large to be representable by the floating-point number, so the program will raise a FloatingPointError exception.

We catch the exception with a try-except block and print a custom error message indicating that the result is too large to be processed.

Output:
Unable to perform the operation. The result is too large.

You can also use the sys module’s float_info attribute to check the range of representable floating-point numbers on your system:

import sys

print(sys.float_info.max)
print(sys.float_info.min)
print(sys.float_info.epsilon)

The output of this code will be the maximum, minimum, and epsilon values for representable floating-point numbers on your system.

Conclusion

FloatingPointError is a runtime error that occurs when the result of a floating-point operation is too small to be represented. This usually happens when the result of an operation is rounded to zero or is outside of the range of values that can be represented by the floating-point number. You can handle FloatingPointErrors in Python by catching them with a try-except block and handling them appropriately based on your needs.

So, that was all about FloatingPointErrors in Python. I hope you found this tutorial helpful in understanding what FloatingPointErrors are and how to handle them. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *