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