In this tutorial, I will explain what are floating point numbers in Python. Someone asked me this question in a Python webinar and I decided to write an article on this topic. I will share my findings in this tutorial with examples and screenshots of executed example code.
Floating Point Number in Python
A floating point number is a number with a decimal point or exponent notation, indicating that a number has a fractional component. For example, numbers like 3.14, 98.6, and -0.001 are all floating point numbers in Python.
Internally, floating-point numbers are represented in computer hardware as base 2 (binary) fractions. While this allows for efficient storage and calculations, it can sometimes lead to precision limitations and rounding errors, which we’ll discuss later.
Read How to Use the repeat() Function in Python?
Create Floating Point Numbers
In Python, you can create a floating point number simply by including a decimal point in a numeric literal:
pi = 3.14159
body_temp = 98.6
You can also use scientific notation with the letter ‘e’ to represent powers of 10:
avogadro_number = 6.022e23
electron_charge = -1.602e-19
Python also provides a built-in float() function that can convert integers, strings, or other number types into floating point numbers:
float_value = float(42) # 42.0
string_float = float("3.14") # 3.14
Check out Python / vs //
Floating Point Arithmetic
Python supports standard arithmetic operations on floating point numbers, such as addition, subtraction, multiplication, and division:
california_sales_tax = 0.0725
item_price = 9.99
total_price = item_price * (1 + california_sales_tax)
print(f"Total price in California: ${total_price:.2f}")
Output:
Total price in California: $10.71
I executed the above example code and added the screenshot below.
Read Should I Learn Java or Python?
Precision and Rounding Errors
Due to the way floating point numbers are represented in binary, some decimal fractions cannot be represented exactly, leading to small rounding errors. For example:
result = 0.1 + 0.2
print(result)
Output:
0.30000000000000004
I executed the above example code and added the screenshot below.
In most cases, these small discrepancies won’t cause issues, but it’s important to be aware of them, especially when comparing floating point numbers for equality.
If you need exact decimal representation, you can use the built-in decimal module, which provides support for fast correctly-rounded decimal floating point arithmetic.
Check out Should I Learn Python or C++?
Floating Point Methods
The float type in Python provides several useful methods for working with floating point numbers:
is_integer(): ReturnsTrueif the float instance is finite with integral value, andFalseotherwise.
value = 3.0
print(value.is_integer())
value = 3.14
print(value.is_integer())
Output:
True
False
I executed the above example code and added the screenshot below.
hex(): Returns a string representation of the float in hexadecimal format.
value = 42.0
print(value.hex())
Output:
as_integer_ratio(): Returns a pair of integers whose ratio is exactly equal to the original float and with a positive denominator.
value = 3.14
print(value.as_integer_ratio())
Output:
(157, 50)
Read How to Create a Void Function in Python?
Conclusion
In this tutorial, I have explained what are floating point numbers in Python. I discussed how to create floating point numbers, floating point algorithmic, precision and rounding errors, and floating point methods.
You may also like to read:
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn