thi-ng / umbrella

⛱ Broadly scoped ecosystem & mono-repository of 199 TypeScript projects (and ~180 examples) for general purpose, functional, data driven development
https://thi.ng
Apache License 2.0
3.41k stars 151 forks source link

[color] ? [binary] Comparison with -0 using the "===" operator will also match 0 [equals-negative-zero] #450

Closed bit-app-3000 closed 9 months ago

bit-app-3000 commented 9 months ago
import { colorFromRange, css } from '@thi.ng/color'
export const color = () => css(colorFromRange('neutral'))

[DEBUG] Comparison with -0 using the "===" operator will also match 0 [equals-negative-zero]

../../node_modules/@thi.ng/binary/float.js:22:12:
  22 │   if (x === -0)
     ╵             ~~

Floating-point equality is defined such that 0 and -0 are equal, so "x === -0" returns true for both 0 and -0. You need to use "Object.is(x, -0)" instead to test for -0.

postspectacular commented 9 months ago

Thank you @bit-app-3000 - I'm aware of this, but this warning can be ignored in this case, since the full line here reads:

if (x === -0) x = 0;

So regardless if 0 or -0, I want both cases to be 0.

In fact, this line could/should be written as an early bail out:

if (x === -0) return 0;
bit-app-3000 commented 9 months ago

maybe it will be more convenient?

if ( !Math.abs(x) ) return  0;