Skip to content

Commit 22e45b1

Browse files
better trigonometric functions' implementation
1 parent 4612955 commit 22e45b1

File tree

7 files changed

+963
-269
lines changed

7 files changed

+963
-269
lines changed

Diff for: double/double.mbti

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ let not_a_number : Double
1919
impl Double {
2020
abs(Double) -> Double
2121
ceil(Double) -> Double
22+
cos(Double) -> Double
2223
exp(Double) -> Double
2324
floor(Double) -> Double
2425
from_int(Int) -> Double
@@ -37,6 +38,8 @@ impl Double {
3738
pow(Double, Double) -> Double
3839
round(Double) -> Double
3940
signum(Double) -> Double
41+
sin(Double) -> Double
42+
tan(Double) -> Double
4043
to_be_bytes(Double) -> Bytes
4144
to_le_bytes(Double) -> Bytes
4245
to_string(Double) -> String

Diff for: double/moon.pkg.json

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"mod_nonjs.mbt": ["not", "js"],
1515
"pow_js.mbt": ["js"],
1616
"pow_nonjs.mbt": ["not", "js"],
17+
"trig_js.mbt" : ["js"],
18+
"trig_nonjs.mbt" : ["not", "js"],
1719
"round_js.mbt": ["js"],
1820
"round_wasm.mbt": ["wasm", "wasm-gc"],
1921
"round.mbt": ["not", "js", "wasm", "wasm-gc"],

Diff for: double/trig_js.mbt

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 sine of a number in radians. Handles special cases and edge
17+
/// conditions according to IEEE 754 standards.
18+
///
19+
/// Parameters:
20+
///
21+
/// * `x` : The angle in radians for which to calculate the sine.
22+
///
23+
/// Returns the sine of the angle `x`.
24+
///
25+
/// Example:
26+
///
27+
/// ```moonbit
28+
/// test "sin" {
29+
/// inspect!(0.0.sin(), content="0")
30+
/// inspect!(1.570796326794897.sin(), content="1") // pi / 2
31+
/// inspect!(2.0.sin(), content="0.9092974268256817")
32+
/// inspect!(-5.0.sin(), content="0.9589242746631385")
33+
/// inspect!(31415926535897.9323846.sin(), content="0.0012091232715481885")
34+
/// inspect!(@double.not_a_number.sin(), content="NaN")
35+
/// inspect!(@double.infinity.sin(), content="NaN")
36+
/// inspect!(@double.neg_infinity.sin(), content="NaN")
37+
/// }
38+
/// ```
39+
pub fn sin(self : Double) -> Double = "Math" "sin"
40+
41+
///|
42+
/// Calculates the cosine of a number in radians. Handles special cases and edge
43+
/// conditions according to IEEE 754 standards.
44+
///
45+
/// Parameters:
46+
///
47+
/// * `x` : The angle in radians for which to calculate the cosine.
48+
///
49+
/// Returns the cosine of the angle `x`.
50+
///
51+
/// Example:
52+
///
53+
/// ```moonbit
54+
/// test "cos" {
55+
/// inspect!(0.0.cos(), content="1")
56+
/// inspect!(2.5.cos(), content="-0.8011436155469337")
57+
/// inspect!((-3.141592653589793).cos(), content="-1") // -pi
58+
/// inspect!((-5.0).cos(), content="0.28366218546322625")
59+
/// inspect!(31415926535897.9323846.cos(), content="0.9999992690101899")
60+
/// inspect!(@double.not_a_number.cos(), content="NaN")
61+
/// inspect!(@double.infinity.cos(), content="NaN")
62+
/// inspect!(@double.neg_infinity.cos(), content="NaN")
63+
/// }
64+
/// ```
65+
pub fn cos(self : Double) -> Double = "Math" "cos"
66+
67+
///|
68+
/// Calculates the tangent of a number in radians. Handles special cases and edge
69+
/// conditions according to IEEE 754 standards.
70+
///
71+
/// Parameters:
72+
///
73+
/// * `x` : The angle in radians for which to calculate the tangent.
74+
///
75+
/// Returns the tangent of the angle `x`.
76+
///
77+
/// Example:
78+
///
79+
/// ```moonbit
80+
/// test "tan" {
81+
/// inspect!(0.0.tan(), content="0")
82+
/// inspect!(0.7853981633974483.tan(), content="0.9999999999999999")
83+
/// inspect!(4.0.tan(), content="1.1578212823495777")
84+
/// inspect!(5.0.tan(), content="-3.380515006246586")
85+
/// inspect!(31415926535897.9323846.tan(), content="0.0012091241554056254")
86+
/// inspect!(@double.not_a_number.tan(), content="NaN")
87+
/// inspect!(@double.infinity.tan(), content="NaN")
88+
/// inspect!(@double.neg_infinity.tan(), content="NaN")
89+
/// }
90+
/// ```
91+
pub fn tan(self : Double) -> Double = "Math" "tan"

0 commit comments

Comments
 (0)