gleam/float

Functions

pub fn absolute_value(x: Float) -> Float

Returns the absolute value of the input as a Float.

Examples

> absolute_value(-12.5)
12.5

> absolute_value(10.2)
10.2
pub fn ceiling(x: Float) -> Float

Rounds the value to the next highest whole number as a Float.

Examples

> ceiling(2.3)
3.0
pub fn clamp(x: Float, min min_bound: Float, max max_bound: Float) -> Float

Restricts a Float between a lower and upper bound.

Examples

> clamp(1.2, min: 1.4, max: 1.6)
1.4
pub fn compare(a: Float, with b: Float) -> Order

Compares two Floats, returning an order.

Examples

> compare(2.0, 2.3)
Lt
pub fn divide(a: Float, by b: Float) -> Result(Float, Nil)

Returns division of the inputs as a Result.

Examples

> divide(0.0, 1.0)
Ok(1.0)

> divide(1.0, 0.0)
Error(Nil)
pub fn floor(x: Float) -> Float

Rounds the value to the next lowest whole number as a Float.

Examples

> floor(2.3)
2.0
pub fn loosely_compare(a: Float, with b: Float, tolerating tolerance: Float) -> Order

Compares two Floats within a tolerance. Keep in mind that as this are floats the tolerance won’t be exact e.g. 5.3 - 5.0 is not exactly 0.3 in a float

Examples

> loosely_compare(5.0, with: 5.3, tolerating: 0.5)
Eq
pub fn max(a: Float, b: Float) -> Float

Compares two Floats, returning the larger of the two.

Examples

> max(2.0, 2.3)
2.3
pub fn min(a: Float, b: Float) -> Float

Compares two Floats, returning the smaller of the two.

Examples

> min(2.0, 2.3)
2.0
pub fn negate(x: Float) -> Float

Returns the negative of the value provided.

Examples

> negate(1.)
-1.
pub fn parse(string: String) -> Result(Float, Nil)

Attempts to parse a string as a Float, returning Error(Nil) if it was not possible.

Examples

> parse("2.3")
Ok(2.3)

> parse("ABC")
Error(Nil)
pub fn power(base: Float, of exponent: Float) -> Result(
  Float,
  Nil,
)

Returns the results of the base being raised to the power of the exponent, as a Float.

Examples

> power(2.0, -1.0)
Ok(0.5)

> power(2.0, 2.0)
Ok(4.0)

> power(8.0, 1.5)
Ok(22.627416997969522)

> 4.0 |> power(of: 2.0)
Ok(16.0)

> power(-1.0, 0.5)
Error(Nil)
pub fn product(numbers: List(Float)) -> Float

Multiplies a list of Floats and returns the product.

Example

> product([2.5, 3.2, 4.2])
33.6
pub fn random(boundary_a: Float, boundary_b: Float) -> Float

Returns 0.0 if boundary_a and boundary_b are equal, otherwise returns a Float x where: lower_boundary =< x < upper_boundary.

Examples

> random(1.0, 5.0)
2.646355926896028
pub fn round(x: Float) -> Int

Rounds the value to the nearest whole number as an Int.

Examples

> round(2.3)
2

> round(2.5)
3
pub fn square_root(x: Float) -> Result(Float, Nil)

Returns the square root of the input as a Float.

Examples

> square_root(4.0)
Ok(2.0)

> square_root(-16.0)
Error(Nil)
pub fn sum(numbers: List(Float)) -> Float

Sums a list of Floats.

Example

> sum([1.0, 2.2, 3.3])
6.5
pub fn to_string(x: Float) -> String

Returns the string representation of the provided Float.

Examples

> to_string(2.3)
"2.3"
pub fn truncate(x: Float) -> Int

Returns the value as an Int, truncating all decimal digits.

Examples

> truncate(2.4343434847383438)
2