Math float equivalence
Oskar Linde
oskar.lindeREM at OVEgmail.com
Wed Jul 5 08:36:54 PDT 2006
Lucas Goss skrev:
> In many game libraries, they often have some type of float equivalence
> function to work around rounding differences, something like:
>
> float equal(float f1, float f2, epsilon=1e-6)
> {
> // check for equivalence between f1 and f2
> // where the difference between them is less than epsilon
> }
>
> How does D check for equivalence between floats, does it account for
> rounding errors? If not, is there a library function for this? I noticed
> "math.feqrel" (I still loathe the names of all these functions, I have
> no idea what feqrel is supposed to mean) which seems like it might work,
> but I would think it would take a lot more processing than to just check
> equivalence.
int mfeq(real x, real y, real precision)
in std.math seems to be what you are asking for. The bad news is that it
is private. The good news is that DMD lets you use it anyway as long as
you use the fully qualified name std.math.mfeq. ;)
int feqrel(real x, feal y) returns to what precision x and y are equal
(number of bits) which may or may not be what you want.
feqrel is not the solution to all floating point equivalence, but is a
very handy tool in many cases. What is equivalent is really application
dependent. Would you consider -1e-99 to be equivalent to 1e-99 for instance?
/Oskar
More information about the Digitalmars-d
mailing list