menu

no-floating-point-equality

Disallow exact equality comparisons between floating-point values

Floating-point arithmetic rounds intermediate results, so mathematically equivalent calculations can produce different representations. Instead, you SHOULD compare an absolute or relative error with a tolerance chosen for the value's scale and domain.

Reported

1function same(left: float64, right: float64): boolean {2    return left === right;3}

Accepted

1function same(left: float64, right: float64): boolean {2    return (left - right).abs() < 0.001;3}