3

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

Timo Kähkönen
  • 11,962
  • 9
  • 71
  • 112

1 Answers1

2

I ended up using completely different function to test double equality from here. The original function uses signed int64 representation of double which is not possible in Javascript without using slow and complex bitwise operations or using some BigDecimal library. It uses a combination of relative and absolute error.

var IsAlmostEqual = function(a, b)
{
  if (a == b) return true;
  var diff = Math.abs(a - b);
  if (diff < 4.94065645841247E-320) return true;
  a = Math.abs(a);
  b = Math.abs(b);
  var smallest = (b < a) ? b : a;
  return diff < smallest * 1e-12;
}

According to my tests it seems reliable. Please test in jsbin.

EDIT: I updated the code above. Now it produces the same result as with ULP technique in all 83 test cases (using maxUpls 10,000). ULP technique is 8x-20x slower than above EPSILON technique due to Javascript lack of 64 bit integers.

Timo Kähkönen
  • 11,962
  • 9
  • 71
  • 112
  • Is it possible to have two numbers whose difference is less than `Number.MIN_VALUE`? – Ry- Jan 07 '14 at 00:58
  • In http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm is said "A typical value for this backup maxAbsoluteError would be very small – FLT_MAX or less, depending on whether the platform supports subnormals." I assume that it should be FLT_MIN, not FLT_MAX. I don't think, Javascript supports subnormals, so I assume that the answer is no. – Timo Kähkönen Jan 07 '14 at 01:11
  • Please ignore the above comment of mine. The FLT_MIN is 5e-324. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE says that values smaller than this are converted to 0. I cannot say yes or no anymore. And I'm not sure if Number.MIN_VALUE is the right value for maxAbsoluteError. – Timo Kähkönen Jan 07 '14 at 01:47
  • I would say `Number.EPSILON`, but it’s not the right one to use if your exponent is negative. – Ry- Jan 07 '14 at 01:50
  • Number.EPSILON seems to be 2.220446049250313e-16, so it is more logical. Why negative exponent makes it unsuitable? – Timo Kähkönen Jan 07 '14 at 02:00
  • Because `Number.EPSILON / 10` (for example) is still a number; it’s just lower precision. – Ry- Jan 07 '14 at 02:14
  • When I use Number.EPSILON instead of Number.MIN_VALUE, and compare 0 and Number.EPSILON/10, it reports wrongly false. But if I compare 1 and 1+Number.EPSILON/10, it reports correctly true. So Number.EPSILON seems not to be suitable. Any ideas? – Timo Kähkönen Jan 07 '14 at 11:38
  • `Math.abs(0 - Number.EPSILON / 10) < Number.EPSILON` is quite true… I’m not sure how you’d get `false` there. – Ry- Jan 07 '14 at 14:48
  • I was calling a wrong function :(. It seems to work: http://jsbin.com/iNEKEJo/2/edit. I don't understand still why negative exponent makes it unsuitable? Could you test it. Add the values A and B in Javascript window. – Timo Kähkönen Jan 07 '14 at 15:14