Skip to content

Commit

Permalink
Added skip for NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Feb 10, 2024
1 parent a254cee commit 704ba5c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/utils/bignumber/nearlyEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export function nearlyEqual (a, b, relTol = 1e-09, absTol = 0) {
throw new Error('Absolute tolerance must be at least 0')
}

if (a.isNaN() || b.isNaN()) {
return false
}

if (!a.isFinite() || !b.isFinite()) {
return a.eq(b)
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,15 +617,23 @@ export function nearlyEqual (a, b, relTol = 1e-09, absTol = 0) {
if (relTol <= 0) {
throw new Error('Relative tolerance must be greater than 0')
}

if (absTol < 0) {
throw new Error('Absolute tolerance must be at least 0')
}

if (isNaN(a) || isNaN(b)) {
return false
}

if (!isFinite(a) || !isFinite(b)) {
return a === b
}

if (a === b) {
return true
}

// abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
return Math.abs(a - b) <= Math.max(relTol * Math.max(Math.abs(a), Math.abs(b)), absTol)
}
Expand Down

0 comments on commit 704ba5c

Please sign in to comment.