Skip to content

Commit

Permalink
feat: use Math for double::{ln,log2,log10} on JS
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfettes committed Jan 16, 2025
1 parent 31a207a commit 9cb3a29
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
81 changes: 81 additions & 0 deletions double/log_js.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2025 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.

///|
/// Calculates the natural logarithm of a double-precision floating-point number.
///
/// Parameters:
///
/// * `self`: The input number.
///
/// Returns the natural logarithm of the input number, with the following special
/// cases:
///
/// * Returns NaN if the input is NaN or negative
/// * Returns negative infinity if the input is zero
/// * Returns the input if it is positive infinity
///
/// Example:
///
/// ```moonbit
/// test "ln" {
/// inspect!(2.0.ln(), content="0.6931471805599453")
/// inspect!(1.0.ln(), content="0")
/// inspect!((-1.0).ln(), content="NaN")
/// inspect!(0.0.ln(), content="-Infinity")
/// }
/// ```
pub fn ln(self : Double) -> Double = "Math" "log"

///|
/// Calculates the base-2 logarithm of a double-precision floating-point number.
///
/// Parameters:
///
/// * `x` : A double-precision floating-point number.
///
/// Returns the base-2 logarithm of the input number.
///
/// Example:
///
/// ```moonbit
/// test "log2" {
/// inspect!(2.0.log2(), content="1")
/// inspect!(0.5.log2(), content="-1")
/// inspect!(3.0.log2(), content="1.584962500721156")
/// }
/// ```
pub fn log2(self : Double) -> Double = "Math" "log2"

///|
/// Calculates the base-10 logarithm of a double-precision floating-point number.
///
/// Parameters:
///
/// * `self` : The double-precision floating-point number to calculate the
/// logarithm of.
///
/// Returns a double-precision floating-point number representing the base-10
/// logarithm of the input.
///
/// Example:
///
/// ```moonbit
/// test "log10" {
/// inspect!(1.0.log10(), content="0")
/// inspect!(10.0.log10(), content="1")
/// inspect!(100.0.log10(), content="2")
/// }
/// ```
pub fn log10(self : Double) -> Double = "Math" "log10"
File renamed without changes.
40 changes: 40 additions & 0 deletions double/log_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2025 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.

test "log2 log10" {
// log2
inspect!(3.0.log2(), content="1.584962500721156")
inspect!(2.0.log2(), content="1")
inspect!(1.0.log2(), content="0")
inspect!(0.5.log2(), content="-1")
inspect!(0.25.log2(), content="-2")
inspect!(0.1.log2(), content="-3.321928094887362")

// log10
inspect!(0.2.log10(), content="-0.6989700043360187")
// inspect!(15.0.log10(), content="1.1760912590556811")
}

test "ln" {
inspect!(not_a_number.ln(), content="NaN")
inspect!(infinity.ln(), content="Infinity")
inspect!(neg_infinity.ln(), content="NaN")
inspect!((-1.0).ln(), content="NaN")
inspect!(0.0.ln(), content="-Infinity")
inspect!((-0.0).ln(), content="-Infinity")
inspect!(50.0.ln(), content="3.912023005428146")
inspect!(2.0.ln(), content="0.6931471805599453")
inspect!(1.1125369292536007e-308.ln(), content="-709.0895657128241")
inspect!(5.562684646268003e-309.ln(), content="-709.782712893384")
}
2 changes: 2 additions & 0 deletions double/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"targets": {
"exp_js.mbt": ["js"],
"exp_nonjs.mbt": ["not", "js"],
"log_js.mbt": ["js"],
"log_nonjs.mbt": ["not", "js"],
"mod_js.mbt": ["js"],
"mod_nonjs.mbt": ["not", "js"],
"pow_js.mbt": ["js"],
Expand Down

0 comments on commit 9cb3a29

Please sign in to comment.