The FloatComparison module

Quick Notes

On this page :

Also see :


Q. Why is the FloatComparison module needed?

SDS2 Python Prompt
>>> float_num = 2.425
>>> float_num
2.424999999999998

A . When comparing two floating point numbers for equivalency, you should compare them within a particular tolerance. The FloatComparison module contains six functions that can help you make such comparisons. Each of these functions take two floating point numbers and an optional tolerance argument. If the tolerance argument is unspecified, the default is .0001.


Q. Are the EqualsAbsoluteTolerance() and feq() functions the same?

SDS2 Python Prompt
>>> from FloatComparison import *
>>> feq(1.1, 1.0999)
True
>>> feq(1.1, 1.0999) == EqualsAbsoluteTolerance(1.1, 1.0999)
True

A . Yes. Given that the arguments are the same in both functions, EqualsAbsoluteTolerance() returns exactly the same result as feq() , which stands for "floats equal." To help users identify a function as having originated from an SDS2 module and to also identify its purpose, the scripts in Help usually use the longer, CamelCase version. However, the shorter version is quicker to type and probably easier to remember, so you may prefer to use it. Each of the six functions in this module can be called with either their CamelCase name or short-version name.


FloatComparison functions :

Each of these six FloatComparison functions has a CamelCase name and a short version name, for reasons explained here .

EqualsAbsoluteTolerance(num1, num2, tol) or feq(num1, num2, tol) . The two required arguments ( num1 , num2 ) are compared as floating point numbers . True is returned if their values are equivalent within the specified or default tolerance. False is returned if their values are different. The third argument ( tol ) is the tolerance and is optional. When a tolerance is not specified, the default is .0001.

SDS2 Python Prompt
>>> num1 = 7.0001
>>> num2 = 7.0
>>> from FloatComparison import EqualsAbsoluteTolerance
>>> EqualsAbsoluteTolerance(num1, num2)
True
>>> num3 = 7.0002
>>> EqualsAbsoluteTolerance(num1, num3)
False
>>> EqualsAbsoluteTolerance(num1, num3, .0002)
True

NotEqualAbsoluteTolerance(num1, num2, tol) or fne(num1, num2, tol) returns True if the two floating point numbers , num1 and num2 , are not closer together than the specified tolerance or a tolerance of .0001. False is returned if they are the same.

SDS2 Python Prompt
>>> num3 = 7.1
>>> num4 = 7.099
>>> from FloatComparison import NotEqualsAbsoluteTolerance
>>> NotEqualsAbsoluteTolerance(num3, num4)
True
>>> NotEqualsAbsoluteTolerance(num3, num4, .001)
False

LessThanAbsoluteTolerance(num1, num2, tol) or flt(num1, num2, tol) returns True if the first floating point number , num1 , is less than the second floating point number, num2 , so long as the two numbers are not closer together than the third argument, which is the specified tolerance ( tol ). If there is no third argument, the two floating point numbers are evaluated to the default tolerance, which is .0001. False is returned if the two numbers are closer together than the tolerance, or if the first number is greater than the second.

SDS2 Python Prompt
>>> num3
7.0999999999999996
>>> num4
7.0990000000000002
>>> from FloatComparison import LessThanAbsoluteTolerance
>>> LessThanAbsoluteTolerance(num4, num3)
True
>>> LessThanAbsoluteTolerance(num4, num3, .001)
False

GreaterThanAbsoluteTolerance(num1, num2, tol) or fgt(num1, num2, tol) returns True if the first floating point number , num1 , is greater than the second floating point number, num2 , unless the two numbers are closer together than the specified tolerance ( tol ). False is returned if the two numbers are closer together than the tolerance, or if the first number is less than the second. A tolerance of .0001 is the default, which is applied when the optional third argument is not specified.

SDS2 Python Prompt
>>> num5
5.5
>>> num6
5.51
>>> from FloatComparison import GreaterThanAbsoluteTolerance
>>> GreaterThanAbsoluteTolerance(num6, num5)
True
>>> LessThanAbsoluteTolerance(num6, num5, .01)
False

LessThanEqualsAbsoluteTolerance(num1, num2, tol) or fle(num1, num2, tol) returns True if num1 <= num2 within the specified tolerance or the default tolerance of .0001, which applies when there is no third argument. Otherwise, False is returned. Following is the information that is shown for help() :

SDS2 Python Prompt
>>> from FloatComparison import *
>>> help(fle)
fle( . . .)
     C++ signature:
           fle(double, double, double) -> bool
     C++ signature:
           fle(double, double) -> bool
double signifies that function takes an argument that is a floating point number . bool signifies that the function returns either True or False.

GreaterThanEqualsAbsoluteTolerance(num1, num2, tol) or fge(num1, num2, tol) returns True if num1 >= num2 within the specified tolerance or the default tolerance of .0001. Otherwise, False is returned.

SDS2 Python Prompt
>>> help(fge)
fge( . . .)
     C++ signature:
           fle(double, double, double) -> bool
     C++ signature:
           fle(double, double) -> bool
As this help() documentation shows, the third argument can be omitted. The first and second arguments are required.