Skip to main content

Command Palette

Search for a command to run...

Learn the Basics of Float in Python - Part 2

Understand the Core Concepts of Float in Python

Published
4 min read
Learn the Basics of Float in Python - Part 2

Building upon the foundation laid in Part 1, we will delve deeper into the intricacies of quality testing and further explore the core concepts of floating-point numbers. In our previous lecture, we discussed various methods to test the 'equality' of two distinct floating-point values. One such approach is to round both sides of the equality expression to a specified number of significant digits, for example, using the round() function like so: round(a, 5) == round(b, 5). Alternatively, we can establish a range within which two numbers are considered equal, thereby accounting for the inherent imprecision in floating-point representations.

Two numbers 'a' and 'b' are considered equal if the absolute difference between them is less than a small positive number 'e', which accounts for the imprecision in floating-point representations. i.e |a-b| < e

let's write a program that compares the above equation.

>>> import math
>>> def is_equal(x,y,eps):
    return math.fabs(x-y) < eps
>>> #eps is range which is samll positive number
>>> #fabs is floating point absolute availalbe in math module

An approach to modify a program that compares two floating-point numbers by setting a tolerance based on a percentage of their size. This ensures that smaller numbers have smaller tolerances and larger numbers have larger tolerances. However, the text also mentions potential issues with this method, such as handling numbers very close to zero compared to those away from zero.

>>> #number close to zero
>>> x = 0.1 + 0.1 + 0.1
>>> y = 0.3
>>> print(format(x,'.20f'))
0.30000000000000004441
>>> print(format(y,'.20f'))
0.29999999999999998890
>>> difference_bt = x - y
>>> difference_bt
5.551115123125783e-17 #17th position after decimal
>>> #number away from zero
>>> a = 10000.1 + 10000.1 + 10000.1
>>> b = 30000.3
>>> print(format(a,'.20f'))
30000.30000000000291038305
>>> print(format(b,'.20f'))
30000.29999999999927240424
>>> difference_bt = a - b
>>> difference_bt
3.637978807091713e-12 #15th position after decimal

Absolute tolerance is a method to determine if two numbers are close to each other by comparing their difference with a predefined threshold. In this example, the absolute tolerance is set to 10^-15. Using this method, the program considers x and y to be close, while a and b are not. This is because the numbers may appear close in relative terms, but in absolute terms, only x and y are close enough.

A relative tolerance of 0.001% is a comparison method used to determine if two numbers are close to each other, by allowing a maximum difference between them based on a percentage (0.001%) of the largest magnitude of the two numbers.

now we can calculate the total tolerance using

$$totl = relrol * max(|x| , |y|)$$

Using the above formula the calculated total tolerance for x and y is 0.0000030000000 and for a and b is 0.300003000000. Now if we compare the number with program 1 for the a and b as well as x and y for the calculated total tolerance both the answers will be true. But still, there is a small catch.

💡
Using the relative tolerance technique doesn't work well for numbers close to zero. So using absolute and relative tolerance in isolation makes it difficult to get size fit solution. we can combine both methods calculating the relative and absolute tolerances and using the larger of the two tolerances tot= max(rel_tot * max(|x|, |y|), abs_tot)

The isclose method in the math module is a built-in function that combines both relative and absolute tolerances to compare the closeness of two numbers, providing a more accurate solution for numbers close to zero.

 >>> math.isclose(a, b, * ,rel_tol=1e-09,abs_tol=0.0)

The isclose method is a function in the math module that compares the closeness of two numbers, a and b, using relative and absolute tolerances. By default, the relative tolerance is set to 1e-09 and the absolute tolerance is set to 0.0.

💡
But if we don't specify abs_tot , then it defaults to 0 and you will face the problem we encountered in the last section when comparing two numbers close to zero.
>>> a = 10000.00000001
>>> b = 10000.00000002
>>> math.isclose(a,b)
True
>>> a = 0.000001
>>> b = 0.000002
>>> math.isclose(a,b)
False
>>> math.isclose(a,b,abs_tol=1e-5)
True
>>> x = 10000.01
>>> y = 10000.02
>>> math.isclose(x,y,rel_tol=1e-5,abs_tol=1e-5)
True
>>> x = 0.01
>>> y = 0.02
>>> math.isclose(x,y,rel_tol=1e-5,abs_tol=1e-5)
False

In conclusion, understanding the basics of floating-point numbers and their representation in Python is crucial for accurate numerical calculations. By utilizing techniques such as rounding, setting tolerances, and employing the isclose method, we can effectively compare floating-point numbers while accounting for their inherent imprecision. Combining both relative and absolute tolerances provides a more robust solution, especially for numbers close to zero.

N

insightful

S

Grate 😇

Python

Part 13 of 30

The Python Learning Series is a comprehensive and structured approach to mastering the Python programming. It is designed to cater to learners of all levels,from beginners to experienced.

Up next

Learn the Basics of Float in Python - Part 3

Understand Python Float Basics - Part 3