Math float equivalence
Don Clugston
dac at nospam.com.au
Wed Jul 5 12:55:01 PDT 2006
Lucas Goss wrote:
> 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)
It stands for "relative floating point equality". If you have a better
name, let me know (I wrote it).
> but I would think it would take a lot more processing than to just check
> equivalence.
Check the source in std.math. It's actually a very fast function (it
only has a couple of subtractions and two predictable branches).
It's a comparison that exactly captures the precision of IEEE arithmetic.
In the Helix library on dsource is a version which is more applicable to
games, since it has an absolute difference as well.
More information about the Digitalmars-d
mailing list