Skip to content

math/lerp test with lerp(0.2, 0.4, 0.5) failed  #32

@CatsJuice

Description

@CatsJuice

Describe the bug

The lerp method may have accuracy problems when calculating with decimals, which may not matter in some scenarios (such as calculating pixels), but may be confusing in some situations.

// math.test.ts
import { lerp } from './math'

it('lerp', () => {
  expect(lerp(0.2, 0.4, 0.5)).toMatchInlineSnapshot('0.30000000000000004')
})

Reproduction

https://github.com/CatsJuice/mir-antfu-utils-math-lerp-precision

System Info

System:
    OS: macOS 14.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 67.19 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
  Browsers:
    Safari: 17.0

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

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