简体   繁体   中英

How to check strict equality of floating numbers in jest

  it('returns the median for even number of grades', () => {
    const grades = [80, 90, 85, 110, 100, 60]
    const expectedMedian = 87.50
    const median = calculateMedian(grades)
    expect(median).toBe(expectedMedian)
  })

This test passes but I don't want, 87.5 to equal with 87.50 .

in numerics I've learned, that you rather want to check, whether the difference of two floating point numbers is below a (small) treshold.

See Machine epsilon .

So use toBeLessThanOrEqual of Jest here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM