Numbers
Use Integer for whole numbers and Float for finite floating-point values.
The +, - and * operators work on integers. Float arithmetic uses checked
library calls that return Result. Conversions between the two types are explicit.
An Integer is an arbitrary-precision whole number; a Float is a finite
binary64 value. Conversions between them are explicit. Checked arithmetic
operations return Result; match or propagate it to handle failure.
Arithmetic
Section titled “Arithmetic”+, - and * are Integer operators. Division, remainder and modulo
use checked library calls that return Result:
mod numbers
alias gale_std.floatalias gale_std.integeralias gale_std.kernelalias gale_std.number
pub fn add(left: Integer, right: Integer) -> Integer { left + right}
pub fn half(value: Float) -> Result<Float, number.ArithmeticError> { float.divide(value, 2.0)}
pub fn from_int(value: Integer) -> Result<Float, number.ArithmeticError> { integer.to_float(value)}Float arithmetic is checked as well. Rather than new operators,
gale_std.float provides add, subtract, multiply and divide, each
returning Result<Float, number.ArithmeticError>. The numeric modules
share this error type.
Division returns both answers
Section titled “Division returns both answers”kernel.div truncates toward zero and kernel.rem gives the matching
remainder; a zero divisor is an Error, never an exception. Compose them
with with:
mod numbers
alias gale_std.integeralias gale_std.kernelalias gale_std.number
@doc "Integer division and remainder are checked calls, not / and % operators."pub fn divide_with_remainder(dividend: Integer, divisor: Integer) -> Result<{Integer, Integer}, number.ArithmeticError> { with { quotient <- kernel.div(dividend, divisor) remainder <- kernel.rem(dividend, divisor) {quotient, remainder} }}gale_std.integer.mod supplies the divisor-signed modulo (the remainder’s
sign follows the divisor, as in Elixir’s Integer.mod/2).
Mixing the two numeric types is a compile-time error. Convert explicitly:
alias gale_std.integeralias gale_std.floatalias gale_std.number
pub fn add_fraction(value: Integer) -> Result<Float, number.ArithmeticError> { with { converted <- integer.to_float(value) total <- float.add(converted, 1.0) total }}Literals, equality and rounding
Section titled “Literals, equality and rounding”Integers are arbitrary precision. Hexadecimal 0xFF, octal 0o755 and
binary 0b1010 are ordinary Integer spellings, and underscores group digits:
1_000_000. Floats require a decimal point in scientific notation (1.0e3,
not 1e3). They are finite; there are no Infinity or NaN literals.
== is native strict equality (=== in Elixir). On the supported OTP
semantics, 0.0 == -0.0 is false while both numeric orderings hold. Mixed
numeric equality, ordering and arithmetic all require explicit conversion.
Kernel round, floor, ceil and trunc convert a Float to an Integer;
integer.to_float goes the other way.
Native integer ranges
Section titled “Native integer ranges”Ranges use Elixir’s inclusive notation. Endpoints and steps must be Integer;
the result has the built-in type Range and is a native %Range{}
on the BEAM:
mod numbers
pub fn odds() -> Range { 1..9//2 }pub fn countdown() -> Range { 3..1//-1 }pub fn full_slice() -> Range { .. }1..3 uses step 1; 3..1 uses step -1. Prefer explicit negative steps for
descending ranges. A step pointing away from the last endpoint creates an empty
range. Bare .. means 0..-1//1, the full-slice range, which is empty as a
collection. Arithmetic binds inside endpoints and steps.
Read the ordinary struct fields value.first, value.last, and value.step,
and test membership with item in value or item not in value. No stdlib
module or alias is needed. Guards accept range syntax with guard-safe endpoints
and a literal nonzero step, for example n when n in 1..9//2.
Literal zero steps are compile errors. Dynamic zero steps raise Elixir’s
ArgumentError; verification requires a nonzero step. The checked
gale_std.list.range(first, last, step) function materializes a list and returns
Error(:invalid_step) for zero.