///|
test {
let a = @rational.new(1L, 2L).unwrap()
let b = @rational.new(1L, 3L).unwrap()
assert_eq(a + b, @rational.new(5L, 6L).unwrap())
assert_eq(a - b, @rational.new(1L, 6L).unwrap())
assert_eq(a * b, @rational.new(1L, 6L).unwrap())
assert_eq(a / b, @rational.new(3L, 2L).unwrap())
assert_eq(-a, @rational.new(-1L, 2L).unwrap())
assert_eq(a.reciprocal(), @rational.new(2L, 1L).unwrap())
assert_eq(a.abs(), @rational.new(1L, 2L).unwrap())
}///|
test {
let a = @rational.new(1L, 2L).unwrap()
let b = @rational.new(1L, 3L).unwrap()
assert_eq(a == b, false)
assert_eq(a != b, true)
assert_eq(a < b, false)
assert_eq(a <= b, false)
assert_eq(a > b, true)
assert_eq(a >= b, true)
assert_eq(Compare::compare(a, b), 1)
}///|
test {
let a = @rational.new(1L, 2L).unwrap()
assert_eq(a.floor(), 0)
assert_eq(a.ceil(), 1)
assert_eq(Show::to_string(a.fract()), "1/2")
assert_eq(a.trunc(), 0)
assert_eq(a.is_integer(), false)
}///|
test {
let a = @rational.new(1L, 2L).unwrap()
assert_eq(a.to_double(), 0.5)
assert_eq(Show::to_string(@rational.from_double(0.5)), "1/2")
}///|
test {
let a = @rational.new(1L, 2L).unwrap()
assert_eq(Show::to_string(a), "1/2")
}trait Integralfn abs(self : Int) -> Intfn compare_fraction(numerator1 : Int, denominator1 : Int, numerator2 : Int, denominator2 : Int) -> Intfn from_int(i : Int) -> Intfn signum(self : Int) -> Intfn abs(self : Int64) -> Int64fn compare_fraction(numerator1 : Int64, denominator1 : Int64, numerator2 : Int64, denominator2 : Int64) -> Intfn from_int(i : Int) -> Int64fn signum(self : Int64) -> Inttest {
let error = RationalError::RationalError("division by zero")
debug_inspect(
error,
content=(
#|RationalError("division by zero")
),
)
}impl Eq for RationalErrortest {
let error1 = @rational.RationalError("division by zero")
let error2 = @rational.RationalError("division by zero")
let error3 = @rational.RationalError("overflow")
inspect(error1 == error2, content="true")
inspect(error1 == error3, content="false")
}#deprecated("`RationalError::equal` is deprecated, use `Eq::equal` instead.")
fn RationalError::equal(self : RationalError, other : RationalError) -> Bool#deprecated("`RationalError::not_equal` is deprecated, use `Eq::not_equal` instead.")
fn RationalError::not_equal(x : RationalError, y : RationalError) -> Bool#deprecated("`RationalError::to_json` is deprecated, use `ToJson::to_json` instead.")
fn RationalError::to_json(RationalError) -> Json#deprecated("`RationalError::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn RationalError::to_repr(RationalError) -> Reprtest {
let a = @rational.new(1L, 2L).unwrap() // 1/2
let b = @rational.new(2L, 3L).unwrap() // 2/3
inspect(Compare::compare(a, b), content="-1") // 1/2 < 2/3
let c = @rational.new(3L, 4L).unwrap() // 3/4
let d = @rational.new(3L, 4L).unwrap() // 3/4
inspect(Compare::compare(c, d), content="0") // 3/4 == 3/4
let e = @rational.new(5L, 6L).unwrap() // 5/6
let f = @rational.new(1L, 2L).unwrap() // 1/2
inspect(Compare::compare(e, f), content="1") // 5/6 > 1/2
}test {
let a = @rational.new(1L, 2L) // 1/2
let b = @rational.new(2L, 3L) // 2/3
match (a, b) {
(Some(x), Some(y)) => {
let result = x / y // 3/4
inspect(result, content="3/4")
}
_ => ()
}
}test {
let a = @rational.new(1L, 2L).unwrap() // 1/2
let b = @rational.new(2L, 3L).unwrap() // 2/3
let c = a * b // 1/3
inspect(c, content="1/3")
}test {
let positive = @rational.new(3L, 4L).unwrap()
let negative = -positive
inspect(Show::to_string(negative), content="-3/4")
let negative_original = @rational.new(-5L, 2L).unwrap()
let positive_result = -negative_original
inspect(Show::to_string(positive_result), content="5/2")
}test {
let zero = @rational.new(0L, 1).unwrap()
inspect(zero, content="0")
let integer = @rational.new(5L, 1).unwrap()
inspect(integer, content="5")
let fraction = @rational.new(3L, 4).unwrap()
inspect(fraction, content="3/4")
let negative = @rational.new(-7L, 3).unwrap()
inspect(negative, content="-7/3")
}test {
let a = @rational.new(1L, 2L).unwrap() // 1/2
let b = @rational.new(1L, 3L).unwrap() // 1/3
inspect(a - b, content="1/6") // 1/2 - 1/3 = 1/6
}test {
let positive = @rational.new(3L, 4L).unwrap()
let negative = @rational.new(-3L, 4L).unwrap()
inspect(positive.abs(), content="3/4")
inspect(negative.abs(), content="3/4")
}test {
let r1 = @rational.new(3L, 2L).unwrap() // 3/2 = 1.5
inspect(r1.ceil(), content="2")
let r2 = @rational.new(-3L, 2L).unwrap() // -3/2 = -1.5
inspect(r2.ceil(), content="-1")
let r3 = @rational.new(4L, 2L).unwrap() // 4/2 = 2.0
inspect(r3.ceil(), content="2")
}test {
let a = @rational.new(1L, 2L)
let b = @rational.new(2L, 3L)
match (a, b) {
(Some(x), Some(y)) => {
let result = x.div_checked(y)
inspect(result, content="3/4")
}
_ => ()
}
}test {
let r1 = @rational.new(7L, 3L).unwrap() // 7/3 ≈ 2.33
inspect(r1.floor(), content="2")
let r2 = @rational.new(-7L, 3L).unwrap() // -7/3 ≈ -2.33
inspect(r2.floor(), content="-3")
let r3 = @rational.new(6L, 3L).unwrap() // 6/3 = 2.0
inspect(r3.floor(), content="2")
}test {
let r1 = @rational.new(7L, 2L).unwrap() // 7/2 = 3.5
inspect(r1.fract(), content="1/2") // fractional part is 0.5 = 1/2
let r2 = @rational.new(-7L, 2L).unwrap() // -7/2 = -3.5
inspect(r2.fract(), content="-1/2") // fractional part is -0.5 = -1/2
let r3 = @rational.new(5L, 1L).unwrap() // 5/1 = 5
inspect(r3.fract(), content="0") // fractional part is 0
}test {
let half = @rational.new(1L, 2L).unwrap()
inspect(half.is_integer(), content="false")
let whole = @rational.new(5L, 1L).unwrap()
inspect(whole.is_integer(), content="true")
let zero = @rational.new(0L, 1L).unwrap()
inspect(zero.is_integer(), content="true")
}test {
let half = @rational.new(1L, 2L).unwrap()
let two = half.reciprocal()
inspect(two, content="2")
let negative_third = @rational.new(-1L, 3L).unwrap()
let negative_three = negative_third.reciprocal()
inspect(negative_three, content="-3")
}test {
let half = @rational.new(1L, 2L).unwrap()
let two = half.reciprocal_checked()
inspect(two, content="2")
}test {
let rational = @rational.new(1L, 2L).unwrap()
inspect(rational.to_double(), content="0.5")
let negative = @rational.new(-3L, 4L).unwrap()
inspect(negative.to_double(), content="-0.75")
}test {
let a = @rational.new(7L, 3L).unwrap() // 7/3 = 2.333...
inspect(a.trunc(), content="2")
let b = @rational.new(-7L, 3L).unwrap() // -7/3 = -2.333...
inspect(b.trunc(), content="-2")
let c = @rational.new(5L, 2L).unwrap() // 5/2 = 2.5
inspect(c.trunc(), content="2")
let d = @rational.new(-5L, 2L).unwrap() // -5/2 = -2.5
inspect(d.trunc(), content="-2")
}test {
// Convert 0.5 to rational 1/2
let half = @rational.from_double(0.5)
inspect(half, content="1/2")
// Convert 0.333... to a close rational approximation
let third = @rational.from_double(1.0 / 3.0)
inspect(third, content="1/3")
}test {
// Create 3/4
let r1 = @rational.new(3L, 4L).unwrap()
inspect(r1, content="3/4")
// Create -1/2 (negative numerator)
let r2 = @rational.new(-1L, 2L).unwrap()
inspect(r2, content="-1/2")
// Create -1/2 (negative denominator gets normalized)
let r3 = @rational.new(1L, -2L).unwrap()
inspect(r3, content="-1/2")
// Create 1/2 (double negatives cancel out)
let r4 = @rational.new(-1L, -2L).unwrap()
inspect(r4, content="1/2")
// Automatic reduction to simplest form
let r5 = @rational.new(6L, 9L).unwrap()
inspect(r5, content="2/3")
// Division by zero returns None
let r6 = @rational.new(1L, 0L)
assert_true(r6 is None)
}Install
Download zipexperimental packages for moonbitlang/core