Oct-28-2018, 06:30 PM
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.
That is not the case!
The above code was executed with Python 3.5.2 on a 64-bit system.
Best regards
AFoeee
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 # FalseIf 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.
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) # FalseIf 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 # FalseWhat am I doing wrong?
The above code was executed with Python 3.5.2 on a 64-bit system.
Best regards
AFoeee