README

#double

This package provides comprehensive support for double-precision floating-point arithmetic, including basic operations, trigonometric functions, exponential and logarithmic functions, as well as utility functions for handling special values.

#Constants and Special Values

The package provides several important constants and special floating-point values:

///|
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")
}

#Basic Operations

Basic mathematical operations and rounding functions:

///|
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")
}

#Special Value Testing

Functions for testing special floating-point values and comparing numbers:

///|
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")
}

#Binary Representation

To convert doubles to their binary representation, use @buffer instead:

///|
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?"
),
)
}

Note: Most methods can be called either as a method (d.to_be_bytes()) or as a package function (@double.to_be_bytes(d)).

Double

Note

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.

#
Double::inf

#deprecated("Use `@double.infinity` and `@double.neg_infinity` instead")
fn Double::inf(sign : Int) -> Double

Returns positive infinity if sign >= 0, negative infinity if sign < 0.

#
Double::min_normal

#deprecated("Use `@double.min_positive` instead")
fn Double::min_normal() -> Double

Returns the smallest positive normal value of a double-precision floating-point number.

Returns a Double value that represents the smallest positive normal number that can be represented by a double-precision floating-point number (approximately 2.2250738585072014e-308).

Example:

test {
inspect(@double.min_positive, content="2.2250738585072014e-308")
}

#
Double::nan

#deprecated("Use `@double.not_a_number` instead")
fn Double::nan() -> Double

[DEPRECATED] Returns a "not-a-number" (NaN) value.

This function is deprecated. Use @double.not_a_number instead.

Returns a double-precision floating-point NaN value.

Example:

test {
let nan = @double.not_a_number
inspect(nan.is_nan(), content="true")
}

#
abs

#deprecated("Use `x.abs()` instead")
fn abs(x : Double) -> Double

Return absolute value.

#
ceil

fn ceil(d : Double) -> Double

Returns the smallest integer greater than or equal to the given number.

Parameters:

  • self : The floating point number to find the ceiling of.

Returns the ceiling value of the input number.

Example:

test {
inspect(3.7.ceil(), content="4")
inspect((-3.7).ceil(), content="-3")
inspect(42.0.ceil(), content="42")
}

#
floor

fn floor(d : Double) -> Double

Returns the largest integer less than or equal to the given number.

Parameters:

  • number : A floating-point number to be rounded down.

Returns a double-precision floating-point number representing the largest integer less than or equal to the input.

Example:

test {
inspect(3.7.floor(), content="3")
inspect((-3.7).floor(), content="-4")
inspect(0.0.floor(), content="0")
}

#
from_int

#deprecated("Use `Double::from_int` instead")
fn from_int(i : Int) -> Double

Create from int.

#
infinity

let infinity : Double

Numeric constant infinity.

#
is_close

#deprecated("Use `Double::is_close` instead")
fn is_close(d : Double, other : Double, relative_tolerance? : Double, absolute_tolerance? : Double) -> Bool

Return whether the value close.

#
max_value

let max_value : Double

Max value constant for this type.

#
min_positive

let min_positive : Double

Numeric constant min_positive.

#
min_value

let min_value : Double

Min value constant for this type.

#
neg_infinity

let neg_infinity : Double

Numeric constant neg_infinity.

#
not_a_number

let not_a_number : Double

Numeric constant not_a_number.

#
pow

#deprecated("Use `@math.pow` instead")
fn pow(m : Double, n : Double) -> Double

Function pow.

#
round

fn round(d : Double) -> Double

Rounds a floating-point number to the nearest integer using "round half up" rule. In this rule, when a number is halfway between two integers (like 3.5), it is rounded up to the next integer.

Parameters:

  • value : The floating-point number to be rounded.

Returns the rounded value as a double-precision floating-point number.

Example:

test {
inspect(3.7.round(), content="4")
inspect(3.2.round(), content="3")
inspect(3.5.round(), content="4")
inspect((-3.5).round(), content="-3")
}

#
trunc

fn trunc(d : Double) -> Double

Returns an integer value by discarding the decimal part of the floating-point number (truncation toward zero).

Parameters:

  • self : The floating-point number to be truncated.

Returns a floating-point number representing the integer part of the input.

Example:

test {
inspect(3.7.trunc(), content="3")
inspect((-3.7).trunc(), content="-3")
inspect(0.0.trunc(), content="0")
}