For machine-precision numbers, Mathematica uses a tolerance for comparisons, so that 1.-$MachineEpsilon==1. However, Floor does not take into account this tolerance, leading to an inconsistency:
{Floor[#], # < 1., # >= 1.} &[1. - $MachineEpsilon]
(*{0, False, True}*)
(*Should be {1,False,True} or {0,True,False}*)
For the particular piece of code this affects, I am using Compile and so can just set the RuntimeOptions to not CompareWithTolerance (which is what Floor assumes). However, I thought it worth asking if it is possible to avoid this in general, or is it necessary to refactor code that assumes this consistency?
For what it's worth, it also appears to be inconsistent with the documentation which states that "Floor[x] gives the greatest integer less than or equal to x" and the documentation gives an example of it working correctly for an arbitrary-precision number, e.g. Floor[1`100 - 10^-130]==1
Comments
Post a Comment