Skip to content

Commit

Permalink
Split and move @num.Num into @math.Num parts
Browse files Browse the repository at this point in the history
  • Loading branch information
fantix committed Apr 2, 2024
1 parent 91b0c05 commit 268fd46
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 112 deletions.
26 changes: 1 addition & 25 deletions double/double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,6 @@ pub fn is_neg_inf(self: Double) -> Bool {
self < -max_val
}

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)?
}

test "is_nan" {
@assertion.assert_true(is_nan(not_a_number))?
@assertion.assert_false(is_nan(0.0))?
Expand Down Expand Up @@ -151,4 +127,4 @@ test "is_neg_inf" {
@assertion.assert_true(is_neg_inf(negative_infinity))?
@assertion.assert_false(is_neg_inf(0.0))?
@assertion.assert_false(is_neg_inf(12345.678))?
}
}
1 change: 0 additions & 1 deletion double/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/num",
"moonbitlang/core/assertion",
"moonbitlang/core/coverage"
]
Expand Down
24 changes: 0 additions & 24 deletions int/int.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,3 @@ pub fn Int::max_value() -> Int {
pub fn Int::min_value() -> Int {
min_val
}

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)?
}
1 change: 0 additions & 1 deletion int/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/num",
"moonbitlang/core/assertion",
"moonbitlang/core/coverage"
]
Expand Down
25 changes: 0 additions & 25 deletions int64/int64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,3 @@ pub fn Int64::max_value() -> Int64 {
pub fn Int64::min_value() -> Int64 {
min_val
}


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)?
}
1 change: 0 additions & 1 deletion int64/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/num",
"moonbitlang/core/assertion",
"moonbitlang/core/coverage"
]
Expand Down
29 changes: 29 additions & 0 deletions math/algebraic.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,32 @@ test "minimum.ref" {
@assertion.assert_is(minimum(x2, x2t), x2)?
@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
}
2 changes: 2 additions & 0 deletions math/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"moonbitlang/core/builtin",
"moonbitlang/core/assertion",
"moonbitlang/core/double",
"moonbitlang/core/int",
"moonbitlang/core/int64",
"moonbitlang/core/coverage"
]
}
49 changes: 49 additions & 0 deletions math/test.mbt
Original file line number Diff line number Diff line change
@@ -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)?
}
6 changes: 0 additions & 6 deletions num/moon.pkg.json

This file was deleted.

24 changes: 0 additions & 24 deletions num/num.mbt

This file was deleted.

2 changes: 1 addition & 1 deletion ref/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/assertion",
"moonbitlang/core/num",
"moonbitlang/core/math",
"moonbitlang/core/int",
"moonbitlang/core/coverage"
]
Expand Down
8 changes: 4 additions & 4 deletions ref/ref.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ pub fn update[T](self : Ref[T], f : (T) -> T) -> Unit {
}

/// Increments the value of a reference by 1. The default value is 1.
pub fn incr[T : @num.Num](
pub fn incr[T : @math.FromInt + @math.Add](
self : Ref[T],
~value : T = @num.Num::from_int(1)
~value : T = @math.FromInt::from_int(1)
) -> Unit {
self.val = self.val + value
}

/// Decrements the value of a reference by value. The default value is 1.
pub fn decr[T : @num.Num](
pub fn decr[T : @math.FromInt + @math.Subtract](
self : Ref[T],
~value : T = @num.Num::from_int(1)
~value : T = @math.FromInt::from_int(1)
) -> Unit {
self.val = self.val - value
}
Expand Down

0 comments on commit 268fd46

Please sign in to comment.