Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In Python, the FloatingPointError
is a built-in exception that is raised when a floating-point operation fails. However, this exception is not raised by default for typical floating-point errors, such as division by zero, overflow, or underflow, because Python follows the IEEE 754 floating-point standard, which defines how to handle such cases.
To enable the FloatingPointError
exception for specific cases, you can use the fpectl
module to control the floating-point exception mode. Unfortunately, the fpectl
module is not built or tested by default in Python, and its usage is discouraged. In practice, you would rarely encounter or need to handle the FloatingPointError
exception in Python.
Instead, for most floating-point operations, you can use exception handling techniques with other exceptions, such as ZeroDivisionError
, which is raised when you attempt to divide by zero.
Here's an example of handling a ZeroDivisionError
:
try: x = 1.0 y = 0.0 result = x / y except ZeroDivisionError as e: print(f"Error: {e}")
In this example, we attempt to divide by zero, which would raise a ZeroDivisionError
. By using a try
and except
block, we can catch the exception and handle it appropriately.
For other floating-point errors, such as overflow or underflow, you can use the math
module's isinf()
and isnan()
functions to check for infinity and NaN (Not a Number) values, respectively, and handle them as needed.
import math x = 1e300 * 1e100 # This should result in an overflow if math.isinf(x): print("Overflow occurred: x is infinite") x = math.sqrt(-1) # This should result in NaN if math.isnan(x): print("Invalid operation: x is NaN")
In this example, we perform floating-point operations that result in overflow and NaN values. We then use the math.isinf()
and math.isnan()
functions to check for these cases and handle them accordingly.
Python FloatingPointError
handling:
FloatingPointError
.try: result = 1 / 0 # Potential FloatingPointError except FloatingPointError as e: print(f'Error: {e}')
Avoiding FloatingPointError
in mathematical operations:
result = 1 / 3 if result == float('inf') or result == float('-inf'): print('Avoided FloatingPointError')
Python math
module and FloatingPointError
:
math
module.import math try: result = math.sqrt(-1) # Potential FloatingPointError except ValueError as e: print(f'Error: {e}')
FloatingPointError
in division operations in Python:
numerator = 1 denominator = 0 try: result = numerator / denominator # Potential FloatingPointError except ZeroDivisionError as e: print(f'Error: {e}')
Dealing with NaN
and Inf
in Python floating-point calculations:
NaN
and Inf
.result = float('inf') + float('-inf') if math.isinf(result): print('Handled Inf value')
Python try-except for FloatingPointError
:
try: result = math.log(0) # Potential FloatingPointError except ValueError as e: print(f'Error: {e}')
Python decimal
module and FloatingPointError
:
decimal
module for precise decimal arithmetic.from decimal import Decimal, DivisionByZero numerator = Decimal('1') denominator = Decimal('0') try: result = numerator / denominator # Potential FloatingPointError except DivisionByZero as e: print(f'Error: {e}')
Python math.isnan()
and FloatingPointError
detection:
math.isnan()
to check for NaN
values.result = math.sqrt(-1) if math.isnan(result): print('Detected NaN value')