Python Forum
How do I calculate the smallest value that is recognized as a difference when compari
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I calculate the smallest value that is recognized as a difference when compari
#1
Hello,
I found that comparing large float values whose difference is small does not work as expected. (See my question on stackoverflow here.)
A difference seems not to be noticed until it is big enough.

import sys

m = sys.float_info.max                        # type 'float'

m == m                                        # True
m < m                                         # False
m > m                                         # False

m == m-1.0                                    # True
m < m-1.0                                     # False
m > m-1.0                                     # False

m == m-1e100                                  # True
m < m-1e100                                   # False
m > m-1e100                                   # False

m == m-1e300                                  # False
m > m-1e300                                   # True
m < m-1e300                                   # False
If I understand the answer correctly, this is not just limited to very large floats, but floats in general:
  • Only when the difference of two values is greater than a minimum value, a difference is noticed.
  • This minimum value depends on the values to be compared.
As I understand it, sys.float_info.epsilon is used to determine this minimum value. The wanted value is the product of sys.float_info.epsilon and the respective float.

import sys

m = sys.float_info.max                        # type 'float'
eps = sys.float_info.epsilon
# minimum value is 'm*eps'

m == m-(m*(eps/10))                           # True
m == m-(m*eps)                                # False
If m * eps represents the minimum value, then any value that is smaller should actually cause two floats to be considered equal.
That is not the case!

import sys

m = 1000000.0
eps = sys.float_info.epsilon
min_dif = m * eps                             # 0.000000000222044.....

m == m - min_dif                              # False
m == m - (min_dif/2)                          # False
m == m - (min_dif/10)                         # True

m == m - 0.00000000021                        # False
What am I doing wrong?

The above code was executed with Python 3.5.2 on a 64-bit system.

Best regards
AFoeee
Reply
#2
If you want to understand precisely what happens when you do these arithmetic operations, you need to learn about the machine representation of floating point numbers in python, which is the IEEE-754 format with 53 bits of precision and 11 bits exponent. I posted here a function allowing you to display the machine representation of a float.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 425 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  The term 'pip' is not recognized as the name of a cmdlet, function michaelnicol 1 626 Jul-16-2023, 11:12 PM
Last Post: deanhystad
  Using log to calculate difference EmBeck87 1 722 Apr-04-2023, 08:43 PM
Last Post: deanhystad
  Index Function not recognized in Python 3 Peter_B_23 1 1,205 Jan-08-2023, 04:52 AM
Last Post: deanhystad
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,567 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Sorting numbers from smallest to biggest Dokugan 2 2,218 Apr-14-2020, 09:24 PM
Last Post: Larz60+
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 2,518 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  smallest Cosine distance in Graph vino689 3 2,315 Jan-12-2020, 08:06 AM
Last Post: rmspacedashrf
  matplotlib isn't recognized after installation Pavel_47 5 2,814 Sep-18-2019, 07:01 PM
Last Post: snippsat
  how do i get y to be recognized in this comprehension? Skaperen 5 3,110 Aug-26-2019, 07:43 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020