diff --git a/double/double_test.mbt b/double/double_test.mbt index 4b9cd36f..f307e133 100644 --- a/double/double_test.mbt +++ b/double/double_test.mbt @@ -11,28 +11,3 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - - -fn test_num[T : @num.Num + Debug + Default + Eq]( - x : T, - y : T, - x_plus_y : T, - x_mul_y : T, - x_minus_y : T, - x_div_y : T, - x_signum : T -) -> Result[Unit, String] { - @assertion.assert_eq(x + y, x_plus_y)? - @assertion.assert_eq(x * y, x_mul_y)? - @assertion.assert_eq(x - y, x_minus_y)? - @assertion.assert_eq(x / y, x_div_y)? - @assertion.assert_eq(x.abs(), T::default() - x)? - @assertion.assert_eq(x.signum(), x_signum)? - Ok(()) -} - -test "double.num" { - let x = -500.0 - let y = 792.0 - test_num(x, y, x + y, x * y, x - y, x / y, -1.0)? -} diff --git a/double/moon.pkg.json b/double/moon.pkg.json index 7eacc139..24125054 100644 --- a/double/moon.pkg.json +++ b/double/moon.pkg.json @@ -6,6 +6,5 @@ "moonbitlang/core/bool", "moonbitlang/core/int64", "moonbitlang/core/coverage" - ], - "test_import": ["moonbitlang/core/num"] + ] } diff --git a/int/int_test.mbt b/int/int_test.mbt index a335d4b6..f307e133 100644 --- a/int/int_test.mbt +++ b/int/int_test.mbt @@ -11,28 +11,3 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - - -fn test_num[T : @num.Num + Debug + Default + Eq]( - x : T, - y : T, - x_plus_y : T, - x_mul_y : T, - x_minus_y : T, - x_div_y : T, - x_signum : T -) -> Result[Unit, String] { - @assertion.assert_eq(x + y, x_plus_y)? - @assertion.assert_eq(x * y, x_mul_y)? - @assertion.assert_eq(x - y, x_minus_y)? - @assertion.assert_eq(x / y, x_div_y)? - @assertion.assert_eq(x.abs(), T::default() - x)? - @assertion.assert_eq(x.signum(), x_signum)? - Ok(()) -} - -test "int.num" { - let x = -5 - let y = 7 - test_num(x, y, x + y, x * y, x - y, x / y, -1)? -} \ No newline at end of file diff --git a/int/moon.pkg.json b/int/moon.pkg.json index 36812239..fc8470a2 100644 --- a/int/moon.pkg.json +++ b/int/moon.pkg.json @@ -1,4 +1,3 @@ { - "import": ["moonbitlang/core/builtin", "moonbitlang/core/coverage"], - "test_import": ["moonbitlang/core/num", "moonbitlang/core/assertion"] + "import": ["moonbitlang/core/builtin", "moonbitlang/core/coverage"] } diff --git a/int64/int64_test.mbt b/int64/int64_test.mbt index 2f909f91..f307e133 100644 --- a/int64/int64_test.mbt +++ b/int64/int64_test.mbt @@ -11,27 +11,3 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - -fn test_num[T : @num.Num + Debug + Default + Eq]( - x : T, - y : T, - x_plus_y : T, - x_mul_y : T, - x_minus_y : T, - x_div_y : T, - x_signum : T -) -> Result[Unit, String] { - @assertion.assert_eq(x + y, x_plus_y)? - @assertion.assert_eq(x * y, x_mul_y)? - @assertion.assert_eq(x - y, x_minus_y)? - @assertion.assert_eq(x / y, x_div_y)? - @assertion.assert_eq(x.abs(), T::default() - x)? - @assertion.assert_eq(x.signum(), x_signum)? - Ok(()) -} - -test "int64.num" { - let x = -500L - let y = 792L - test_num(x, y, x + y, x * y, x - y, x / y, -1L)? -} \ No newline at end of file diff --git a/int64/moon.pkg.json b/int64/moon.pkg.json index 3f548e9c..144575e9 100644 --- a/int64/moon.pkg.json +++ b/int64/moon.pkg.json @@ -1,7 +1,6 @@ { "import": [ "moonbitlang/core/builtin", - "moonbitlang/core/num", "moonbitlang/core/assertion", "moonbitlang/core/coverage" ] diff --git a/math/README.md b/math/README.md new file mode 100644 index 00000000..bb99271c --- /dev/null +++ b/math/README.md @@ -0,0 +1,6 @@ +# Moonbit/Core Math + +## Overview + +A collection of common arithmetic/trigonometric functions and traits for +numerical computing. diff --git a/math/algebraic.mbt b/math/arithmetic.mbt similarity index 86% rename from math/algebraic.mbt rename to math/arithmetic.mbt index 831bc4ce..cff6a41a 100644 --- a/math/algebraic.mbt +++ b/math/arithmetic.mbt @@ -90,6 +90,35 @@ test "minimum.ref" { @assertion.assert_is(minimum(x2t, x2), x2t)? } +pub trait Add { + op_add(Self, Self) -> Self +} + +pub trait Subtract { + op_sub(Self, Self) -> Self +} + +pub trait Multiply { + op_mul(Self, Self) -> Self +} + +pub trait Divide { + op_div(Self, Self) -> Self +} + +pub trait Negate { + op_neg(Self) -> Self +} + +pub trait FromInt { + from_int(Int) -> Self +} + +pub trait Num: FromInt + Add + Subtract + Multiply + Divide + Negate { + abs(Self) -> Self + signum(Self) -> Self +} + // For testing purposes struct T { mut x : Int diff --git a/math/math.mbti b/math/math.mbti index 7b12de84..01a69c80 100644 --- a/math/math.mbti +++ b/math/math.mbti @@ -18,6 +18,34 @@ fn T::debug_write(T, Buffer) -> Unit fn T::op_equal(T, T) -> Bool // Traits +pub trait Add { + op_add(Self, Self) -> Self +} + +pub trait Divide { + op_div(Self, Self) -> Self +} + +pub trait FromInt { + from_int(Int) -> Self +} + +pub trait Multiply { + op_mul(Self, Self) -> Self +} + +pub trait Negate { + op_neg(Self) -> Self +} + +pub trait Num : FromInt + Add + Subtract + Multiply + Divide + Negate { + abs(Self) -> Self + signum(Self) -> Self +} + +pub trait Subtract { + op_sub(Self, Self) -> Self +} // Extension Methods diff --git a/math/moon.pkg.json b/math/moon.pkg.json index 63fdd7e7..59f17679 100644 --- a/math/moon.pkg.json +++ b/math/moon.pkg.json @@ -6,5 +6,10 @@ "moonbitlang/core/double", "moonbitlang/core/string", "moonbitlang/core/coverage" + ], + "test_import": [ + "moonbitlang/core/double", + "moonbitlang/core/int", + "moonbitlang/core/int64" ] } diff --git a/math/num_test.mbt b/math/num_test.mbt new file mode 100644 index 00000000..e4546233 --- /dev/null +++ b/math/num_test.mbt @@ -0,0 +1,49 @@ +// Copyright 2024 International Digital Economy Academy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +fn test_num[T : Num + Debug + Default + Eq]( + x : T, + y : T, + x_plus_y : T, + x_mul_y : T, + x_minus_y : T, + x_div_y : T, + x_signum : T +) -> Result[Unit, String] { + @assertion.assert_eq(x + y, x_plus_y)? + @assertion.assert_eq(x * y, x_mul_y)? + @assertion.assert_eq(x - y, x_minus_y)? + @assertion.assert_eq(x / y, x_div_y)? + @assertion.assert_eq(x.abs(), T::default() - x)? + @assertion.assert_eq(x.signum(), x_signum)? + Ok(()) +} + +test "double.num" { + let x = -500.0 + let y = 792.0 + test_num(x, y, x + y, x * y, x - y, x / y, -1.0)? +} + +test "int.num" { + let x = -5 + let y = 7 + test_num(x, y, x + y, x * y, x - y, x / y, -1)? +} + +test "int64.num" { + let x = -500L + let y = 792L + test_num(x, y, x + y, x * y, x - y, x / y, -1L)? +} diff --git a/num/moon.pkg.json b/num/moon.pkg.json deleted file mode 100644 index 661e75a8..00000000 --- a/num/moon.pkg.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "import": [ - "moonbitlang/core/builtin", - "moonbitlang/core/coverage" - ] -} diff --git a/num/num.mbt b/num/num.mbt deleted file mode 100644 index c92e426e..00000000 --- a/num/num.mbt +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2024 International Digital Economy Academy -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -pub trait Num { - from_int(Int) -> Self - op_add(Self, Self) -> Self - op_sub(Self, Self) -> Self - op_mul(Self, Self) -> Self - op_div(Self, Self) -> Self - op_neg(Self) -> Self - abs(Self) -> Self - signum(Self) -> Self -} diff --git a/num/num.mbti b/num/num.mbti deleted file mode 100644 index 56f79d79..00000000 --- a/num/num.mbti +++ /dev/null @@ -1,20 +0,0 @@ -package moonbitlang/core/num - -// Values - -// Types and methods - -// Traits -pub trait Num { - from_int(Int) -> Self - op_add(Self, Self) -> Self - op_sub(Self, Self) -> Self - op_mul(Self, Self) -> Self - op_div(Self, Self) -> Self - op_neg(Self) -> Self - abs(Self) -> Self - signum(Self) -> Self -} - -// Extension Methods - diff --git a/ref/moon.pkg.json b/ref/moon.pkg.json index 018b3cb1..36681f17 100644 --- a/ref/moon.pkg.json +++ b/ref/moon.pkg.json @@ -2,7 +2,7 @@ "import": [ "moonbitlang/core/builtin", "moonbitlang/core/assertion", - "moonbitlang/core/num", + "moonbitlang/core/math", "moonbitlang/core/int", "moonbitlang/core/coverage" ]