I have translated the experimental C# "float" version of Clipper library to javascript. In the newest sandbox version there is a function IsAlmostEqual which seems to be hard to translate. Double equality cannot be compared using == operator due to numerical stability issues, so this function is needed to handle those issues.
-9223372036854775808 - aInt
and -9223372036854775808 - bInt
are easy to calculate using e.g. BigInteger library, but BitConverter.DoubleToInt64Bits
is harder.
Any idea how to translate IsAlmostEqual
function to javascript? Or specifically how to implement BitConverter.DoubleToInt64Bits
to javascript?
private static bool IsAlmostEqual(double A, double B)
{
//http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Int64 aInt = BitConverter.DoubleToInt64Bits(A);
if (aInt < 0) aInt = unchecked(-9223372036854775808 - aInt);
Int64 bInt = BitConverter.DoubleToInt64Bits(B);
if (bInt < 0) bInt = unchecked(-9223372036854775808 - bInt);
return (Math.Abs(aInt - bInt) <= 10000000000);
}
Numerical stability and robustness:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
http://www.mpi-inf.mpg.de/~kettner/pub/nonrobust_cgta_06.pdf
http://cpc.cs.qub.ac.uk/MRSN/higham.pdf
http://www.2ality.com/2012/04/number-encoding.html