///|
test "special values" {
// Special values
inspect(@double.infinity, content="Infinity")
inspect(@double.neg_infinity, content="-Infinity")
inspect(@double.not_a_number, content="NaN")
// Limits
inspect(@double.max_value, content="1.7976931348623157e+308")
inspect(@double.min_value, content="-1.7976931348623157e+308")
inspect(@double.min_positive, content="2.2250738585072014e-308")
}///|
test "basic operations" {
// Absolute value
inspect(Double::abs(-3.14), content="3.14")
// Rounding functions
inspect(Double::floor(3.7), content="3")
inspect(Double::ceil(3.2), content="4")
inspect(Double::round(3.5), content="4")
inspect(Double::trunc(3.7), content="3")
// Sign
inspect(Double::signum(-3.14), content="-1")
inspect(Double::signum(2.0), content="1")
// Type conversion
inspect(Double::from_int(42), content="42")
}///|
test "special value testing" {
// Testing for special values
inspect(@double.not_a_number.is_nan(), content="true")
inspect(@double.infinity.is_inf(), content="true")
inspect(@double.infinity.is_pos_inf(), content="true")
inspect(@double.neg_infinity.is_neg_inf(), content="true")
// Approximate equality
let relative_tolerance = 1.e-9
inspect(Double::is_close(0.1 + 0.2, 0.3, relative_tolerance~), content="true")
}///|
test "binary representation" {
let num = 1.0
// Convert to big-endian and little-endian bytes
// Different byte orders should produce different results
let buffer = Buffer()
buffer.write_double_be(num)
inspect(
buffer.to_bytes(),
content=(
#|b"?\xf0\x00\x00\x00\x00\x00\x00"
),
)
buffer.reset()
buffer.write_double_le(num)
inspect(
buffer.to_bytes(),
content=(
#|b"\x00\x00\x00\x00\x00\x00\xf0?"
),
)
}Double is a built-in type. The documentation may not be complete here. You can find all methods and implementations in the core/builtin and core/double package.
#deprecated("Use `@double.infinity` and `@double.neg_infinity` instead")
fn Double::inf(sign : Int) -> Double#deprecated("Use `@double.min_positive` instead")
fn Double::min_normal() -> Doubletest {
inspect(@double.min_positive, content="2.2250738585072014e-308")
}#deprecated("Use `@double.not_a_number` instead")
fn Double::nan() -> Doubletest {
let nan = @double.not_a_number
inspect(nan.is_nan(), content="true")
}fn ceil(d : Double) -> Doubletest {
inspect(3.7.ceil(), content="4")
inspect((-3.7).ceil(), content="-3")
inspect(42.0.ceil(), content="42")
}fn floor(d : Double) -> Doubletest {
inspect(3.7.floor(), content="3")
inspect((-3.7).floor(), content="-4")
inspect(0.0.floor(), content="0")
}#deprecated("Use `Double::from_int` instead")
fn from_int(i : Int) -> Double#deprecated("Use `Double::is_close` instead")
fn is_close(d : Double, other : Double, relative_tolerance? : Double, absolute_tolerance? : Double) -> Bool#deprecated("Use `@math.pow` instead")
fn pow(m : Double, n : Double) -> Doublefn round(d : Double) -> Doubletest {
inspect(3.7.round(), content="4")
inspect(3.2.round(), content="3")
inspect(3.5.round(), content="4")
inspect((-3.5).round(), content="-3")
}fn trunc(d : Double) -> Doubletest {
inspect(3.7.trunc(), content="3")
inspect((-3.7).trunc(), content="-3")
inspect(0.0.trunc(), content="0")
}Install
Installed by default