Debian Codegraph @

common/core_math2.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include "core_globals.h"
19 #include "core_math2.h"
20
21 phloat math_random() {
22 if (random_number_low == 0 && random_number_high == 0) {
23 random_number_high = 0;
24 random_number_low = 2787;
25 // The RPL RAND/RDZ functions differ from the HP-42S RAN/SEED
26 // only in their initial seed, which is this:
27 // random_number_high = 9995003;
28 // random_number_low = 33083533;
29 }
30 int8 temp = random_number_low * 30928467;
31 random_number_high = (random_number_low * 28511 + random_number_high * 30928467 + temp / 100000000) % 10000000;
32 random_number_low = temp % 100000000;
33 if (random_number_high >= 1000000) {
34 temp = random_number_low / 1000;
35 #ifdef BCD_MATH
36 const Phloat n1(1000000000000LL);
37 const Phloat n2(10000000);
38 return Phloat(temp) / n1 + Phloat(random_number_high) / n2;
39 #else
40 return temp / 1000000000000.0 + random_number_high / 10000000.0;
41 #endif
42 } else if (random_number_high >= 100000) {
43 temp = random_number_low / 100;
44 #ifdef BCD_MATH
45 const Phloat n1(10000000000000LL);
46 const Phloat n2(10000000);
47 return Phloat(temp) / n1 + Phloat(random_number_high) / n2;
48 #else
49 return temp / 10000000000000.0 + random_number_high / 10000000.0;
50 #endif
51 } else if (random_number_high >= 10000) {
52 temp = random_number_low / 10;
53 #ifdef BCD_MATH
54 const Phloat n1(100000000000000LL);
55 const Phloat n2(10000000);
56 return Phloat(temp) / n1 + Phloat(random_number_high) / n2;
57 #else
58 return temp / 100000000000000.0 + random_number_high / 10000000.0;
59 #endif
60 } else {
61 #ifdef BCD_MATH
62 const Phloat n1(1000000000000000LL);
63 const Phloat n2(10000000);
64 return Phloat(random_number_low) / n1 + Phloat(random_number_high) / n2;
65 #else
66 return random_number_low / 1000000000000000.0 + random_number_high / 10000000.0;
67 #endif
68 }
69 }
70
71 int math_tan(phloat x, phloat *y, bool rad) {
72 if (rad || flags.f.rad) {
73 *y = tan(x);
74 } else if (flags.f.grad) {
75 bool neg = false;
76 if (x < 0) {
77 x = -x;
78 neg = true;
79 }
80 // [0 200[
81 x = fmod(x, 200);
82 if (x == 100)
83 goto infinite;
84 // TAN(x+100gon) = -TAN(100gon-x)
85 if (x > 100) {
86 x = 200 - x;
87 neg = !neg;
88 }
89 // to improve accuracy for x close to 100gon
90 if (x > 89)
91 *y = 1 / tan((100 - x) / (200 / PI));
92 else
93 *y = tan(x / (200 / PI));
94 if (neg)
95 *y = -(*y);
96 } else {
97 bool neg = false;
98 if (x < 0) {
99 x = -x;
100 neg = true;
101 }
102 // [0 180[
103 x = fmod(x, 180);
104 if (x == 90)
105 goto infinite;
106 // TAN(x+90°) = -TAN(90°-x)
107 if (x > 90) {
108 x = 180 - x;
109 neg = !neg;
110 }
111 // to improve accuracy for x close to 90°
112 if (x > 80)
113 *y = 1 / tan((90 - x) / (180 / PI));
114 else
115 *y = tan(x / (180 / PI));
116 if (neg)
117 *y = -(*y);
118 }
119 if (p_isnan(*y) || p_isinf(*y) != 0) {
120 infinite:
121 if (flags.f.range_error_ignore)
122 *y = POS_HUGE_PHLOAT;
123 else
124 return ERR_OUT_OF_RANGE;
125 }
126 return ERR_NONE;
127 }
128
129 int math_asinh(phloat xre, phloat xim, phloat *yre, phloat *yim) {
130
131 if (xim == 0) {
132 *yre = asinh(xre);
133 *yim = 0;
134 return ERR_NONE;
135 } else if (xre == 0) {
136 if (xim > 1) {
137 *yre = acosh(xim);
138 *yim = PI / 2;
139 } else if (xim < -1) {
140 *yre = -acosh(-xim);
141 *yim = -PI / 2;
142 } else {
143 *yre = 0;
144 *yim = asin(xim);
145 }
146 return ERR_NONE;
147 }
148
149 // Calculating asinh(x) as i * asin(x / i)
150 phloat zre = xim;
151 phloat zim = -xre;
152
153 phloat are, aim, bre, bim;
154 math_sqrt(zre + 1, zim, &are, &aim);
155 math_sqrt(-zre + 1, -zim, &bre, &bim);
156 phloat x2 = atan(zre / (are * bre - aim * bim));
157 phloat y2 = asinh(are * bim - aim * bre);
158
159 *yre = -copysign(y2, zim);
160 *yim = copysign(x2, zre);
161 return ERR_NONE;
162 }
163
164 int math_acosh(phloat xre, phloat xim, phloat *yre, phloat *yim) {
165
166 if (xim == 0) {
167 if (xre >= 1) {
168 *yre = acosh(xre);
169 *yim = 0;
170 } else if (xre <= -1) {
171 *yre = acosh(-xre);
172 *yim = PI;
173 } else {
174 *yre = 0;
175 *yim = acos(xre);
176 }
177 return ERR_NONE;
178 } else if (xre == 0) {
179 if (xim > 0) {
180 *yre = asinh(xim);
181 *yim = PI / 2;
182 } else {
183 *yre = -asinh(xim);
184 *yim = -PI / 2;
185 }
186 return ERR_NONE;
187 }
188
189 phloat are, aim, bre, bim;
190
191 /* a = sqrt(x - 1) */
192 math_sqrt(xre - 1, xim, &are, &aim);
193 /* b = sqrt(x + 1) */
194 math_sqrt(xre + 1, xim, &bre, &bim);
195
196 *yre = asinh(are * bre + aim * bim);
197 *yim = atan(aim / bre) * 2;
198
199 return ERR_NONE;
200 }
201
202 int math_atanh(phloat xre, phloat xim, phloat *yre, phloat *yim) {
203
204 if (xim == 0) {
205 if (xre == 1 || xre == -1)
206 return ERR_INVALID_DATA;
207 else if (xre > -1 && xre < 1) {
208 *yre = atanh(xre);
209 *yim = 0;
210 return ERR_NONE;
211 } else {
212 *yre = atanh(1 / xre);
213 if (xre > 1)
214 *yim = -PI / 2;
215 else
216 *yim = PI / 2;
217 return ERR_NONE;
218 }
219 } else if (xre == 0) {
220 *yre = 0;
221 *yim = atan(xim);
222 return ERR_NONE;
223 }
224
225 phloat x = fabs(xim);
226 phloat y = fabs(xre);
227
228 #ifdef BCD_MATH
229 const Phloat BIG(1000000000000000000LL);
230 #else
231 const double BIG = 0x8000000; // 2^27
232 #endif
233
234 if (x >= BIG || y >= BIG) { // atan(1/z) ~= 1/z
235 math_inv(x, y, &x, &y); // atan(x+y*I) ~= pi/2 + t
236 x += PI / 2; // |re(t^3/3)/re(t)| <= 0x1p-54
237 // |im(t^3/3)/im(t)| <= 0x1p-54
238 } else {
239 phloat x2 = x * x;
240 phloat ym = 1 - y;
241 x = atan2(2 * x, (1 + y) * ym - x2) / 2;
242 y = log1p(4 * y / (ym * ym + x2)) / 4;
243 }
244
245 x = fabs(x);
246 if (xim < 0)
247 x = -x;
248 y = fabs(y);
249 if (xre < 0)
250 y = -y;
251
252 *yre = y;
253 *yim = x;
254 return ERR_NONE;
255 }
256
257 int math_ln(phloat xre, phloat xim, phloat *yre, phloat *yim) {
258 if (xim == 0) {
259 if (xre == 0)
260 return ERR_INVALID_DATA;
261 if (xre > 0) {
262 *yre = log(xre);
263 *yim = 0;
264 } else {
265 *yre = log(-xre);
266 *yim = PI;
267 }
268 return ERR_NONE;
269 } else if (xre == 0) {
270 if (xim > 0) {
271 *yre = log(xim);
272 *yim = PI / 2;
273 } else {
274 *yre = log(-xim);
275 *yim = -PI / 2;
276 }
277 return ERR_NONE;
278 } else {
279 phloat h = xre * xre + xim * xim;
280 phloat a = atan2(xim, xre);
281 if (h > 0.5 && h < 3.0) {
282 if ((h = fabs(xre)) < (xim = fabs(xim))) {
283 h = xim;
284 xim = xre;
285 }
286 h -= 1; /* x^2-1 = 2*(x-1) + (x-1)^2 */
287 h = log1p(2 * h + h * h + xim * xim) / 2;
288 } else if (p_isnormal(h)) {
289 h = log(h) / 2;
290 } else {
291 #ifdef BCD_MATH
292 phloat m = scalbn(phloat(1), 38);
293 phloat b = -log(phloat(10)) * 38;
294 #else
295 phloat m = 0x2000000000000000; // 2^61
296 phloat b = -log(phloat(2)) * 61;
297 #endif
298 if (p_isinf(h)) {
299 m = 1 / m;
300 b = -b;
301 }
302 h = log(hypot(m * xre, m * xim)) + b;
303 }
304 *yre = h;
305 *yim = a;
306 return ERR_NONE;
307 }
308 }
309
310 int math_sqrt(phloat xre, phloat xim, phloat *yre, phloat *yim) {
311 if (xre == 0) {
312 if (xim == 0) {
313 *yre = 0;
314 *yim = 0;
315 } else {
316 bool neg = xim < 0;
317 if (neg)
318 xim = -xim;
319 phloat r;
320 if (xim > 1)
321 r = sqrt(xim / 2);
322 else
323 r = sqrt(xim * 2) / 2;
324 *yre = r;
325 *yim = neg ? -r : r;
326 }
327 return ERR_NONE;
328 } else if (xim == 0) {
329 if (xre > 0) {
330 *yre = sqrt(xre);
331 *yim = 0;
332 } else {
333 *yre = 0;
334 *yim = sqrt(-xre);
335 }
336 return ERR_NONE;
337 }
338
339 phloat r = hypot(xre, xim);
340 phloat a = sqrt((r + fabs(xre)) / 2);
341 phloat b = xim / (a * 2);
342
343 if (p_isinf(a)) {
344 xre /= 100;
345 xim /= 100;
346 r = hypot(xre, xim);
347 a = sqrt((r + fabs(xre)) / 2);
348 b = xim / (a * 2);
349 a *= 10;
350 b *= 10;
351 }
352
353 if (xre >= 0) {
354 *yre = a;
355 *yim = b;
356 } else if (xim >= 0) {
357 *yre = b;
358 *yim = a;
359 } else {
360 *yre = -b;
361 *yim = -a;
362 }
363 return ERR_NONE;
364 }
365
366 int math_inv(phloat xre, phloat xim, phloat *yre, phloat *yim) {
367 phloat r, t, rre, rim;
368 int inf;
369 if (xre == 0) {
370 if (xim == 0)
371 return ERR_DIVIDE_BY_0;
372 else {
373 rre = 0;
374 rim = -1 / xim;
375 }
376 } else {
377 if (xim == 0) {
378 rre = 1 / xre;
379 rim = 0;
380 } else {
381 if (fabs(xim) <= fabs(xre)) {
382 r = xim / xre;
383 t = 1 / (xre + xim * r);
384 if (r == 0) {
385 rre = t;
386 rim = -xim * (1 / xre) * t;
387 } else {
388 rre = t;
389 rim = -r * t;
390 }
391 } else {
392 r = xre / xim;
393 t = 1 / (xre * r + xim);
394 if (r == 0) {
395 rre = xre * (1 / xim) * t;
396 rim = -t;
397 } else {
398 rre = r * t;
399 rim = -t;
400 }
401 }
402 }
403 }
404 inf = p_isinf(rre);
405 if (inf != 0) {
406 if (flags.f.range_error_ignore)
407 rre = inf == 1 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT;
408 else
409 return ERR_OUT_OF_RANGE;
410 }
411 inf = p_isinf(rim);
412 if (inf != 0) {
413 if (flags.f.range_error_ignore)
414 rim = inf == 1 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT;
415 else
416 return ERR_OUT_OF_RANGE;
417 }
418 *yre = rre;
419 *yim = rim;
420 return ERR_NONE;
421 }