gleam_community/maths/conversion
Conversion: A module containing various functions for converting between types and quantities.
- Misc. functions
Functions
pub fn degrees_to_radians(x: Float) -> Float
Convert a value in degrees to a value measured in radians. That is, $$1 \text{ degrees } = \frac{\pi}{180} \text{ radians }$$.
Example
import gleeunit/should
import gleam_community/maths/conversion
import gleam_community/maths/elementary
pub fn example() {
conversion.degrees_to_radians(360.)
|> should.equal(2. *. elementary.pi())
}
pub fn float_to_int(x: Float) -> Int
The function returns the integral part of a given floating point value. That is, everything after the decimal point of a given floating point value is discarded and only the integer value before the decimal point is returned.
Example
import gleeunit/should
import gleam/option
import gleam_community/maths/conversion
import gleam_community/maths/piecewise
pub fn example() {
conversion.float_to_int(12.0654)
|> should.equal(12)
// Note: Making the following function call is equivalent
// but instead of returning a value of type 'Int' a value
// of type 'Float' is returned.
piecewise.round(12.0654, option.Some(0), option.Some(piecewise.RoundToZero))
|> should.equal(Ok(12.0))
}
pub fn int_to_float(x: Int) -> Float
A function that produces a number of type Float
from an Int
.
Note: The function is equivalent to the int.to_float
function in the Gleam stdlib.
Example:
import gleeunit/should
import gleam_community/maths/conversion
pub fn example() {
conversion.int_to_float(-1)
|> should.equal(-1.0)
conversion.int_to_float(1)
|> should.equal(1.0)
}
pub fn radians_to_degrees(x: Float) -> Float
Convert a value in degrees to a value measured in radians. That is, $$1 \text{ radians } = \frac{180}{\pi} \text{ degrees }$$.
Example
import gleeunit/should
import gleam_community/maths/conversion
import gleam_community/maths/elementary
pub fn example() {
conversion.radians_to_degrees(0.0)
|> should.equal(0.0)
conversion.radians_to_degrees(2. *. elementary.pi())
|> should.equal(360.)
}