% $Id: FloatPredicates.oz,v 1.3 2007/10/23 02:14:21 leavens Exp leavens $ %% Some functions to do approximate equality of floating point numbers. %% AUTHOR: Gary T. Leavens declare %% Return true iff the difference between X and Y %% is no larger than Epsilon fun {Within Epsilon X Y} {Abs X-Y} =< Epsilon end %% Partly curried version of Within fun {WithinMaker Epsilon} fun {$ X Y} {Within Epsilon X Y} end end %% Return true iff the corresponding lists are %% equal relative to the given predicate fun {CompareLists Pred Xs Ys} case Xs#Ys of nil#nil then true [] (X|Xr)#(Y|Yr) then {Pred X Y} andthen {CompareLists Pred Xr Yr} else false end end %% Return true iff the lists are equal %% in the sense that the corresponging elements %% are equal to within Epsilon fun {WithinLists Epsilon Xs Ys} {CompareLists {WithinMaker Epsilon} Xs Ys} end %% Return true iff the ratio of X-Y to Y is within Epsilon fun {Relative Epsilon X Y} {Abs X-Y} =< Epsilon*{Abs Y} end %% Partly curried version of Relative fun {RelativeMaker Epsilon} fun {$ X Y} {Relative Epsilon X Y} end end %% Return true iff the lists are equal %% in the sense that the corresponging elements %% are relatively equal to within Epsilon fun {RelativeLists Epsilon Xs Ys} {CompareLists {RelativeMaker Epsilon} Xs Ys} end %% A useful tolerance for testing StandardTolerance = 1.0e~3 %% A convenience for testing, relative equality with a fixed Epsilon ApproxEqual = {RelativeMaker StandardTolerance}