Fidel90
Fidel90

Reputation: 1838

Browsersupport of Number.EPSILON?

I'd like to use the Number.EPSILON value in my application and was wondering how this is supported across different browsers. MDN states that it is supported by Firefox and Chrome but not by IE and Safari.

I've tested it myself in the console of IE11 and Edge. Edge gives me 2.220446049250313e-16 as expected, IE11 gives undefined. I can't test Safari myself.

  1. Is there a support table that shows the support of different browser versions?

  2. Can I safely use var eps = Number.EPSILON || 2.220446049250313e-16; as fallback?

Upvotes: 4

Views: 1775

Answers (1)

TylerY86
TylerY86

Reputation: 3792

1) You can try it out yourself at browserling and punch in "https://jsfiddle.net/jz350gf1/1/" (without the quotes). If you see red, it's not supported.

The javascript code is document.body.bgColor = Number.EPSILON !== undefined ? 'lime' : 'red';.

IE9 and Safari 5.1 on Vista do not support it, for example.

There's also the compatibility table at the bottom of the MDN page for Number.EPSILON. At the current time, it claims that only Chrome, Firefox, and (maybe) Opera support it. Versions of Safari greater than 5.1 may support it. I don't have a Mac to test it out on though.

At caniuse.com, here's where the features are introduced by ES6. Looks like recent versions of Safari have full support. I'm not sure how far back it goes though. Versions between then and now were the dark ages for Safari. Opera may not support it after all? No, it does support it.

Either way, as for 2) Yes. Constants are constants are constants. The epsilon for double precision floating points are always going to be the same across all languages. Though should you want to derive the value specifically, according to this wiki page, you should be able to derive it computationally. Here's a demonstration.

function getEpsilon() {
  var e = 1.0;
  while ( ( 1.0 + 0.5 * e ) !== 1.0 )
    e *= 0.5;
  return e;
}

alert("And the epsilon is... "+getEpsilon());

So you could do var eps = Number.EPSILON || getEpsilon(); if you want to be super-pendantic about non-conforming browsers that may use something less than a double precision floating point number type, though I believe none exist (I'm open to exceptions). :)

Upvotes: 6

Related Questions