Introduction
Python is a powerful and versatile programming language used across various domains and industries. It is commonly used for data analysis, web development, scientific computing, and more. When working with numerical data in Python, one of the critical data types is float. Float values represent real numbers and play a crucial role in mathematical operations. In this tutorial, we will take a closer look at how float is stored in Python. We will cover the internal representation of float values and explore concepts such as floating-point precision, rounding errors, and arithmetic operations on float values. By the end of this tutorial, you will have a solid understanding of how float works in Python and be able to use it effectively in your programming projects.
Table of Contents :
- Representing floats in Python
- Using float() function in Python
- Comparing floats
- Using isclose() function
Representing floats in Python
- Floating point numbers are represented in python using the
float
class. - Python uses 8 bytes to represent floating point numbers.
- We know that in Python
int
type do not use fixed number of bytes to represent integers. - But case of float Python uses fixed number of bytes to represent real numbers.
- Breaking down the 8 bytes (64 bits) of float type :
- sign of number (positive or negative ) - 1 bit
- exponent - 11 bits
- significant digits - 52 bits
- Significant digits are the all the digits of a real number except leading and trailing zeros.
- 0.36 => 2 significant digits
- 1.49 => 3 significant digits
- 0.346 => 3 significant digits
- 23. 87 => 4 significant digits
- 2.87420 => 5 significant digits
- Some floating point numbers cannot be represented by finite number of digits in binary.
- E.g.
0.1
is represented as01.0001100110011...
in binary. - This sequence continues infinitely.
- Such numbers are represented using the approximate float representations in Python.
- E.g.
Using float() function in Python
- The float() function takes an integer or a string as a parameter.
- It returns the floating point number depending on the argument passed.
- If we do not pass anything to the float() function, it returns
0.0
- Code Sample :
num_1 = float("2.5")
num_2 = float(20)
print(num_1)
print(type(num_1))
print(num_2)
print(type(num_2))
# Output
# 2.5
# <class 'float'>
# 20.0
# <class 'float'>
Comparing floats
- We have read in the previous section that Python can store some float numbers approximately.
- This causes some problems while comparing two floating point numbers.
- Code Sample : Let's consider an example
x = 1.2 + 1.2 + 1.2
y = 3.6
print(x == y)
# Output
# False
- printing the values
- Here we can see that python internally considers both these numbers different.
- Though
x
andy
are equal mathematically - If we print the numbers in the above example we see the difference
- Code Sample :
x = 1.2 + 1.2 + 1.2
y = 3.6
print(x == y)
print(x)
print(y)
# Output
# False
# 3.5999999999999996
# 3.6
- Using format method
- Internally Python is using infinite number of digits to store such numbers.
- If we print the numbers in the above example using format method we'll see that there are more digits after decimal in the above numbers than we can see in the normal print function.
- Code Sample :
x = 1.2 + 1.2 + 1.2
y = 3.6
print(format(x, '.30f'))
print(format(y, '.30f'))
# Output
# 3.599999999999999644728632119950
# 3.600000000000000088817841970013
- Infinite digits
- In the example above we have printed only 30 digits after the decimal.
- If we print more digits after the decimal we'll see that python is storing the numbers with even more digits.
- In-fact the number of digits after decimal are infinite.
- Code Sample :
x = 1.2 + 1.2 + 1.2
y = 3.6
print(format(x, '.60f'))
print(format(y, '.60f'))
# Output
# 3.599999999999999644728632119949907064437866210937500000000000
# 3.600000000000000088817841970012523233890533447265625000000000
Using isclose() function
- We can use the
isclose()
function to get a workaround of the problem of comparing floats, discussed in previous section. isclose()
function is defined in themath
module of Python.- The
isclose()
function returnsTrue
if two numbers are very close to each other. - Code Sample :
from math import isclose
x = 1.2 + 1.2 + 1.2
y = 3.6
print(isclose(x,y))
# Output
# True
Prev. Tutorial : Storing Integers
Next Tutorial : Iterators vs. Iterables