Numbers.isSafeFloat()

Syntax

function isSafeFloat(value: number): boolean

Checks if a number is a “safe” floating point number, that is whether it is unlikely to suffer from floating point inaccuracies.

Parameters
value

A number which may or may not be a floating point number.

Example

import { Numbers } from 'potence';

Numbers.isSafeFloat(0.1);  // -> false
Numbers.isSafeFloat(0.5);  // -> true

Remarks

This function tests for floating point inaccuracy by conducting an arbitrary mathematical operation on the value, inverting it, and comparing it with the original value. As a result, this function only returns a “guess”. You should not base any important conditions off the result of this function.

This function always returns false for NaN, Infinity, and -Infinity.

To compare floating point numbers, use Numbers.compare() instead. Its page also offers a more detailed explanation of floating point inaccuracies.

Contrast Numbers.isUnsafeFloat().