ReactOS  0.4.14-dev-49-gfb4591c
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 
23 static double validate_rad2angle(double a);
24 static double validate_angle2rad(calc_number_t *c);
25 
27 {
28  unsigned __int64 mask;
29 
30  switch (calc.size) {
31  case IDC_RADIO_QWORD:
32  mask = _UI64_MAX;
33  break;
34  case IDC_RADIO_DWORD:
35  mask = ULONG_MAX;
36  break;
37  case IDC_RADIO_WORD:
38  mask = USHRT_MAX;
39  break;
40  case IDC_RADIO_BYTE:
41  mask = UCHAR_MAX;
42  break;
43  default:
44  mask = (unsigned __int64)-1;
45  }
46  r->i &= mask;
47 }
48 
49 double asinh(double x)
50 {
51  return log(x+sqrt(x*x+1));
52 }
53 
54 double 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 
63 double 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 
71 static 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 
333 static 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 
437 static double stat_sum(void)
438 {
439  double sum = 0;
440  statistic_t *p = calc.stat;
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 
452 static double stat_sum2(void)
453 {
454  double sum = 0;
455  statistic_t *p = calc.stat;
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 
519 static 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;
525  statistic_t *p = calc.stat;
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 sqrt(const complex< float > &)
Definition: complex.cpp:188
_STLP_DECLSPEC complex< float > _STLP_CALL sinh(const complex< float > &)
static double validate_angle2rad(calc_number_t *c)
Definition: fun_ieee.c:86
_Check_return_ _CRTIMP double __cdecl ceil(_In_ double x)
statistic_t * stat
Definition: calc.h:181
DWORD base
Definition: calc.h:189
static double stat_sum(void)
Definition: fun_ieee.c:437
GLint GLint GLsizei width
Definition: gl.h:1546
#define IDC_RADIO_DEG
Definition: resource.h:86
#define CALC_3_PI_2
Definition: calc.h:213
#define TRUE
Definition: types.h:120
_Check_return_ __MINGW_NOTHROW _CRTIMP int __cdecl _isnan(_In_ double)
_STLP_DECLSPEC complex< float > _STLP_CALL cosh(const complex< float > &)
void rpn_sinh(calc_number_t *c)
Definition: fun_ieee.c:163
void rpn_sqrt(calc_number_t *c)
Definition: fun_ieee.c:358
calc_number_t code
Definition: calc.h:178
#define IDC_RADIO_GRAD
Definition: resource.h:88
#define _UI64_MAX
Definition: limits.h:63
#define IDC_RADIO_DEC
Definition: resource.h:22
void rpn_acos(calc_number_t *c)
Definition: fun_ieee.c:150
void rpn_frac(calc_number_t *c)
Definition: fun_ieee.c:209
#define NEXT(n, i)
double f
Definition: calc.h:126
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define IDC_RADIO_WORD
Definition: resource.h:84
void rpn_alloc(calc_number_t *c)
Definition: fun_ieee.c:601
void apply_int_mask(calc_number_t *r)
Definition: fun_ieee.c:26
GLdouble n
Definition: glext.h:7729
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
void rpn_asinh(calc_number_t *c)
Definition: fun_ieee.c:182
double __cdecl cbrt(double)
DWORD degr
Definition: calc.h:191
void rpn_atanh(calc_number_t *c)
Definition: fun_ieee.c:194
#define SendDlgItemMessage
Definition: winuser.h:5743
GLfloat angle
Definition: glext.h:10853
void rpn_tanh(calc_number_t *c)
Definition: fun_ieee.c:175
void rpn_exp10(calc_number_t *c)
Definition: fun_ieee.c:407
BOOL is_nan
Definition: calc.h:183
void rpn_exp3(calc_number_t *c)
Definition: fun_ieee.c:318
void rpn_tan(calc_number_t *c)
Definition: fun_ieee.c:131
void rpn_exp(calc_number_t *c)
Definition: fun_ieee.c:400
double asinh(double x)
Definition: fun_ieee.c:49
const GLfloat * m
Definition: glext.h:10848
static int dev
Definition: mkdosfs.c:536
void rpn_sum(calc_number_t *c)
Definition: fun_ieee.c:499
valarray< _Tp > asin(const valarray< _Tp > &__x)
Definition: _valarray.h:910
#define USHRT_MAX
Definition: limits.h:38
void rpn_cbrt(calc_number_t *c)
Definition: fun_ieee.c:387
void rpn_2pi(calc_number_t *c)
Definition: fun_ieee.c:295
valarray< _Tp > acos(const valarray< _Tp > &__x)
Definition: _valarray.h:901
_STLP_DECLSPEC complex< float > _STLP_CALL tanh(const complex< float > &)
void rpn_reci(calc_number_t *c)
Definition: fun_ieee.c:216
GLenum GLint GLuint mask
Definition: glext.h:6028
#define IDC_RADIO_QWORD
Definition: resource.h:82
float pow(float __x, int __y)
Definition: _cmath.h:458
#define a
Definition: ke_i.h:78
static size_t double number
Definition: printf.c:64
void rpn_atan(calc_number_t *c)
Definition: fun_ieee.c:156
void rpn_cosh(calc_number_t *c)
Definition: fun_ieee.c:169
DWORD size
Definition: calc.h:190
static const char mbstate_t *static wchar_t const char mbstate_t *static const wchar_t int *static double
Definition: string.c:80
_STLP_DECLSPEC complex< float > _STLP_CALL cos(const complex< float > &)
smooth NULL
Definition: ftsmooth.c:416
double acosh(double x)
Definition: fun_ieee.c:54
void rpn_s_m1(calc_number_t *c)
Definition: fun_ieee.c:558
void rpn_sum2(calc_number_t *c)
Definition: fun_ieee.c:509
#define LB_GETCOUNT
Definition: winuser.h:2020
static int sum(int x_, int y_)
Definition: ptr2_test.cpp:35
#define b
Definition: ke_i.h:79
void rpn_sign(calc_number_t *c)
Definition: fun_ieee.c:300
#define CALC_2_PI
Definition: calc.h:214
#define CALC_PI_2
Definition: calc.h:211
static __int64 myabs64(__int64 number)
Definition: fun_ieee.c:328
_Check_return_ double __cdecl fmod(_In_ double x, _In_ double y)
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
#define d
Definition: ke_i.h:81
if(!(yy_init))
Definition: macro.lex.yy.c:714
void rpn_int(calc_number_t *c)
Definition: fun_ieee.c:201
_Check_return_ __MINGW_NOTHROW _CRTIMP int __cdecl _finite(_In_ double)
void rpn_acosh(calc_number_t *c)
Definition: fun_ieee.c:188
const GLubyte * c
Definition: glext.h:8905
_STLP_DECLSPEC complex< float > _STLP_CALL log10(const complex< float > &)
Definition: complex.cpp:230
GLuint GLuint num
Definition: glext.h:9618
int n1
Definition: dwarfget.c:148
valarray< _Tp > atan(const valarray< _Tp > &__x)
Definition: _valarray.h:919
__int64 logic_dbl2int(calc_number_t *a)
Definition: fun_ieee.c:261
#define CALC_PI
Definition: calc.h:212
void rpn_copy(calc_number_t *dst, calc_number_t *src)
Definition: fun_ieee.c:591
GLdouble s
Definition: gl.h:2039
void rpn_ave(calc_number_t *c)
Definition: fun_ieee.c:467
GLenum src
Definition: glext.h:6340
static void rpn_s_ex(calc_number_t *c, int pop_type)
Definition: fun_ieee.c:519
static double validate_rad2angle(double a)
Definition: fun_ieee.c:71
void rpn_pi(calc_number_t *c)
Definition: fun_ieee.c:290
_Check_return_ _CRT_JIT_INTRINSIC double __cdecl fabs(_In_ double x)
int rpn_is_zero(calc_number_t *c)
Definition: fun_ieee.c:596
double logic_int2dbl(calc_number_t *a)
Definition: fun_ieee.c:275
calc_number_t num
Definition: calc.h:155
void rpn_ave2(calc_number_t *c)
Definition: fun_ieee.c:483
HWND hStatWnd
Definition: calc.h:193
#define UCHAR_MAX
Definition: limits.h:25
GLenum GLenum dst
Definition: glext.h:6340
#define IDC_RADIO_DWORD
Definition: resource.h:83
void rpn_s(calc_number_t *c)
Definition: fun_ieee.c:553
#define IDC_LIST_STAT
Definition: resource.h:96
DWORD exp
Definition: msg.c:15681
void rpn_sin(calc_number_t *c)
Definition: fun_ieee.c:101
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
void rpn_ln(calc_number_t *c)
Definition: fun_ieee.c:421
static __int64 cbrti(__int64 x)
Definition: fun_ieee.c:370
void rpn_asin(calc_number_t *c)
Definition: fun_ieee.c:144
#define IDC_RADIO_RAD
Definition: resource.h:87
void rpn_free(calc_number_t *c)
Definition: fun_ieee.c:605
void rpn_exp2(calc_number_t *c)
Definition: fun_ieee.c:308
static unsigned __int64 sqrti(unsigned __int64 number)
Definition: fun_ieee.c:333
double atanh(double x)
Definition: fun_ieee.c:63
static double stat_sum2(void)
Definition: fun_ieee.c:452
_STLP_DECLSPEC complex< float > _STLP_CALL sin(const complex< float > &)
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
void rpn_not(calc_number_t *c)
Definition: fun_ieee.c:280
GLfloat GLfloat p
Definition: glext.h:8902
_STLP_DECLSPEC complex< float > _STLP_CALL tan(const complex< float > &)
calc_t calc
Definition: winmain.c:247
#define log(outFile, fmt,...)
Definition: util.h:15
void rpn_log(calc_number_t *c)
Definition: fun_ieee.c:429
void rpn_dec2dms(calc_number_t *c)
Definition: fun_ieee.c:577
void rpn_dms2dec(calc_number_t *c)
Definition: fun_ieee.c:563
void rpn_zero(calc_number_t *c)
Definition: fun_ieee.c:586
#define ULONG_MAX
Definition: limits.h:44
#define __int64
Definition: basetyps.h:16
#define IDC_RADIO_BYTE
Definition: resource.h:85
_Check_return_ _CRTIMP double __cdecl modf(_In_ double x, _Out_ double *y)
void rpn_cos(calc_number_t *c)
Definition: fun_ieee.c:116
void rpn_fact(calc_number_t *c)
Definition: fun_ieee.c:224