moonnum — Python's numbers for MoonBit: arbitrary-precision integers, floats, complex numbers and fractions, read, written and formatted as Python reads, writes and formats them. It is not a decimal type and not a bignum encoding.
let n = @moonnum.parse_int("0xff_ec", base=0) // 65516, any base, any size
let z = @moonnum.parse_complex("3+4j") // (3+4j)
let f = @moonnum.parse_fraction("0.1") // 1/10, exactly
@moonnum.repr_float(0.1 + 0.2) // "0.30000000000000004"
@moonnum.format(f, ".3%") // "10.000%"@moonnum.repr_float(0.1) // "0.1" shortest string that reads back
@moonnum.float_hex(0.1) // "0x1.999999999999ap-4"
@moonnum.as_ratio(0.1) // 3602879701896397/36028797018963968
@moonnum.repr_float(0.1 + 0.2) // "0.30000000000000004"
@moonnum.format(@moonnum.parse("22/7"), ".6f") // "3.142857", exactly roundedmoon add moonbitstack/moonnumimport { "moonbitstack/moonnum" }let third = @moonnum.parse("1/3")
let total = @moonnum.parse("1") + third + @moonnum.parse("0.5") // 11/6
let judged = @moonnum.parse("1") == @moonnum.parse("1.0") // true| On | Functions |
|---|---|
| integers | floordiv, floor_mod, divmod, power (with modulus~), gcd, lcm, isqrt, factorial, comb, perm, bit_length, to_bytes, of_bytes |
| floats | round_float(d, ndigits~), round_big, floor_big, ceil_big, trunc_big, as_ratio, as_integer_ratio |
| complex | + - *, Complex::divide, Complex::pow, abs, conjugate |
| fractions | + - *, Fraction::divide, Fraction::pow, compare, floor_big, ceil_big, round, round_big, to_double, limit_denominator |
| any of them | Number::add, Number::sub, Number::mul, Number::divide, Number::pow, compare_numeric, equal_numeric |
| Function | Reads |
|---|---|
| parse_int(text, base~ = 10) | int(s, base): prefixes when base is 0, single underscores between digits, surrounding whitespace |
| parse_float(text) | float(s): inf, infinity, nan in any case, decimal and exponent forms, underscores |
| parse_complex(text) | complex(s): 1+2j, (1+2j), j, -j, 1e3j — and 3+5i |
| parse_fraction(text) | Fraction(s): 3/4, -1.25, 1e-3, 7 |
| parse(text) | a Number of whichever kind the literal spells |
| Function | Writes |
|---|---|
| repr_float(d) | the shortest string that reads back, switching to an exponent at 1e16 and 1e-5 |
| float_hex(d) / float_of_hex(s) | the exact hexadecimal form, and back |
| repr_complex(z), repr_fraction(f) | (1+2j), 2j, Fraction(3, 4) |
| format(value, spec) | the format-specification mini-language, for any kind |
[[fill]align][sign][z][#][0][width][grouping][.precision][type]@moonnum.format_int(1234567N, "015,d") // "000,001,234,567"
@moonnum.format_int(255N, "#010_x") // "0x000_00ff"
@moonnum.format_float(0.25, ".2%") // "25.00%"
@moonnum.format_float(-0.0, "z") // "0.0"
z.format(".2f") // "3.00+4.00j"pub(all) suberror Refused {
Malformed(at~ : Int, reason~ : String) // where the text stopped making sense
ZeroDivision
Overflow(reason~ : String)
Domain(reason~ : String)
}let half = @moonnum.parse_fraction("1/2")
let bad : String = half.divide(@moonnum.parse_fraction("0")).to_string() catch {
@moonnum.ZeroDivision => "no"
_ => "no"
}pub(all) suberror Refused {
Malformed(at~ : Int, reason~ : String)
ZeroDivision
Overflow(reason~ : String)
Domain(reason~ : String)
}pub(all) struct Complex {
re : Double
im : Double
}fn repr_float(d : Double) -> Stringfn round_float(d : Double, ndigits~ : Int) -> DoubleInstall
Download zipmoonnum — Python's numbers for MoonBit: arbitrary-precision integers, floats, complex numbers and fractions, read, written and formatted as Python reads, writes and formats them. It is not a decimal type and not a bignum encoding.