1 | // Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. |
2 | // Use of this source code is governed by an MIT license |
3 | // that can be found in the LICENSE file. |
4 | module math |
5 | |
6 | // aprox_sin returns an approximation of sin(a) made using lolremez |
7 | pub fn aprox_sin(a f64) f64 { |
8 | a0 := 1.91059300966915117e-31 |
9 | a1 := 1.00086760103908896 |
10 | a2 := -1.21276126894734565e-2 |
11 | a3 := -1.38078780785773762e-1 |
12 | a4 := -2.67353392911981221e-2 |
13 | a5 := 2.08026600266304389e-2 |
14 | a6 := -3.03996055049204407e-3 |
15 | a7 := 1.38235642404333740e-4 |
16 | return a0 + a * (a1 + a * (a2 + a * (a3 + a * (a4 + a * (a5 + a * (a6 + a * a7)))))) |
17 | } |
18 | |
19 | // aprox_cos returns an approximation of sin(a) made using lolremez |
20 | pub fn aprox_cos(a f64) f64 { |
21 | a0 := 9.9995999154986614e-1 |
22 | a1 := 1.2548995793001028e-3 |
23 | a2 := -5.0648546280678015e-1 |
24 | a3 := 1.2942246466519995e-2 |
25 | a4 := 2.8668384702547972e-2 |
26 | a5 := 7.3726485210586547e-3 |
27 | a6 := -3.8510875386947414e-3 |
28 | a7 := 4.7196604604366623e-4 |
29 | a8 := -1.8776444013090451e-5 |
30 | return a0 + a * (a1 + a * (a2 + a * (a3 + a * (a4 + a * (a5 + a * (a6 + a * (a7 + a * a8))))))) |
31 | } |
32 | |
33 | // copysign returns a value with the magnitude of x and the sign of y |
34 | [inline] |
35 | pub fn copysign(x f64, y f64) f64 { |
36 | return f64_from_bits((f64_bits(x) & ~sign_mask) | (f64_bits(y) & sign_mask)) |
37 | } |
38 | |
39 | // degrees converts from radians to degrees. |
40 | [inline] |
41 | pub fn degrees(radians f64) f64 { |
42 | return radians * (180.0 / pi) |
43 | } |
44 | |
45 | // angle_diff calculates the difference between angles in radians |
46 | [inline] |
47 | pub fn angle_diff(radian_a f64, radian_b f64) f64 { |
48 | mut delta := fmod(radian_b - radian_a, tau) |
49 | delta = fmod(delta + 1.5 * tau, tau) |
50 | delta -= .5 * tau |
51 | return delta |
52 | } |
53 | |
54 | [params] |
55 | pub struct DigitParams { |
56 | base int = 10 |
57 | reverse bool |
58 | } |
59 | |
60 | // digits returns an array of the digits of `num` in the given optional `base`. |
61 | // The `num` argument accepts any integer type (i8|i16|int|isize|i64), and will be cast to i64 |
62 | // The `base:` argument is optional, it will default to base: 10. |
63 | // This function returns an array of the digits in reverse order i.e.: |
64 | // Example: assert math.digits(12345, base: 10) == [5,4,3,2,1] |
65 | // You can also use it, with an explicit `reverse: true` parameter, |
66 | // (it will do a reverse of the result array internally => slower): |
67 | // Example: assert math.digits(12345, reverse: true) == [1,2,3,4,5] |
68 | pub fn digits(num i64, params DigitParams) []int { |
69 | // set base to 10 initially and change only if base is explicitly set. |
70 | mut b := params.base |
71 | if b < 2 { |
72 | panic('digits: Cannot find digits of n with base ${b}') |
73 | } |
74 | mut n := num |
75 | mut sign := 1 |
76 | if n < 0 { |
77 | sign = -1 |
78 | n = -n |
79 | } |
80 | |
81 | mut res := []int{} |
82 | if n == 0 { |
83 | // short-circuit and return 0 |
84 | res << 0 |
85 | return res |
86 | } |
87 | for n != 0 { |
88 | next_n := n / b |
89 | res << int(n - next_n * b) |
90 | n = next_n |
91 | } |
92 | |
93 | if sign == -1 { |
94 | res[res.len - 1] *= sign |
95 | } |
96 | |
97 | if params.reverse { |
98 | res = res.reverse() |
99 | } |
100 | |
101 | return res |
102 | } |
103 | |
104 | // count_digits return the number of digits in the number passed. |
105 | // Number argument accepts any integer type (i8|i16|int|isize|i64) and will be cast to i64 |
106 | pub fn count_digits(number i64) int { |
107 | mut n := number |
108 | if n == 0 { |
109 | return 1 |
110 | } |
111 | mut c := 0 |
112 | for n != 0 { |
113 | n = n / 10 |
114 | c++ |
115 | } |
116 | return c |
117 | } |
118 | |
119 | // minmax returns the minimum and maximum value of the two provided. |
120 | pub fn minmax(a f64, b f64) (f64, f64) { |
121 | if a < b { |
122 | return a, b |
123 | } |
124 | return b, a |
125 | } |
126 | |
127 | // clamp returns x constrained between a and b |
128 | [inline] |
129 | pub fn clamp(x f64, a f64, b f64) f64 { |
130 | if x < a { |
131 | return a |
132 | } |
133 | if x > b { |
134 | return b |
135 | } |
136 | return x |
137 | } |
138 | |
139 | // sign returns the corresponding sign -1.0, 1.0 of the provided number. |
140 | // if n is not a number, its sign is nan too. |
141 | [inline] |
142 | pub fn sign(n f64) f64 { |
143 | // dump(n) |
144 | if is_nan(n) { |
145 | return nan() |
146 | } |
147 | return copysign(1.0, n) |
148 | } |
149 | |
150 | // signi returns the corresponding sign -1, 1 of the provided number. |
151 | [inline] |
152 | pub fn signi(n f64) int { |
153 | return int(copysign(1.0, n)) |
154 | } |
155 | |
156 | // radians converts from degrees to radians. |
157 | [inline] |
158 | pub fn radians(degrees f64) f64 { |
159 | return degrees * (pi / 180.0) |
160 | } |
161 | |
162 | // signbit returns a value with the boolean representation of the sign for x |
163 | [inline] |
164 | pub fn signbit(x f64) bool { |
165 | return f64_bits(x) & sign_mask != 0 |
166 | } |
167 | |
168 | // tolerance checks if a and b difference are less than or equal to the tolerance value |
169 | pub fn tolerance(a f64, b f64, tol f64) bool { |
170 | mut ee := tol |
171 | // Multiplying by ee here can underflow denormal values to zero. |
172 | // Check a==b so that at least if a and b are small and identical |
173 | // we say they match. |
174 | if a == b { |
175 | return true |
176 | } |
177 | mut d := a - b |
178 | if d < 0 { |
179 | d = -d |
180 | } |
181 | // note: b is correct (expected) value, a is actual value. |
182 | // make error tolerance a fraction of b, not a. |
183 | if b != 0 { |
184 | ee = ee * b |
185 | if ee < 0 { |
186 | ee = -ee |
187 | } |
188 | } |
189 | return d < ee |
190 | } |
191 | |
192 | // close checks if a and b are within 1e-14 of each other |
193 | pub fn close(a f64, b f64) bool { |
194 | return tolerance(a, b, 1e-14) |
195 | } |
196 | |
197 | // veryclose checks if a and b are within 4e-16 of each other |
198 | pub fn veryclose(a f64, b f64) bool { |
199 | return tolerance(a, b, 4e-16) |
200 | } |
201 | |
202 | // alike checks if a and b are equal |
203 | pub fn alike(a f64, b f64) bool { |
204 | // eprintln('>>> a: ${f64_bits(a):20} | b: ${f64_bits(b):20} | a==b: ${a == b} | a: ${a:10} | b: ${b:10}') |
205 | // compare a and b, ignoring their last 2 bits: |
206 | if f64_bits(a) & 0xFFFF_FFFF_FFFF_FFFC == f64_bits(b) & 0xFFFF_FFFF_FFFF_FFFC { |
207 | return true |
208 | } |
209 | if a == -0 && b == 0 { |
210 | return true |
211 | } |
212 | if a == 0 && b == -0 { |
213 | return true |
214 | } |
215 | if is_nan(a) && is_nan(b) { |
216 | return true |
217 | } |
218 | if a == b { |
219 | return signbit(a) == signbit(b) |
220 | } |
221 | return false |
222 | } |
223 | |
224 | fn is_odd_int(x f64) bool { |
225 | xi, xf := modf(x) |
226 | return xf == 0 && (i64(xi) & 1) == 1 |
227 | } |
228 | |
229 | fn is_neg_int(x f64) bool { |
230 | if x < 0 { |
231 | _, xf := modf(x) |
232 | return xf == 0 |
233 | } |
234 | return false |
235 | } |