Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split and move @num.Num into @math.Num parts #165

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 0 additions & 25 deletions double/double_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
}
3 changes: 1 addition & 2 deletions double/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"moonbitlang/core/bool",
"moonbitlang/core/int64",
"moonbitlang/core/coverage"
],
"test_import": ["moonbitlang/core/num"]
]
}
25 changes: 0 additions & 25 deletions int/int_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
}
3 changes: 1 addition & 2 deletions int/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -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"]
}
24 changes: 0 additions & 24 deletions int64/int64_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
}
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
6 changes: 6 additions & 0 deletions math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Moonbit/Core Math

## Overview

A collection of common arithmetic/trigonometric functions and traits for
numerical computing.
29 changes: 29 additions & 0 deletions math/algebraic.mbt → math/arithmetic.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions math/math.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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

5 changes: 5 additions & 0 deletions math/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
"moonbitlang/core/double",
"moonbitlang/core/string",
"moonbitlang/core/coverage"
],
"test_import": [
"moonbitlang/core/double",
"moonbitlang/core/int",
"moonbitlang/core/int64"
]
}
49 changes: 49 additions & 0 deletions math/num_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.

20 changes: 0 additions & 20 deletions num/num.mbti

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