Python's numbers for MoonBit.
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") // truelet bits = @moonnum.Fixed::bits_for_digits(50)
let two = @moonnum.Fixed::of_int(2, bits)
two.sqrt().to_string() // 1.41421356237309504880168872420969807856967187537695
two.sqrt() * two.sqrt() < two // true: each step truncates
(two * @moonnum.Fixed::pi(bits)).to_decimal(10) // 6.2831853072
@moonnum.parse_fixed("1e-3", bits) catch { _ => two } // text from outside, refused with where it stoppedlet c = @moonnum.Context::new(200)
let two = @moonnum.BigFloat::of_int(2, context=c)
two.sqrt(context=c).to_significant(60) // 1.41421356237309504880168872420969807856967187537694807317668
@moonnum.BigFloat::pi(context=c).to_significant(60)
// 3.14159265358979323846264338327950288419716939937510582097494
two.pow(@moonnum.BigFloat::of_double(0.5), context=c) // the same as sqrt@moonnum.Rounding::Nearest // ties to the even digit
@moonnum.Rounding::Floor // toward minus infinity
@moonnum.Rounding::Ceiling // toward plus infinity
@moonnum.Rounding::Down // toward zero
@moonnum.Rounding::Up // away from zero
let flinch = @moonnum.Context::new(53, rounding=@moonnum.Rounding::Up)
flinch.add(@moonnum.BigFloat::one(), @moonnum.BigFloat::parse("1e-100")) // 1.0000000000000002let c = @moonnum.Context::new(200)
let minus_one = -@moonnum.BigFloat::one(context=c)
minus_one.to_complex().sqrt(context=c) // 0 + 1j, where sqrt(-1) was nan
minus_one.to_complex().ln(context=c) // 0 + pi*j
@moonnum.BigComplex::new(two, zero).asin(context=c) // pi/2 - 1.3169...*j| 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 |
| BigFloat | + - * /, divide, abs, sqrt, exp, expm1, ln, log1p, pow, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, round, pi, e, ln2, to_complex |
| BigComplex | + - *, divide, abs, arg, conjugate, sqrt, exp, ln, pow, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, to_real |
| 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 |
| BigFloat::parse(text, context?) | a decimal at a width: inf, infinity, nan, exponents, underscores, surrounding whitespace |
| 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 |
| to_string | as many digits as the width speaks for — fifteen at 53 bits, sixty at 200 — trailing zeros dropped |
| to_decimal(places), to_significant(digits) | an exact count of places, or of significant digits |
[[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)
}impl Add for BigCompleximpl Default for BigCompleximpl Eq for BigCompleximpl Mul for BigCompleximpl Neg for BigCompleximpl Show for BigCompleximpl Sub for BigComplexfn BigComplex::divide(self : BigComplex, other : BigComplex, context? : Context) -> BigComplex raise Refusedfn BigComplex::pow(self : BigComplex, exponent : BigComplex, context? : Context) -> BigComplex raise Refusedlet c = @moonnum.Context::new(200)
(-@moonnum.BigFloat::one(context=c)).to_complex().sqrt(context=c) // 0 + 1jpub(all) struct Complex {
re : Double
im : Double
}fn Context::divide_complex(self : Context, a : BigComplex, b : BigComplex) -> BigComplex raise Refusedfn repr_float(d : Double) -> Stringfn round_float(d : Double, ndigits~ : Int) -> DoubleInstall
Download zipPython's numbers for MoonBit.