@std/math/big.ms#
Import each required symbol with import Name from "@std/math/big.ms".
BigFloat Type#
An arbitrary-precision binary floating-point number, mantissa * 2^exponent, whose mantissa holds at most precision bits.
Every operation rounds once, to nearest with ties to even, at the larger operand precision, so results are exact
up to that single rounding. Zero is unsigned and there are no infinities or NaN: out-of-domain operations return a Result.
Example: big_float_of_int(2, 100).sqrt() returns the square root of two to 100 bits as a Result, and to_fixed(30) formats it.
opaque type BigFloat {
}BigFloat.abs#
Returns the absolute value.
fn abs() BigFloatBigFloat.add#
Returns the sum, rounded once at the larger operand precision.
fn add(other BigFloat) BigFloatBigFloat.clone#
Returns an independent copy.
fn clone() BigFloatBigFloat.compare#
Returns -1, 0 or 1 as this value is less than, equal to or greater than other,
ignoring precision.
fn compare(other BigFloat) IntBigFloat.divide#
Returns the quotient, rounded once at the larger operand precision; a zero divisor returns EINVAL.
fn divide(other BigFloat) Result[BigFloat, Error]BigFloat.max#
Returns the greater value, or this value when they compare equal.
fn max(other BigFloat) BigFloatBigFloat.min#
Returns the lesser value, or this value when they compare equal.
fn min(other BigFloat) BigFloatBigFloat.multiply#
Returns the product, rounded once at the larger operand precision.
fn multiply(other BigFloat) BigFloatBigFloat.negate#
Returns the same magnitude with the opposite sign and unchanged precision; zero stays zero.
fn negate() BigFloatBigFloat.precision#
Returns the precision in bits.
fn precision() IntBigFloat.sign#
Returns -1, 0 or 1 by sign.
fn sign() IntBigFloat.sqrt#
Returns the square root rounded once; a negative operand returns EINVAL.
fn sqrt() Result[BigFloat, Error]BigFloat.subtract#
Returns the difference, rounded once at the larger operand precision.
fn subtract(other BigFloat) BigFloatBigFloat.to_fixed#
Formats in plain decimal notation with exactly digits fractional digits, rounding
ties to even. A negative digit count returns EINVAL.
fn to_fixed(digits Int) Result[String, Error]BigFloat.to_float#
Converts to the nearest Float, or null when the magnitude is beyond the Float range.
fn to_float() Float?BigFloat.to_rat#
Returns the exact value as a fraction.
fn to_rat() BigRatBigFloat.truncate#
Discards the fraction toward zero.
fn truncate() BigIntBigFloat.with_precision#
Returns this value rounded to precision bits; a precision outside 1 through
1000000 is a fault.
fn with_precision(precision Int) BigFloatBigRat Type#
An exact fraction of two arbitrary-precision integers, always in lowest terms with a positive denominator,
so two equal values compare equal with ==. Arithmetic uses named methods and never rounds.
Build values with rat, rat_of_int, rat_of_big_int, rat_of_float or parse_rat.
Example: (parse_rat("1/3")?).add(parse_rat("1/6")?).to_string() is "1/2".
opaque type BigRat {
}BigRat.abs#
Returns the absolute value.
fn abs() BigRatBigRat.add#
Returns the exact sum.
fn add(other BigRat) BigRatBigRat.ceil#
Returns the smallest integer not less than this value.
fn ceil() BigIntBigRat.clone#
Returns an independent copy.
fn clone() BigRatBigRat.compare#
Returns -1, 0 or 1 as this value is less than, equal to or greater than other.
fn compare(other BigRat) IntBigRat.denominator#
Returns the positive denominator.
fn denominator() BigIntBigRat.divide#
Returns the exact quotient; a zero divisor returns EINVAL.
fn divide(other BigRat) Result[BigRat, Error]BigRat.floor#
Returns the largest integer not greater than this value.
fn floor() BigIntBigRat.invert#
Returns 1 / self; zero returns EINVAL.
fn invert() Result[BigRat, Error]BigRat.is_integer#
Reports whether the denominator is 1, so the value is a whole number.
fn is_integer() BoolBigRat.max#
Returns the greater value, or this value when they compare equal.
fn max(other BigRat) BigRatBigRat.min#
Returns the lesser value, or this value when they compare equal.
fn min(other BigRat) BigRatBigRat.multiply#
Returns the exact product.
fn multiply(other BigRat) BigRatBigRat.negate#
Returns the same magnitude with the opposite sign; zero stays zero.
fn negate() BigRatBigRat.numerator#
Returns the numerator, which carries the sign.
fn numerator() BigIntBigRat.pow#
Returns the exact power. A negative exponent returns the reciprocal power, so zero with a negative exponent returns EINVAL. A numerator or denominator above 16777216 bits returns EOVERFLOW.
fn pow(exponent Int) Result[BigRat, Error]BigRat.round#
Rounds to the nearest integer; an exact tie rounds away from zero.
fn round() BigIntBigRat.sign#
Returns -1, 0 or 1 by sign.
fn sign() IntBigRat.subtract#
Returns the exact difference.
fn subtract(other BigRat) BigRatBigRat.to_fixed#
Formats in plain decimal notation with exactly digits fractional digits, rounding
ties to even. A negative digit count returns EINVAL; a result that rounds to zero
has no minus sign.
fn to_fixed(digits Int) Result[String, Error]BigRat.to_float#
Converts to the nearest Float, or null when the magnitude is beyond the Float range.
fn to_float() Float?BigRat.to_string#
Formats as a/b, or just a for an integer; parse_rat reads the result back.
fn to_string() StringBigRat.truncate#
Discards the fraction toward zero.
fn truncate() BigIntbig_float_of_big_int Function#
Returns value rounded to precision bits; a precision outside 1 through 1000000 is
a fault.
fn big_float_of_big_int(value BigInt, precision Int) BigFloat { ... }big_float_of_float Function#
Returns a Float rounded to precision bits (exact when precision is at least 53), or
null for NaN and infinities.
fn big_float_of_float(value Float, precision Int) BigFloat? { ... }big_float_of_int Function#
Returns value rounded to precision bits; a precision outside 1 through 1000000 is
a fault.
fn big_float_of_int(value Int, precision Int) BigFloat { ... }big_float_of_rat Function#
Returns the rational rounded once to precision bits.
fn big_float_of_rat(value BigRat, precision Int) BigFloat { ... }max_big_float_precision Constant#
The largest supported precision in bits.
max_big_float_precision Intparse_big_float Function#
Parses an integer or decimal with optional exponent (2, -0.1, 6.02e23) and
rounds once to precision bits. Malformed text returns EBADMSG and an exponent
beyond 100000 in magnitude EOVERFLOW.
fn parse_big_float(text String, precision Int) Result[BigFloat, Error] { ... }parse_rat Function#
Parses a/b, an integer, or a decimal with an optional exponent: 3/4, -12,
1.25, 6.02e23, 5e-3. A zero denominator returns EINVAL, malformed text EBADMSG
and an exponent beyond 100000 in magnitude EOVERFLOW.
fn parse_rat(text String) Result[BigRat, Error] { ... }rat Function#
Returns numerator / denominator in lowest terms; a zero denominator returns EINVAL.
fn rat(numerator BigInt, denominator BigInt) Result[BigRat, Error] { ... }rat_of_big_int Function#
Returns an integer as a fraction.
fn rat_of_big_int(value BigInt) BigRat { ... }rat_of_binary Function#
Returns the exact value mantissa * 2^exponent. A result needing more than 16777216
bits faults.
fn rat_of_binary(mantissa BigInt, exponent Int) BigRat { ... }rat_of_float Function#
Returns the exact value of a Float as a fraction: 1/2 for 0.5, and 3602879701896397/36028797018963968 for 0.1, which a Float stores inexactly. Returns null for NaN and infinities.
fn rat_of_float(value Float) BigRat? { ... }rat_of_int Function#
Returns the integer value as a fraction.
fn rat_of_int(value Int) BigRat { ... }