Functions operating on numbers
The following functions supplement the built-in operators on numbers, like +
, *
, -
, /
.
Int::toFloat : Int -> Float
Converts an Int
to a Float
.
Examples
val a = Int::toFloat 4
// = 4.0
Int::toString : Int -> String
Converts an Int
to a String
.
Examples
val a = Int::toString 4
// = "4"
Float::toInt : Float -> Int
Converts a Float
to an Int
.
The fractional part of the float is discarded.
Since integers wrap around, it is possible to create under/overflows if the integral part is below -9223372036854775808 (-2^63) or above 9223372036854775807 (2^63 - 1).
The conversion is performed using narrowing primitive conversion (https://docs.oracle.com/javase/specs/jls/se10/html/jls-5.html#jls-5.1.3).
Examples
val a = Float::toInt 4.371
// = 4
val b = Float::toInt (-6.7912)
// = -6
Math::abs : Int -> Int
Get the absolute value of an Int
.
Examples
val a = Math::abs (10 - 15)
// = 5
val b = Math::abs 8
// = 8
Math::fabs : Float -> Float
Get the absolute value of a Float
.
Examples
val a = Math::fabs (10.0 - 15.0)
// = 5.0
val b = Math::fabs 8.0
// = 8.0
Math::pow : Float -> Float -> Float
The power function: \(a^b\).
Examples
val a = Math::pow 4.0 3.0
// = 64.0
Math::sqrt : Float -> Float
The square root function: \(\sqrt{a}\).
Examples
val a = Math::sqrt 9.0
// = 3.0
Math::round : Float -> Int -> RoundingMode -> Float
Round a number to a certain scale.
The least significant digit is calculated based on the specified RoundingMode
(see Rounding mode).
Examples
val a = Math::round 123.456 2 Down
// = 123.45
Math::ceiling : Float -> Float
Round a number towards positive infinity.
Examples
val a = Math::ceiling 1.00001
// = 2.0
val b = Math::ceiling (-1.99999)
// = -1.0
Math::floor : Float -> Float
Round a number towards negative infinity.
Examples
val a = Math::floor 1.99999
// = 1.0
val b = Math::floor (-1.00001)
// = -2.0