Skip to content

Commit

Permalink
Remove global side-effect (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanwins authored and mfogel committed Nov 24, 2018
1 parent 958de5d commit 99190ec
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/flp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON
*/

let epsilon = Number.EPSILON

// IE Polyfill
if (Number.EPSILON === undefined) Number.EPSILON = Math.pow(2, -52)
if (epsilon === undefined) epsilon = Math.pow(2, -52)

const EPSILON_SQ = Number.EPSILON * Number.EPSILON
const EPSILON_SQ = epsilon * epsilon

/* FLP comparator */
export const cmp = (a, b) => {
// check if they're both 0
if (-Number.EPSILON < a && a < Number.EPSILON) {
if (-Number.EPSILON < b && b < Number.EPSILON) {
if (-epsilon < a && a < epsilon) {
if (-epsilon < b && b < epsilon) {
return 0
}
}
Expand All @@ -37,10 +39,10 @@ export const cmpPoints = (aPt, bPt) => {

// inlined version of cmp() for performance boost
if (
a <= -Number.EPSILON ||
Number.EPSILON <= a ||
b <= -Number.EPSILON ||
Number.EPSILON <= b
a <= -epsilon ||
epsilon <= a ||
b <= -epsilon ||
epsilon <= b
) {
const diff = a - b
if (diff * diff >= EPSILON_SQ * a * b) {
Expand All @@ -53,10 +55,10 @@ export const cmpPoints = (aPt, bPt) => {

// inlined version of cmp() for performance boost
if (
a <= -Number.EPSILON ||
Number.EPSILON <= a ||
b <= -Number.EPSILON ||
Number.EPSILON <= b
a <= -epsilon ||
epsilon <= a ||
b <= -epsilon ||
epsilon <= b
) {
const diff = a - b
if (diff * diff >= EPSILON_SQ * a * b) {
Expand Down

0 comments on commit 99190ec

Please sign in to comment.