Skip to content

Commit 4612955

Browse files
tonyfettesbobzhang
authored andcommitted
feat: use Math for double::{ln,log2,log10} on JS
1 parent 9d700ab commit 4612955

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

double/log_js.mbt

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|
16+
/// Calculates the natural logarithm of a double-precision floating-point number.
17+
///
18+
/// Parameters:
19+
///
20+
/// * `self`: The input number.
21+
///
22+
/// Returns the natural logarithm of the input number, with the following special
23+
/// cases:
24+
///
25+
/// * Returns NaN if the input is NaN or negative
26+
/// * Returns negative infinity if the input is zero
27+
/// * Returns the input if it is positive infinity
28+
///
29+
/// Example:
30+
///
31+
/// ```moonbit
32+
/// test "ln" {
33+
/// inspect!(2.0.ln(), content="0.6931471805599453")
34+
/// inspect!(1.0.ln(), content="0")
35+
/// inspect!((-1.0).ln(), content="NaN")
36+
/// inspect!(0.0.ln(), content="-Infinity")
37+
/// }
38+
/// ```
39+
pub fn ln(self : Double) -> Double = "Math" "log"
40+
41+
///|
42+
/// Calculates the base-2 logarithm of a double-precision floating-point number.
43+
///
44+
/// Parameters:
45+
///
46+
/// * `x` : A double-precision floating-point number.
47+
///
48+
/// Returns the base-2 logarithm of the input number.
49+
///
50+
/// Example:
51+
///
52+
/// ```moonbit
53+
/// test "log2" {
54+
/// inspect!(2.0.log2(), content="1")
55+
/// inspect!(0.5.log2(), content="-1")
56+
/// inspect!(3.0.log2(), content="1.584962500721156")
57+
/// }
58+
/// ```
59+
pub fn log2(self : Double) -> Double = "Math" "log2"
60+
61+
///|
62+
/// Calculates the base-10 logarithm of a double-precision floating-point number.
63+
///
64+
/// Parameters:
65+
///
66+
/// * `self` : The double-precision floating-point number to calculate the
67+
/// logarithm of.
68+
///
69+
/// Returns a double-precision floating-point number representing the base-10
70+
/// logarithm of the input.
71+
///
72+
/// Example:
73+
///
74+
/// ```moonbit
75+
/// test "log10" {
76+
/// inspect!(1.0.log10(), content="0")
77+
/// inspect!(10.0.log10(), content="1")
78+
/// inspect!(100.0.log10(), content="2")
79+
/// }
80+
/// ```
81+
pub fn log10(self : Double) -> Double = "Math" "log10"
File renamed without changes.

double/log_test.mbt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
test "log2 log10" {
16+
// log2
17+
inspect!(3.0.log2(), content="1.584962500721156")
18+
inspect!(2.0.log2(), content="1")
19+
inspect!(1.0.log2(), content="0")
20+
inspect!(0.5.log2(), content="-1")
21+
inspect!(0.25.log2(), content="-2")
22+
inspect!(0.1.log2(), content="-3.321928094887362")
23+
24+
// log10
25+
inspect!(0.2.log10(), content="-0.6989700043360187")
26+
inspect!(15.0.log10(), content="1.1760912590556813")
27+
}
28+
29+
test "ln" {
30+
inspect!(not_a_number.ln(), content="NaN")
31+
inspect!(infinity.ln(), content="Infinity")
32+
inspect!(neg_infinity.ln(), content="NaN")
33+
inspect!((-1.0).ln(), content="NaN")
34+
inspect!(0.0.ln(), content="-Infinity")
35+
inspect!((-0.0).ln(), content="-Infinity")
36+
inspect!(50.0.ln(), content="3.912023005428146")
37+
inspect!(2.0.ln(), content="0.6931471805599453")
38+
inspect!(1.1125369292536007e-308.ln(), content="-709.0895657128241")
39+
inspect!(5.562684646268003e-309.ln(), content="-709.782712893384")
40+
}

double/moon.pkg.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"targets": {
99
"exp_js.mbt": ["js"],
1010
"exp_nonjs.mbt": ["not", "js"],
11+
"log_js.mbt": ["js"],
12+
"log_nonjs.mbt": ["not", "js"],
1113
"mod_js.mbt": ["js"],
1214
"mod_nonjs.mbt": ["not", "js"],
1315
"pow_js.mbt": ["js"],

0 commit comments

Comments
 (0)