2019-12-19 20:16:03 +01:00
|
|
|
module math
|
2019-06-29 17:24:55 +02:00
|
|
|
|
2020-02-26 12:14:06 +01:00
|
|
|
fn tst_res(str1 string, str2 string) bool {
|
|
|
|
if (math.abs(str1.f64() - str2.f64())) < 1e-5 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-29 22:49:57 +02:00
|
|
|
fn test_gcd() {
|
2019-12-19 20:16:03 +01:00
|
|
|
assert gcd(6, 9) == 3
|
|
|
|
assert gcd(6, -9) == 3
|
|
|
|
assert gcd(-6, -9) == 3
|
|
|
|
assert gcd(0, 0) == 0
|
2019-06-29 22:49:57 +02:00
|
|
|
}
|
2019-06-29 17:24:55 +02:00
|
|
|
|
2019-06-29 22:49:57 +02:00
|
|
|
fn test_lcm() {
|
2019-12-19 20:16:03 +01:00
|
|
|
assert lcm(2, 3) == 6
|
|
|
|
assert lcm(-2, 3) == 6
|
|
|
|
assert lcm(-2, -3) == 6
|
|
|
|
assert lcm(0, 0) == 0
|
2019-06-29 17:24:55 +02:00
|
|
|
}
|
2019-07-02 12:50:33 +02:00
|
|
|
|
|
|
|
fn test_digits() {
|
2019-12-19 20:16:03 +01:00
|
|
|
digits_in_10th_base := digits(125, 10)
|
2019-07-02 12:50:33 +02:00
|
|
|
assert digits_in_10th_base[0] == 5
|
|
|
|
assert digits_in_10th_base[1] == 2
|
|
|
|
assert digits_in_10th_base[2] == 1
|
|
|
|
|
2019-12-19 20:16:03 +01:00
|
|
|
digits_in_16th_base := digits(15, 16)
|
2019-07-02 12:50:33 +02:00
|
|
|
assert digits_in_16th_base[0] == 15
|
|
|
|
|
2019-12-19 20:16:03 +01:00
|
|
|
negative_digits := digits(-4, 2)
|
2019-07-02 12:50:33 +02:00
|
|
|
assert negative_digits[2] == -1
|
|
|
|
}
|
|
|
|
|
2019-07-12 07:46:40 +02:00
|
|
|
fn test_erf() {
|
2019-12-19 20:16:03 +01:00
|
|
|
assert erf(0) == 0
|
|
|
|
assert erf(1.5) + erf(-1.5) == 0
|
|
|
|
assert erfc(0) == 1
|
|
|
|
assert erf(2.5) + erfc(2.5) == 1
|
|
|
|
assert erfc(3.6) + erfc(-3.6) == 2
|
2019-07-12 07:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_gamma() {
|
2019-12-19 20:16:03 +01:00
|
|
|
assert gamma(1) == 1
|
|
|
|
assert gamma(5) == 24
|
2019-11-28 09:46:52 +01:00
|
|
|
sval := '2.453737'
|
2020-02-26 12:14:06 +01:00
|
|
|
assert tst_res(log_gamma(4.5).str(), sval)
|
|
|
|
assert tst_res(log(gamma(4.5)).str(), sval)
|
|
|
|
//assert log_gamma(4.5).str() == sval
|
|
|
|
//assert log(gamma(4.5)).str() == sval
|
2019-12-19 20:16:03 +01:00
|
|
|
assert abs( log_gamma(4.5) - log(gamma(4.5)) ) < 0.000001
|
|
|
|
// assert log_gamma(4.5) == log(gamma(4.5)) /* <-- fails on alpine/musl
|
2019-07-12 07:46:40 +02:00
|
|
|
}
|
2019-11-06 16:04:48 +01:00
|
|
|
|
|
|
|
fn test_mod() {
|
|
|
|
assert 4 % 2 == 0
|
|
|
|
x := 2
|
|
|
|
assert u64(5) % x == 1
|
2019-11-06 18:47:03 +01:00
|
|
|
mut a := 10
|
|
|
|
a %= 2
|
|
|
|
assert a == 0
|
2019-12-19 20:16:03 +01:00
|
|
|
}
|