Module Relude_Float

Relude.Float contains typeclass instances and utility functions for the float type.

let eq: float => float => bool;

Float.eq indicates whether the two provided floats are exactly equal.

module Eq: BsBastet.Interface.EQ with type Eq.t = float;
let zero: float;

Float.zero is the zero constant used by Semiring. For floats, it is equal to 0.0.

let one: float;

Float.one is the one constant used by Semiring. For floats, it is equal to 1.0.

let nan: float;

Float.nan is the floating point value representing "not a number." This may be used for mathematical operations that do not result in a real number, such as 0.0 /. 0.0 and sqrt(-2).

Note that two Float.nan values are not equal to each other (and in fact, Float.nan is not equal to itself). To check for nan, use Float.isNaN.

let infinity: float;

Positive infinity

let negativeInfinity: float;

Negative infinity

let add: float => float => float;

Float.add finds the sum of two floats.

let subtract: float => float => float;

Float.subtract finds the difference between two floats.

let multiply: float => float => float;

Float.multiply finds the product of two floats.

let divide: float => float => float;

Float.divide finds the quotient of two floats.

let pow: float => float => float;

Float.pow raises the first number to the power of the second number.

Float.pow(2.0, 4.0) == 16.0;
Float.pow(3.0, 2.0) == 9.0;
Float.(pow(-2.0, (0.333333)) |> isNaN) == true;
let sqrt: float => float;

Float.sqrt determines the square root of the given float. The square root of negative numbers is nan.

let top: float;

Float.top is the constant representing the maximum float value.

let bottom: float;

Float.bottom is the minimum float value. Note that when using Bucklescript, this value is hard-coded and is not necessarily equal to Number.MIN_VALUE in JS.

let isNaN: float => bool;

Float.isNaN determines whether the provided floating point number is nan. NaN values are never equal to other numeric values, including other NaN values. This means that the intuitive myValue == nan is not sufficient for determining whether a value is NaN.

Float.isNaN(3.14) == false;
Float.isNaN(infinity) == false;
Float.isNaN(nan) == true;
let compare: float => float => BsBastet.Interface.ordering;

Compates two floats

module Ord: BsBastet.Interface.ORD with type Ord.t = float;
include { ... };
let compareWithConversion: ('b => Ord.t) => Relude_Ord.compare('b);
let compareReversed: Relude_Ord.compare(Ord.t);
module OrdReversed: { ... };
let compareAsInt: Ord.t => Ord.t => int;
let min: Ord.t => Ord.t => Ord.t;
let max: Ord.t => Ord.t => Ord.t;
let lessThan: Ord.t => Ord.t => bool;
let lt: Ord.t => Ord.t => bool;
let lessThanOrEq: Ord.t => Ord.t => bool;
let lte: Ord.t => Ord.t => bool;
let greaterThan: Ord.t => Ord.t => bool;
let gt: Ord.t => Ord.t => bool;
let greaterThanOrEq: Ord.t => Ord.t => bool;
let gte: Ord.t => Ord.t => bool;
let clamp: min:Ord.t => max:Ord.t => Ord.t => Ord.t;
let between: min:Ord.t => max:Ord.t => Ord.t => bool;
module OrdRingExtensions: (R: { ... }) => { ... };
module OrdNamed: { ... };
module type ORD_BY_F = (A: { ... }) => { ... };
module OrdBy: (A: { ... }) => { ... };
module Semiring: BsBastet.Interface.SEMIRING with type Semiring.t = float;
include { ... };
module Ring: BsBastet.Interface.RING with type Ring.t = float;
include { ... };
let (-): Ring.t => Ring.t => Ring.t;
let negate: Ring.t => Ring.t;
include { ... };
let abs: Ring.t => Ring.t;
let signum: Ring.t => Ring.t;
module EuclideanRing: BsBastet.Interface.EUCLIDEAN_RING with type EuclideanRing.t = float;
let approximatelyEqual: tolerance:float => float => float => bool;

Float.approximatelyEqual returns true if the two provided floats are within ~tolerance of one another.

If a negative ~tolerance is provided, the returned value will always be false.

Float.approximatelyEqual(~tolerance=0.01, 1.500, 1.495) == true;
Float.approximatelyEqual(~tolerance=0.01, 1.500, 1.49) == false;
let toInt: float => int;

Float.toInt converts a float to an int by dropping the fractional part. Removing the portion after the decimal is distinct from round or floor.

Floats that can't be represented as an integer (e.g. infinity, nan) will return 0. Because the range of ints is smaller (32-bit) than floats, this operation can lead to int overflows.

Float.toInt(3.1415) == 3;
Float.toInt(-2.999) == -2;
Float.toInt(Float.infinity) == 0;
Float.toInt(12345678901.0) == -539222987;
let fromInt: int => float;

Float.fromInt converts an int (e.g. 1) to its floating-point representation (1.0).

let fractionalPart: float => Ring.t;

Float.fractionalPart returns only the decimal portion as a positive floating point number, with the whole-number portion set to 0.

Note that the returned value is subject to floating point rounding errors and probably won't be exactly equal to the fractional part of the original number.

Float.fractionalPart(3.141592) ~= 0.141592;
Float.fractionalPart(-12.3456) ~= 0.3456;
let floor: float => float;

Float.floor rounds a floating point number to the nearest lower whole number.

let floorAsInt: float => int;

Float.floorAsInt rounds a floating point number to the nearest lower integer.

let ceil: float => float;

Float.ceil rounds a floating point number to the nearest higher whole number.

let ceilAsInt: float => int;

Float.ceilAsInt rounds a floating point number to the nearest higher integer.

let round: float => float;

Float.round rounds a floating point number to the nearest whole number.

let roundAsInt: float => int;

Float.roundAsInt rounds a floating point number to the nearest integer.

let toPrecision: decimals:int => float => float;

Float.toPrecision drops decimals so that the given float has no more than the requested number of decimals.

Float.toPrecision(~decimals=2, 3.141592) == 3.14;
Float.toPrecision(~decimals=4, -4.99999999999) == -4.9999;
let show: float => string;

Float.show returns the string representation of x.

Note that because there are multiple ways to represent floats in source code, the output returned by show may not match the literal float syntax provided to the function.

Float.show(3.49) == "3.49";
Float.show(7.0000) == "7";
Float.show(1.0e+03) == "1000";
let toString: float => string;

Float.toString is an alias for show.

module Show: BsBastet.Interface.SHOW with type Show.t = float;
let fromString: string => option(float);

Float.fromString attempts to parse the provided string, returning Some float if the input string is a valid floating point number or None otherwise.

Float.fromString("1000.0") == Some(1000.0);
Float.fromString("-5.25E2") == Some(-525.0);
Float.fromString("3.4.5") == None;
module Additive: { ... };
module Multiplicative: { ... };
module Subtractive: { ... };
module Divisive: { ... };
module Infix: { ... };