Floating Point Numbers in Python:
In Python, floating-point numbers (or simply floats) are used to represent real numbers that have a fractional part. These numbers are typically used for calculations that require precision beyond whole numbers, such as measurements, financial calculations, scientific computations, and more.
Key Features of Floating Point Numbers in Python:
-
Definition: A floating-point number is a number that has a decimal point. It can be a positive or negative value and includes both whole numbers and fractional parts.
x = 3.14 # Positive float y = -2.71 # Negative float z = 0.0 # Float representing zero -
Creation of Floats: You can create floating-point numbers in Python by including a decimal point in the number or by using scientific notation.
x = 3.14159 # Decimal notation y = 1e3 # Scientific notation (1 * 10^3 = 1000.0) z = -5.67e-2 # Scientific notation (-5.67 * 10^-2 = -0.0567) -
Arithmetic Operations with Floats: Python supports standard arithmetic operations (addition, subtraction, multiplication, division, etc.) with floating-point numbers.
x = 5.5 y = 2.2 print(x + y) # Addition, Output: 7.7 print(x - y) # Subtraction, Output: 3.3 print(x * y) # Multiplication, Output: 12.1 print(x / y) # Division, Output: 2.5 (floating-point result) print(x // y) # Floor division, Output: 2.0 print(x % y) # Modulus (remainder), Output: 1.1 print(x ** y) # Exponentiation, Output: 118.033375 -
Precision and Limitations:
- Floating-point numbers are approximations. This means that not all decimal numbers can be precisely represented in binary format (which is how computers handle floating-point numbers). For example, some numbers like
0.1cannot be represented exactly in binary and might have small rounding errors.
a = 0.1 + 0.2 print(a) # Output: 0.30000000000000004 (small floating-point error) - Floating-point numbers are approximations. This means that not all decimal numbers can be precisely represented in binary format (which is how computers handle floating-point numbers). For example, some numbers like
-
Floating Point Representation (Scientific Notation): You can represent large or very small floating-point numbers using scientific notation (also known as exponential notation), where a number is expressed as a multiple of 10 raised to a power:
large_float = 1.23e+6 # 1.23 * 10^6 = 1230000.0 small_float = 4.56e-3 # 4.56 * 10^-3 = 0.00456 -
Type Conversion: You can convert other data types to floating-point numbers using the
float()function. This is useful when you want to convert integers or strings to floats.x = float(5) # Convert integer to float, Output: 5.0 y = float("3.14") # Convert string to float, Output: 3.14 -
Built-in Functions for Floats: Python provides several built-in functions for working with floating-point numbers:
-
round(): Rounds a float to a specified number of decimal places.x = 3.14159 print(round(x, 2)) # Output: 3.14 -
abs(): Returns the absolute value of a float.x = -3.14 print(abs(x)) # Output: 3.14 -
float(): Converts a number or a string into a floating-point number.x = float("42.5") print(x) # Output: 42.5
-
-
Floating-Point Precision in Python: The precision of floating-point numbers in Python is generally based on the IEEE 754 double-precision standard. This provides up to 15-17 significant digits of precision. Beyond this limit, Python may encounter rounding errors or representation issues.
x = 1.1234567891234567 # 16 digits print(x) # Output: 1.1234567891234567
Example:
x = 1.5
y = 2.7
# Perform some arithmetic
result = (x + y) * 3 / 2
print(result) # Output: 6.3
Conclusion:
Floating-point numbers in Python are used for representing real numbers that include decimals. They are versatile and support a wide range of operations, but they come with certain limitations in terms of precision due to their binary representation. Understanding floating-point numbers' behavior, including rounding and precision issues, is important when performing calculations that require high accuracy.