-
Notifications
You must be signed in to change notification settings - Fork 0
/
fun_ieee.c
607 lines (529 loc) · 11.2 KB
/
fun_ieee.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* ReactOS Calc (Math functions, IEEE-754 engine)
*
* Copyright 2007-2017, Carlo Bramini
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "calc.h"
static double validate_rad2angle(double a);
static double validate_angle2rad(calc_number_t *c);
void apply_int_mask(calc_number_t *r)
{
unsigned __int64 mask;
switch (calc.size) {
case IDC_RADIO_QWORD:
mask = _UI64_MAX;
break;
case IDC_RADIO_DWORD:
mask = ULONG_MAX;
break;
case IDC_RADIO_WORD:
mask = USHRT_MAX;
break;
case IDC_RADIO_BYTE:
mask = UCHAR_MAX;
break;
default:
mask = (unsigned __int64)-1;
}
r->i &= mask;
}
double asinh(double x)
{
return log(x+sqrt(x*x+1));
}
double acosh(double x)
{
// must be x>=1, if not return Nan (Not a Number)
if(!(x>=1.0)) return sqrt(-1.0);
// return only the positive result (as sqrt does).
return log(x+sqrt(x*x-1.0));
}
double atanh(double x)
{
// must be x>-1, x<1, if not return Nan (Not a Number)
if(!(x>-1.0 && x<1.0)) return sqrt(-1.0);
return log((1.0+x)/(1.0-x))/2.0;
}
static double validate_rad2angle(double a)
{
switch (calc.degr) {
case IDC_RADIO_DEG:
a = a * (180.0/CALC_PI);
break;
case IDC_RADIO_RAD:
break;
case IDC_RADIO_GRAD:
a = a * (200.0/CALC_PI);
break;
}
return a;
}
static double validate_angle2rad(calc_number_t *c)
{
switch (calc.degr) {
case IDC_RADIO_DEG:
c->f = c->f * (CALC_PI/180.0);
break;
case IDC_RADIO_RAD:
break;
case IDC_RADIO_GRAD:
c->f = c->f * (CALC_PI/200.0);
break;
}
return c->f;
}
void rpn_sin(calc_number_t *c)
{
double angle = validate_angle2rad(c);
if (angle == 0 || angle == CALC_PI)
c->f = 0;
else
if (angle == CALC_3_PI_2)
c->f = -1;
else
if (angle == CALC_2_PI)
c->f = 1;
else
c->f = sin(angle);
}
void rpn_cos(calc_number_t *c)
{
double angle = validate_angle2rad(c);
if (angle == CALC_PI_2 || angle == CALC_3_PI_2)
c->f = 0;
else
if (angle == CALC_PI)
c->f = -1;
else
if (angle == CALC_2_PI)
c->f = 1;
else
c->f = cos(angle);
}
void rpn_tan(calc_number_t *c)
{
double angle = validate_angle2rad(c);
if (angle == CALC_PI_2 || angle == CALC_3_PI_2)
calc.is_nan = TRUE;
else
if (angle == CALC_PI || angle == CALC_2_PI)
c->f = 0;
else
c->f = tan(angle);
}
void rpn_asin(calc_number_t *c)
{
c->f = validate_rad2angle(asin(c->f));
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_acos(calc_number_t *c)
{
c->f = validate_rad2angle(acos(c->f));
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_atan(calc_number_t *c)
{
c->f = validate_rad2angle(atan(c->f));
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_sinh(calc_number_t *c)
{
c->f = sinh(c->f);
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_cosh(calc_number_t *c)
{
c->f = cosh(c->f);
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_tanh(calc_number_t *c)
{
c->f = tanh(c->f);
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_asinh(calc_number_t *c)
{
c->f = asinh(c->f);
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_acosh(calc_number_t *c)
{
c->f = acosh(c->f);
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_atanh(calc_number_t *c)
{
c->f = atanh(c->f);
if (_isnan(c->f))
calc.is_nan = TRUE;
}
void rpn_int(calc_number_t *c)
{
double int_part;
modf(calc.code.f, &int_part);
c->f = int_part;
}
void rpn_frac(calc_number_t *c)
{
double int_part;
c->f = modf(calc.code.f, &int_part);
}
void rpn_reci(calc_number_t *c)
{
if (c->f == 0)
calc.is_nan = TRUE;
else
c->f = 1./c->f;
}
void rpn_fact(calc_number_t *c)
{
double fact, mult, num;
if (calc.base == IDC_RADIO_DEC)
num = c->f;
else
num = (double)c->i;
if (num > 1000) {
calc.is_nan = TRUE;
return;
}
if (num < 0) {
calc.is_nan = TRUE;
return;
} else
if (num == 0)
fact = 1;
else {
rpn_int(c);
fact = 1;
mult = 2;
while (mult <= num) {
fact *= mult;
mult++;
}
c->f = fact;
}
if (_finite(fact) == 0)
calc.is_nan = TRUE;
else
if (calc.base == IDC_RADIO_DEC)
c->f = fact;
else
c->i = (__int64)fact;
}
__int64 logic_dbl2int(calc_number_t *a)
{
double int_part;
int width;
modf(a->f, &int_part);
width = (int_part==0) ? 1 : (int)log10(fabs(int_part))+1;
if (width > 63) {
calc.is_nan = TRUE;
return 0;
}
return (__int64)int_part;
}
double logic_int2dbl(calc_number_t *a)
{
return (double)a->i;
}
void rpn_not(calc_number_t *c)
{
if (calc.base == IDC_RADIO_DEC) {
calc_number_t n;
n.i = logic_dbl2int(c);
c->f = (long double)(~n.i);
} else
c->i = ~c->i;
}
void rpn_pi(calc_number_t *c)
{
c->f = CALC_PI;
}
void rpn_2pi(calc_number_t *c)
{
c->f = CALC_PI*2;
}
void rpn_sign(calc_number_t *c)
{
if (calc.base == IDC_RADIO_DEC)
c->f = 0-c->f;
else
c->i = 0-c->i;
}
void rpn_exp2(calc_number_t *c)
{
if (calc.base == IDC_RADIO_DEC) {
c->f *= c->f;
if (_finite(c->f) == 0)
calc.is_nan = TRUE;
} else
c->i *= c->i;
}
void rpn_exp3(calc_number_t *c)
{
if (calc.base == IDC_RADIO_DEC) {
c->f = pow(c->f, 3.);
if (_finite(c->f) == 0)
calc.is_nan = TRUE;
} else
c->i *= (c->i*c->i);
}
static __int64 myabs64(__int64 number)
{
return (number < 0) ? 0-number : number;
}
static unsigned __int64 sqrti(unsigned __int64 number)
{
/* modified form of Newton's method for approximating roots */
#define NEXT(n, i) (((n) + (i)/(n)) >> 1)
unsigned __int64 n, n1;
#ifdef __GNUC__
if (number == 0xffffffffffffffffULL)
#else
if (number == 0xffffffffffffffffUI64)
#endif
return 0xffffffff;
n = 1;
n1 = NEXT(n, number);
while (myabs64(n1 - n) > 1) {
n = n1;
n1 = NEXT(n, number);
}
while((n1*n1) > number)
n1--;
return n1;
#undef NEXT
}
void rpn_sqrt(calc_number_t *c)
{
if (calc.base == IDC_RADIO_DEC) {
if (c->f < 0)
calc.is_nan = TRUE;
else
c->f = sqrt(c->f);
} else {
c->i = sqrti(c->i);
}
}
static __int64 cbrti(__int64 x) {
__int64 s, y, b;
s = 60;
y = 0;
while(s >= 0) {
y = 2*y;
b = (3*y*(y + 1) + 1) << s;
s = s - 3;
if (x >= b) {
x = x - b;
y = y + 1;
}
}
return y;
}
void rpn_cbrt(calc_number_t *c)
{
if (calc.base == IDC_RADIO_DEC)
#if defined(__GNUC__) && !defined(__REACTOS__)
c->f = cbrt(c->f);
#else
c->f = pow(c->f,1./3.);
#endif
else {
c->i = cbrti(c->i);
}
}
void rpn_exp(calc_number_t *c)
{
c->f = exp(c->f);
if (_finite(c->f) == 0)
calc.is_nan = TRUE;
}
void rpn_exp10(calc_number_t *c)
{
double int_part;
modf(c->f, &int_part);
if (fmod(int_part, 2.) == 0.)
calc.is_nan = TRUE;
else {
c->f = pow(10., c->f);
if (_finite(c->f) == 0)
calc.is_nan = TRUE;
}
}
void rpn_ln(calc_number_t *c)
{
if (c->f <= 0)
calc.is_nan = TRUE;
else
c->f = log(c->f);
}
void rpn_log(calc_number_t *c)
{
if (c->f <= 0)
calc.is_nan = TRUE;
else
c->f = log10(c->f);
}
static double stat_sum(void)
{
double sum = 0;
statistic_t *p = calc.stat;
while (p != NULL) {
if (p->base == IDC_RADIO_DEC)
sum += p->num.f;
else
sum += p->num.i;
p = (statistic_t *)(p->next);
}
return sum;
}
static double stat_sum2(void)
{
double sum = 0;
statistic_t *p = calc.stat;
while (p != NULL) {
if (p->base == IDC_RADIO_DEC)
sum += p->num.f * p->num.f;
else
sum += (double)p->num.i * (double)p->num.i;
p = (statistic_t *)(p->next);
}
return sum;
}
void rpn_ave(calc_number_t *c)
{
double ave = 0;
int n;
ave = stat_sum();
n = SendDlgItemMessage(calc.hStatWnd, IDC_LIST_STAT, LB_GETCOUNT, 0, 0);
if (n)
ave = ave / (double)n;
if (calc.base == IDC_RADIO_DEC)
c->f = ave;
else
c->i = (__int64)ave;
}
void rpn_ave2(calc_number_t *c)
{
double ave = 0;
int n;
ave = stat_sum2();
n = SendDlgItemMessage(calc.hStatWnd, IDC_LIST_STAT, LB_GETCOUNT, 0, 0);
if (n)
ave = ave / (double)n;
if (calc.base == IDC_RADIO_DEC)
c->f = ave;
else
c->i = (__int64)ave;
}
void rpn_sum(calc_number_t *c)
{
double sum = stat_sum();
if (calc.base == IDC_RADIO_DEC)
c->f = sum;
else
c->i = (__int64)sum;
}
void rpn_sum2(calc_number_t *c)
{
double sum = stat_sum2();
if (calc.base == IDC_RADIO_DEC)
c->f = sum;
else
c->i = (__int64)sum;
}
static void rpn_s_ex(calc_number_t *c, int pop_type)
{
double ave = 0;
double n = 0;
double dev = 0;
double num = 0;
statistic_t *p = calc.stat;
ave = stat_sum();
n = (double)SendDlgItemMessage(calc.hStatWnd, IDC_LIST_STAT, LB_GETCOUNT, 0, 0);
if (n == 0) {
c->f = 0;
return;
}
ave = ave / n;
dev = 0;
p = calc.stat;
while (p != NULL) {
if (p->base == IDC_RADIO_DEC)
num = p->num.f;
else
num = (double)p->num.i;
dev += pow(num-ave, 2.);
p = (statistic_t *)(p->next);
}
dev = sqrt(dev/(pop_type ? n-1 : n));
if (calc.base == IDC_RADIO_DEC)
c->f = dev;
else
c->i = (__int64)dev;
}
void rpn_s(calc_number_t *c)
{
rpn_s_ex(c, 0);
}
void rpn_s_m1(calc_number_t *c)
{
rpn_s_ex(c, 1);
}
void rpn_dms2dec(calc_number_t *c)
{
double d, m, s;
m = modf(c->f, &d) * 100;
s = (modf(m, &m) * 100)+.5;
modf(s, &s);
m = m/60;
s = s/3600;
c->f = d + m + s;
}
void rpn_dec2dms(calc_number_t *c)
{
double d, m, s;
m = modf(c->f, &d) * 60;
s = ceil(modf(m, &m) * 60);
c->f = d + m/100. + s/10000.;
}
void rpn_zero(calc_number_t *c)
{
c->f = 0;
}
void rpn_copy(calc_number_t *dst, calc_number_t *src)
{
*dst = *src;
}
int rpn_is_zero(calc_number_t *c)
{
return (c->f == 0);
}
void rpn_alloc(calc_number_t *c)
{
}
void rpn_free(calc_number_t *c)
{
}