Floating-Point Numbers (float) in Python
Floating-point numbers (float) in Python represent real numbers and include a decimal point. They are used for calculations that require fractional values and are more flexible than integers for representing numbers with decimals.
Characteristics of Floating-Point Numbers
Memory Representation
In Python, floating-point numbers are represented using the IEEE 754 standard for double-precision floating-point numbers. This allows for high precision but can also introduce rounding errors.
- Precision: Double precision uses 64 bits, with 52 bits for the mantissa, 11 bits for the exponent, and 1 bit for the sign.
- Limits: Precision is finite, which can lead to rounding errors for very precise calculations.
Example
# Floating-point numbers with high precision a = 1.234567890123456789 b = 0.000000000000000001 print(a) # Displays the floating-point number with precision print(b) # Displays the very small floating-point number
Decimal and Scientific Notation
Floating-point numbers can be represented in decimal or scientific notation.
- Decimal Notation: Directly with a decimal part, e.g., 3.14
- Scientific Notation: Uses exponential notation, e.g., 1.23e4 (equivalent to 12300.0)
Examples
# Decimal notation decimal_float = 3.14159 print(decimal_float) # 3.14159 # Scientific notation scientific_float = 2.5e-3 # 0.0025 print(scientific_float) # 0.0025
Arithmetic Operations with Floats
Floats support basic arithmetic operations such as addition, subtraction, multiplication, division, and more.
Examples
x = 5.0 y = 2.0 # Addition print(x + y) # 7.0 # Subtraction print(x - y) # 3.0 # Multiplication print(x * y) # 10.0 # Division print(x / y) # 2.5 # Integer Division print(x // y) # 2.0 (as a float) # Modulo print(x % y) # 1.0
Precision and Rounding Errors
Floating-point numbers can suffer from rounding errors due to their finite precision. This can lead to unexpected results in calculations.
Example of Rounding Error
# Rounding errors result = 0.1 + 0.2 print(result) # Displays 0.30000000000000004 # Comparison print(result == 0.3) # False, due to rounding errors
Using math.isclose()
To compare floats with a certain tolerance, use the math.isclose() function.
Example
import math # Comparison with tolerance print(math.isclose(result, 0.3)) # True
Type Conversion
Floats can be converted to and from other numeric types.
- float(): Converts to a float
- int(): Converts to an integer (truncates the decimal part)
- complex(): Converts to a complex number
Examples of Conversion
# Convert integer to float int_number = 7 float_from_int = float(int_number) # 7.0 print(float_from_int) # 7.0 # Convert string to float string_number = "3.14" float_from_string = float(string_number) # 3.14 print(float_from_string) # 3.14 # Convert float to integer (truncation) float_number = 9.99 int_from_float = int(float_number) # 9 print(int_from_float) # 9
Usage of Floats in Programming
Floats are commonly used in scientific calculations, statistics, and applications requiring decimal values.
Example with Calculations
# Scientific calculations radius = 5.5 area = 3.14159 * radius ** 2 print(area) # Area of a circle with the given radius
Type Checking
It’s often useful to check the type of a variable to confirm it is a float.
Example
# Check type x = 3.14 print(type(x) == float) # True
Handling Floating-Point Issues
To address floating-point issues, such as precision and rounding errors, consider the following:
- Use specialized libraries like decimal for more precise arithmetic.
- Understand floating-point limitations to avoid unexpected results in calculations.
Example with decimal
from decimal import Decimal, getcontext # Set precision getcontext().prec = 10 # Calculations with Decimal decimal_value1 = Decimal('0.1') decimal_value2 = Decimal('0.2') result = decimal_value1 + decimal_value2 print(result) # 0.3