diff --git a/double/log_js.mbt b/double/log_js.mbt new file mode 100644 index 000000000..0c11bb7cf --- /dev/null +++ b/double/log_js.mbt @@ -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" diff --git a/double/log.mbt b/double/log_nonjs.mbt similarity index 100% rename from double/log.mbt rename to double/log_nonjs.mbt diff --git a/double/log_test.mbt b/double/log_test.mbt new file mode 100644 index 000000000..6e37a7c3a --- /dev/null +++ b/double/log_test.mbt @@ -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.1760912590556813") +} + +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") +} diff --git a/double/moon.pkg.json b/double/moon.pkg.json index eaeab5d0f..34b0e672d 100644 --- a/double/moon.pkg.json +++ b/double/moon.pkg.json @@ -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"],