Skip to content

rounding errors are not catched properly #3

@jlaurens

Description

@jlaurens

Hi

almostEqual tests for absolute tolerance such that small values are always considered equal whereas big values are not.

With actual function

function almostEqual (left, right, tol) {
  return Math.abs(left - right) <= tol
}

the tests will pass when left === tol and right === 0 which may be weird, but is will not pass for big numbers.

Instead,

function almostEqual (left, right, tol) {
  return Math.abs(left - right) <= tol * (Math.abs(left)+Math.abs(right)+1) // the +1 for small
}

will pass only on rounding errors, whatever the magnitude of arguments is, either known in advance or not.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions