Python's numbers for MoonBit.
test "read the kinds" {
assert_eq(@moonnum.parse_int("0xff_ec", base=0).to_string(), "65516") // any base, any size
assert_eq(@moonnum.parse_complex("3+4j").to_string(), "(3+4j)")
assert_eq(@moonnum.format(@moonnum.parse("0.1"), ".3%"), "10.000%") // 1/10, exactly
assert_eq(@moonnum.repr_float(0.1 + 0.2), "0.30000000000000004")
}test "a float, read and written" {
assert_eq(@moonnum.repr_float(0.1), "0.1") // shortest string that reads back
assert_eq(@moonnum.float_hex(0.1), "0x1.999999999999ap-4")
assert_eq(@moonnum.as_ratio(0.1).to_string(), "3602879701896397/36028797018963968")
assert_eq(@moonnum.repr_float(0.1 + 0.2), "0.30000000000000004")
assert_eq(@moonnum.format(@moonnum.parse("22/7"), ".6f"), "3.142857") // exactly rounded
}moon add moonbitstack/moonnumimport { "moonbitstack/moonnum" }test "an integer, a fraction and a float in one sum" {
let third = @moonnum.parse("1/3")
let total = @moonnum.parse("1") + third + @moonnum.parse("0.5")
assert_eq(total.to_string(), "1.8333333333333333") // promoted to a float
let judged = @moonnum.parse("1") == @moonnum.parse("1.0")
assert_true(judged) // an integer and its float are equal
}test "a fixed point carries one width" {
let bits = @moonnum.Fixed::bits_for_digits(50)
let two = @moonnum.Fixed::of_int(2, bits)
assert_eq(two.sqrt().to_string(), "1.41421356237309504880168872420969807856967187537695")
assert_true(two.sqrt() * two.sqrt() < two) // each step truncates
assert_eq((two * @moonnum.Fixed::pi(bits)).to_decimal(10), "6.2831853072")
let one_milli = @moonnum.parse_fixed("1e-3", bits) catch { _ => two }
assert_eq(one_milli.to_string(), "0.001") // text from outside, read at the width
let third = @moonnum.Fixed::of_decimal("0.3", bits)
assert_eq(third.expm1().to_string(), "0.34985880757600310398374431332800733037829969735937")
assert_eq(third.log1p().to_string(), "0.26236426446749105203549598688095439720416645613143")
}test "a real carried to two hundred bits" {
let c = @moonnum.Context::new(200)
let two = @moonnum.BigFloat::of_int(2, context=c)
assert_eq(
two.sqrt(context=c).to_significant(60),
"1.41421356237309504880168872420969807856967187537694807317668",
)
assert_eq(
@moonnum.BigFloat::pi(context=c).to_significant(60),
"3.14159265358979323846264338327950288419716939937510582097494",
)
assert_eq(
two.pow(@moonnum.BigFloat::of_double(0.5), context=c).to_significant(60), // the same as sqrt
"1.41421356237309504880168872420969807856967187537694807317668",
)
}test "the five directions, and the one that rounds up" {
// The five are Nearest (ties to even), Floor, Ceiling, Down and Up.
let flinch = @moonnum.Context::new(53, rounding=@moonnum.Rounding::Up)
assert_eq(
flinch.add(@moonnum.BigFloat::one(), @moonnum.BigFloat::parse("1e-100")).to_string(),
"1.0",
)
}test "the value is one promotion away from a real" {
let c = @moonnum.Context::new(200)
let minus_one = -@moonnum.BigFloat::one(context=c)
let zero = @moonnum.BigFloat::zero(context=c)
let two = @moonnum.BigFloat::of_int(2, context=c)
assert_eq(minus_one.to_complex().sqrt(context=c).to_string(), "(0.0+1.0j)") // where sqrt(-1) was nan
assert_eq(
minus_one.to_complex().ln(context=c).to_string(),
"(0.0+3.14159265358979323846264338327950288419716939937510582097494j)",
)
assert_eq(
@moonnum.BigComplex::new(two, zero).asin(context=c).to_string(),
"(1.57079632679489661923132169163975144209858469968755291048747-1.31695789692481670862504634730796844402698197146751647976847j)",
)
}| On | Functions |
|---|---|
| integers | floordiv, floor_mod, divmod, power (with modulus~), gcd, lcm, gcd_ext, isqrt, factorial, comb, perm, bit_length, bit_count, ilog, to_bytes, of_bytes |
| floats | round_float(d, ndigits~), round_big, floor_big, ceil_big, trunc_big, as_ratio, as_integer_ratio, is_integer |
| complex | + - *, Complex::divide, Complex::pow, abs, conjugate |
| fractions | + - *, Fraction::divide, Fraction::pow, compare, floor_big, ceil_big, round, round_big, trunc_big, as_integer_ratio, 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, cbrt, nth_root, hypot, exp, expm1, ln, log1p, log2, log10, pow, powi, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, round, pi, e, ln2, ln10, 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) |
| to_radix(n, radix~) | the integer in any base from 2 to 36, lowercase, sign in front |
| 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]test "the format language, on every kind" {
assert_eq(@moonnum.format_int(1234567N, "015,d"), "000,001,234,567")
assert_eq(@moonnum.format_int(255N, "#010_x"), "0x000_00ff")
assert_eq(@moonnum.format_float(0.25, ".2%"), "25.00%")
assert_eq(@moonnum.format_float(-0.0, "z"), "0.0")
let z = @moonnum.parse_complex("3+4j")
assert_eq(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)
}test "a refusal says no, and the reason" {
let half = @moonnum.parse_fraction("1/2")
let bad = half.divide(@moonnum.parse_fraction("0")).to_string() catch {
@moonnum.ZeroDivision => "no"
_ => "no"
}
assert_eq(bad, "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 is_integer(d : Double) -> Boolfn repr_float(d : Double) -> Stringfn round_float(d : Double, ndigits~ : Int) -> DoubleInstall
Download zipPython's numbers for MoonBit.