Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  A: <input id="A" ><br>
  B: <input id="B" ><br>
  C: <input id="C" ><br>
  <br>
  C = IsAlmostEqual(A,B)
</body>
</html>
 
var IsAlmostEqual = function(a, b)
{
  var diff = Math.abs(a - b);
  if (a == b) return true;
  if (diff < 4.94065645841247E-320) return true;
  a = Math.abs(a);
  b = Math.abs(b);
  var smallest = (b < a) ? b : a;
  return diff <= smallest * 1.6777e-12;
}
var A = 1;
var B = A + 1.6777e-12 / 2;
var C = IsAlmostEqual(A,B);
document.getElementById("A").value = A;
document.getElementById("B").value = B;
document.getElementById("C").value = C;
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers