• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/lib/math.c

  1 /*
  2  * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 /**
 18  * # Mathematical Functions
 19  *
 20  * The `math` module bundles various mathematical and trigonometrical functions.
 21  *
 22  * Functions can be individually imported and directly accessed using the
 23  * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#named_import named import}
 24  * syntax:
 25  *
 26  *   ```
 27  *   import { pow, rand } from 'math';
 28  *
 29  *   let x = pow(2, 5);
 30  *   let y = rand();
 31  *   ```
 32  *
 33  * Alternatively, the module namespace can be imported
 34  * using a wildcard import statement:
 35  *
 36  *   ```
 37  *   import * as math from 'math';
 38  *
 39  *   let x = math.pow(2, 5);
 40  *   let y = math.rand();
 41  *   ```
 42  *
 43  * Additionally, the math module namespace may also be imported by invoking the
 44  * `ucode` interpreter with the `-lmath` switch.
 45  * 
 46  * It should be noted that when the ucode interpreter is run as `-p "..."`,
 47  * values involving Infinity are returned as the max double precision value
 48  * +/-1e309 (JSON), whereas when run as `-e "print(...)"` Infinity is
 49  * represented by the string `Infinity`. The boolean check `isinf()` is
 50  * available to determine Infinity values.
 51  *
 52  * @module math
 53  */
 54 
 55 #include <math.h>
 56 #include <errno.h>
 57 #include <sys/time.h>
 58 
 59 #include "ucode/module.h"
 60 
 61 #ifndef M_PI
 62 #define M_PI   3.14159265358979323846264338327950288
 63 #endif
 64 #ifndef M_E
 65 #define M_E    2.71828182845904523536028747135266250
 66 #endif
 67 #ifndef M_SQRT2
 68 #define M_SQRT2 1.41421356237309504880168872420969808
 69 #endif
 70 #ifndef M_SQRT1_2
 71 #define M_SQRT1_2 0.70710678118654752440084436210484904
 72 #endif
 73 #ifndef M_LN2
 74 #define M_LN2 0.693147180559945309417232121458176568
 75 #endif
 76 #ifndef M_LN10
 77 #define M_LN10 2.302585092994045684017991454684364208
 78 #endif
 79 #ifndef M_LOG2_10
 80 #define M_LOG2_10 3.321928094887362347870319429489390529
 81 #endif
 82 #ifndef M_LOG10_2
 83 #define M_LOG10_2 0.30102999566398119521373890567845649694
 84 #endif
 85 #ifndef M_PI_2
 86 #define M_PI_2 1.570796326794896619231321691639751442
 87 #endif
 88 #ifndef M_PI_4
 89 #define M_PI_4 0.785398163397448309615660845819875721
 90 #endif
 91 #ifndef M_1_PI
 92 #define M_1_PI 0.318309886183790671537767526745028724069
 93 #endif
 94 #ifndef M_2_PI
 95 #define M_2_PI 0.636619772367581343075535053490057448138
 96 #endif
 97 #ifndef M_2_SQRTPI
 98 #define M_2_SQRTPI 1.12837916709551257389615890312154517
 99 #endif
100 #ifndef M_LOG2E
101 #define M_LOG2E 1.44269504088896340735992468100189214
102 #endif
103 #ifndef M_LOG10E
104 #define M_LOG10E 0.434294481903251827651128918916605082
105 #endif
106 
107 #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
108 #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
109 
110 
111 /**
112  * Returns the absolute value of the given numeric value.
113  *
114  * @function module:math#abs
115  *
116  * @param {*} number
117  * The number to return the absolute value for.
118  *
119  * @returns {number}
120  * Returns the absolute value or `NaN` if the given argument could
121  * not be converted to a number.
122  */
123 static uc_value_t *
124 uc_abs(uc_vm_t *vm, size_t nargs)
125 {
126         uc_value_t *v = uc_fn_arg(0), *nv, *res;
127         int64_t n;
128         double d;
129 
130         nv = v ? ucv_to_number(v) : NULL;
131 
132         switch (ucv_type(nv)) {
133         case UC_INTEGER:
134                 n = ucv_int64_get(nv);
135 
136                 if (n >= 0 || errno == ERANGE)
137                         res = ucv_get(nv);
138                 else if (n == INT64_MIN)
139                         res = ucv_uint64_new((uint64_t)INT64_MAX + 1);
140                 else
141                         res = ucv_uint64_new(-n);
142 
143                 break;
144 
145         case UC_DOUBLE:
146                 d = ucv_double_get(nv);
147 
148                 if (isnan(d) || d >= 0)
149                         res = ucv_get(nv);
150                 else
151                         res = ucv_double_new(-d);
152 
153                 break;
154 
155         default:
156                 res = ucv_double_new(NAN);
157                 break;
158         }
159 
160         ucv_put(nv);
161 
162         return res;
163 }
164 
165 /**
166  * Calculates the arc cosine of `x`.
167  *
168  * On success, this function returns the principal value of the arc
169  * cosine of `x` in radians; the return value is in the range [pi, 0].
170  *
171  *  - If `x` is -1, pi is returned.
172  *  - If `x` is  0, pi/2 is returned.
173  *  - If `x` is +1, 0 is returned.
174  *
175  * When `x` can't be converted to a numeric value, `NaN` is
176  * returned.
177  *
178  * @function module:math#acos
179  *
180  * @param {double} x
181  * The `x` value.
182  *
183  * @returns {double}
184  * @example
185  * acos(-1); // 3.1415926535898 i.e. pi
186  * acos(0);  // 1.5707963267949 i.e. pi/2
187  * acos(1);  // 0.0 i.e. 0 pi
188  */
189 static uc_value_t *
190 uc_arccos(uc_vm_t *vm, size_t nargs)
191 {
192         double x = ucv_to_double(uc_fn_arg(0));
193 
194         if (isnan(x))
195                 return ucv_double_new(NAN);
196 
197         return ucv_double_new(acos(x));
198 }
199 
200 /**
201  * Calculates the arc sine of `x`.
202  *
203  * On success, this function returns the principal value of the arc
204  * sine of `x` in radians; the return value is in the range [-pi/2, pi/2].
205  *
206  *  - If `x` is +0 (-0), 0 is returned.
207  *  - If `x` is +1 (-1), pi/2 (-pi/2) is returned.
208  *
209  * When `x` can't be converted to a numeric value, `NaN` is
210  * returned.
211  *
212  * @function module:math#asin
213  *
214  * @param {double} x
215  * The `x` value.
216  *
217  * @returns {double}
218  * @example
219  * asin(-1); // -1.5707963267949 i.e. -pi/2
220  * asin(0);  // 0.0 i.e. 0 pi
221  * asin(1);  // 1.5707963267949 i.e. pi/2
222  */
223 static uc_value_t *
224 uc_arcsin(uc_vm_t *vm, size_t nargs)
225 {
226         double x = ucv_to_double(uc_fn_arg(0));
227 
228         if (isnan(x))
229                 return ucv_double_new(NAN);
230 
231         return ucv_double_new(asin(x));
232 }
233 
234 /**
235  * Calculates the arc tangent of `x`.
236  *
237  * On success, this function returns the principal value of the arc
238  * tangent of `x` in radians; the return value is in the range [-pi/2, pi/2].
239  *
240  *  - If `x` is +0 (-0), 0 is returned.
241  *  - As `x` tends toward +Infinity (-Infinity), the return value asymptotically
242  * converges toward pi/2 (-pi/2).
243  *
244  * When `x` can't be converted to a numeric value, `NaN` is
245  * returned.
246  *
247  * @function module:math#atan
248  *
249  * @param {double} x
250  * The `x` value.
251  *
252  * @returns {double}
253  * @example
254  * atan(-100000); // -1.5707863267949 i.e. ~ -pi/2
255  * atan(0);       // 0.0 i.e. 0 pi
256  * atan(100000);  // 1.5707863267949 i.e. ~ pi/2
257  */
258 static uc_value_t *
259 uc_arctan(uc_vm_t *vm, size_t nargs)
260 {
261         double x = ucv_to_double(uc_fn_arg(0));
262 
263         if (isnan(x))
264                 return ucv_double_new(NAN);
265 
266         return ucv_double_new(atan(x));
267 }
268 
269 /**
270  * Calculates the hyperbolic cosine of `x`.
271  *
272  * On success, this function returns the principal value of the hyperbolic
273  * cosine of `x`; the return value is in the range [Infinity, 1].
274  * 
275  * The relationship is: cosh = `((e^x) + (e^-x)) / 2`.
276  *
277  *  - As `x` decreases below -1, the return value exponentiates toward Infinity.
278  *  - If `x` is  0, 1 is returned.
279  *  - As `x` increases above +1, the return value exponentiates toward Infinity.
280  *
281  * When `x` can't be converted to a numeric value, `NaN` is
282  * returned.
283  *
284  * @function module:math#cosh
285  *
286  * @param {double} x
287  * The `x` value.
288  *
289  * @returns {double}
290  * @example
291  * cosh(-10); // 11013.232920103
292  * cosh(-1);  // 1.5430806348152
293  * cosh(0);   // 1.0
294  * cosh(1);   // 1.5430806348152
295  * cosh(10);  // 11013.232920103
296  */
297 static uc_value_t *
298 uc_cosh(uc_vm_t *vm, size_t nargs)
299 {
300         double x = ucv_to_double(uc_fn_arg(0));
301 
302         if (isnan(x))
303                 return ucv_double_new(NAN);
304 
305         return ucv_double_new(cosh(x));
306 }
307 
308 /**
309  * Calculates the hyperbolic sine of `x`.
310  *
311  * On success, this function returns the principal value of the hyperbolic
312  * sine of `x`; the return value is in the range [-Infinity, Infinity].
313  *  
314  * The relationship is: sinh = `((e^x) - (e^-x)) / 2`.
315  *
316  *  - As `x` decreases below -1, the return value exponentiates toward -Infinity.
317  *  - If `x` is  0, 0 is returned.
318  *  - As `x` increases above +1, the return value exponentiates toward Infinity.
319  *
320  * When `x` can't be converted to a numeric value, `NaN` is
321  * returned.
322  *
323  * @function module:math#sinh
324  *
325  * @param {double} x
326  * The `x` value.
327  *
328  * @returns {double}
329  * @example
330  * sinh(-10); // -11013.232920103
331  * sinh(-1);  // -1.1752011936438
332  * sinh(0);   // 0.0
333  * sinh(1);   // 1.1752011936438
334  * sinh(10);  // 11013.232920103
335  */
336 static uc_value_t *
337 uc_sinh(uc_vm_t *vm, size_t nargs)
338 {
339         double x = ucv_to_double(uc_fn_arg(0));
340 
341         if (isnan(x))
342                 return ucv_double_new(NAN);
343 
344         return ucv_double_new(sinh(x));
345 }
346 
347 /**
348  * Calculates the hyperbolic tangent of `x`.
349  *
350  * On success, this function returns the principal value of the hyperbolic
351  * tangent of `x`; the return value is in the range [-1, 1].
352  *
353  * The relationship is: tanh = `((e^x) - (e^-x)) / ((e^x) + (e^-x))`, or
354  * tanh = `sinh(x) / cosh(x)`.
355  *
356  *  - As `x` decreases below -1, the return value asymptotically expands
357  * toward -1.
358  *  - If `x` is  0, 0 is returned.
359  *  - As `x` increases above +1, the return value asymptotically expands
360  * toward 1.
361  *
362  * When `x` can't be converted to a numeric value, `NaN` is
363  * returned.
364  *
365  * @function module:math#tanh
366  *
367  * @param {double} x
368  * The `x` value.
369  *
370  * @returns {double}
371  * @example
372  * atan(-100); // -1.0
373  * atan(-10);  // -0.99999999587769
374  * atan(0);    // 0.0
375  * atan(10);   // 0.99999999587769
376  * atan(100);  // 1.0
377  */
378 static uc_value_t *
379 uc_tanh(uc_vm_t *vm, size_t nargs)
380 {
381         double x = ucv_to_double(uc_fn_arg(0));
382 
383         if (isnan(x))
384                 return ucv_double_new(NAN);
385 
386         return ucv_double_new(tanh(x));
387 }
388 
389 /**
390  * Calculates the principal value of the arc tangent of `y`/`x`,
391  * using the signs of the two arguments to determine the quadrant
392  * of the result.
393  *
394  * On success, this function returns the principal value of the arc
395  * tangent of `y`/`x` in radians; the return value is in the range [-pi, pi].
396  *
397  *  - If `y` is +0 (-0) and `x` is less than 0, +pi (-pi) is returned.
398  *  - If `y` is +0 (-0) and `x` is greater than 0, +0 (-0) is returned.
399  *  - If `y` is less than 0 and `x` is +0 or -0, -pi/2 is returned.
400  *  - If `y` is greater than 0 and `x` is +0 or -0, pi/2 is returned.
401  *  - If either `x` or `y` is NaN, a NaN is returned.
402  *  - If `y` is +0 (-0) and `x` is -0, +pi (-pi) is returned.
403  *  - If `y` is +0 (-0) and `x` is +0, +0 (-0) is returned.
404  *  - If `y` is a finite value greater (less) than 0, and `x` is negative
405  *    infinity, +pi (-pi) is returned.
406  *  - If `y` is a finite value greater (less) than 0, and `x` is positive
407  *    infinity, +0 (-0) is returned.
408  *  - If `y` is positive infinity (negative infinity), and `x` is finite,
409  *    pi/2 (-pi/2) is returned.
410  *  - If `y` is positive infinity (negative infinity) and `x` is negative
411  *    infinity, +3 * pi/4 (-3 * pi/4) is returned.
412  *  - If `y` is positive infinity (negative infinity) and `x` is positive
413  *    infinity, +pi/4 (-pi/4) is returned.
414  *
415  * When either `x` or `y` can't be converted to a numeric value, `NaN` is
416  * returned.
417  *
418  * @function module:math#atan2
419  *
420  * @param {*} y
421  * The `y` value.
422  *
423  * @param {*} x
424  * The `x` value.
425  *
426  * @returns {number}
427  */
428 static uc_value_t *
429 uc_atan2(uc_vm_t *vm, size_t nargs)
430 {
431         double d1 = ucv_to_double(uc_fn_arg(0));
432         double d2 = ucv_to_double(uc_fn_arg(1));
433 
434         if (isnan(d1) || isnan(d2))
435                 return ucv_double_new(NAN);
436 
437         return ucv_double_new(atan2(d1, d2));
438 }
439 
440 /**
441  * Calculates the tangent of `x`, the floating-point value representing the
442  * angle in radians.
443  *
444  * On success, this function returns the tangent of `x`.
445  * 
446  * The relationship is `tan(x) = sin(x) / cos (x)`. A graph of the tangent has
447  * periodic patterns directly related to ratios of pi, where radian values of
448  * whole multiples of (1, 2, 3, ...) pi are 0, and radian values of half
449  * multiples of pi (1/2, 3/2, 5/2, ...) are +/-Infinity.
450  *
451  *
452  * When `x` can't be converted to a numeric value, `NaN` is
453  * returned.
454  *
455  * @function module:math#tan
456  *
457  * @param {double} x
458  * The `x` value.
459  *
460  * @returns {double}
461  */
462 static uc_value_t *
463 uc_tan(uc_vm_t *vm, size_t nargs)
464 {
465         double x = ucv_to_double(uc_fn_arg(0));
466 
467         if (isnan(x))
468                 return ucv_double_new(NAN);
469 
470         return ucv_double_new(tan(x));
471 }
472 
473 /**
474  * Calculates the cosine of `x`, where `x` is given in radians.
475  *
476  * Returns the resulting cosine value.
477  *
478  * Returns `NaN` if the `x` value can't be converted to a number.
479  *
480  * @function module:math#cos
481  *
482  * @param {number} x
483  * Radians value to calculate cosine for.
484  *
485  * @returns {number}
486  */
487 static uc_value_t *
488 uc_cos(uc_vm_t *vm, size_t nargs)
489 {
490         double d = ucv_to_double(uc_fn_arg(0));
491 
492         if (isnan(d))
493                 return ucv_double_new(NAN);
494 
495         return ucv_double_new(cos(d));
496 }
497 
498 /**
499  * Calculates the value of `e` (the base of natural logarithms)
500  * raised to the power of `x`.
501  *
502  * On success, returns the exponential value of `x`.
503  *
504  *  - If `x` is positive infinity, positive infinity is returned.
505  *  - If `x` is negative infinity, `+0` is returned.
506  *  - If the result underflows, a range error occurs, and zero is returned.
507  *  - If the result overflows, a range error occurs, and `Infinity` is returned.
508  *
509  * Returns `NaN` if the `x` value can't be converted to a number.
510  *
511  * @function module:math#exp
512  *
513  * @param {number} x
514  * Power to raise `e` to.
515  *
516  * @returns {number}
517  */
518 static uc_value_t *
519 uc_exp(uc_vm_t *vm, size_t nargs)
520 {
521         double d = ucv_to_double(uc_fn_arg(0));
522 
523         if (isnan(d))
524                 return ucv_double_new(NAN);
525 
526         return ucv_double_new(exp(d));
527 }
528 
529 /**
530  * Calculates the natural logarithm of `x`.
531  *
532  * On success, returns the natural logarithm of `x`.
533  *
534  *  - If `x` is `1`, the result is `+0`.
535  *  - If `x` is positive infinity, positive infinity is returned.
536  *  - If `x` is zero, then a pole error occurs, and the function
537  *    returns negative infinity.
538  *  - If `x` is negative (including negative infinity), then a domain
539  *    error occurs, and `NaN` is returned.
540  *
541  * Returns `NaN` if the `x` value can't be converted to a number.
542  *
543  * @function module:math#log
544  *
545  * @param {number} x
546  * Value to calculate natural logarithm of.
547  *
548  * @returns {number}
549  */
550 static uc_value_t *
551 uc_log(uc_vm_t *vm, size_t nargs)
552 {
553         double d = ucv_to_double(uc_fn_arg(0));
554 
555         if (isnan(d))
556                 return ucv_double_new(NAN);
557 
558         return ucv_double_new(log(d));
559 }
560 
561 /**
562  * Calculate base-10 log of x.
563  *
564  * @function module:math#log10
565  *
566  * @param {double} x number
567  *
568  * @returns {double}
569  * The common (base-10) logarithm of x, or
570  * `NaN` if the given argument could not be converted to a number.
571  *
572  * @example
573  * log10(100);   // 2.0
574  * log10(10);    // 1.0
575  * log10(5);     // 0.69897000433602
576  * log10(1);     // 0.0
577  * log10(0);     // -1e309
578  */
579 static uc_value_t *
580 uc_log10(uc_vm_t *vm, size_t nargs)
581 {
582         double x = ucv_to_double(uc_fn_arg(0));
583 
584         if (isnan(x))
585                 return ucv_double_new(NAN);
586 
587         return ucv_double_new(log10(x));
588 }
589 
590 /**
591  * Calculate base-2 log of x.
592  *
593  * @function module:math#log2
594  *
595  * @param {double} x number
596  *
597  * @returns {double}
598  * The common (base-2) logarithm of x, or
599  * `NaN` if the given argument could not be converted to a number.
600  *
601  * @example
602  * log2(1024);  // 10.0
603  * log2(512);   // 9.0
604  * log2(16);    // 4.0
605  * log2(4);     // 2.0
606  * log2(2);     // 1.0
607  * log2(1);     // 0.0
608  * log2(0);     // -1e309
609  */
610 static uc_value_t *
611 uc_log2(uc_vm_t *vm, size_t nargs)
612 {
613         double x = ucv_to_double(uc_fn_arg(0));
614 
615         if (isnan(x))
616                 return ucv_double_new(NAN);
617 
618         return ucv_double_new(log2(x));
619 }
620 
621 /**
622  * Computes the natural (base e) logarithm of 1 + x. This function is more
623  * precise than the expression {@link module:math#log `log`}(1 + x) if x is
624  * close to zero.
625  *
626  * @function module:math#log1p
627  *
628  * @param {double} x number
629  *
630  * @returns {double}
631  * The natural (base e) logarithm of 1 + x, or
632  * `NaN` if the given argument could not be converted to a number.
633  *
634  * @example
635  * log1p(10);    // 2.3978952727984
636  * log1p(1);     // 0.69314718055995
637  * log1p(0.1);   // 0.095310179804325
638  * log1p(0.001); // 0.00099950033308353
639  * log1p(0);     // 0.0
640  */
641 static uc_value_t *
642 uc_log1p(uc_vm_t *vm, size_t nargs)
643 {
644         double x = ucv_to_double(uc_fn_arg(0));
645 
646         if (isnan(x))
647                 return ucv_double_new(NAN);
648 
649         return ucv_double_new(log1p(x));
650 }
651 
652 /**
653  * Computes the e (Euler's number, 2.7182818) raised to the given power x,
654  * minus 1.0. This function is more accurate than the expression 
655  * {@link module:math#exp `exp(x)`}-1.0
656  * if x is close to zero.
657  *
658  * @function module:math#expm1
659  *
660  * @param {double} x number
661  *
662  * @returns {double}
663  * The e (Euler's number, 2.7182818) raised to the given power x, minus 1.0, or
664  * `NaN` if the given argument could not be converted to a number.
665  *
666  * @example
667  * expm1(10);       // 22025.465794807
668  * expm1(1);        // 1.718281828459
669  * expm1(0.1);      // 0.10517091807565
670  * expm1(0.001);    // 0.0010005001667083
671  * exp(0.001)-1;    // 0.0010005001667084
672  * expm1(0.0001);   // 0.00010000500016667
673  * exp(0.0001)-1;   // 0.00010000500016671
674  * expm1(0.000001); // 1.0000005000002e-06
675  * exp(0.000001)-1; // 1.0000004999622e-06
676  * expm1(0);        // 0.0
677  */
678 static uc_value_t *
679 uc_expm1(uc_vm_t *vm, size_t nargs)
680 {
681         double x = ucv_to_double(uc_fn_arg(0));
682 
683         if (isnan(x))
684                 return ucv_double_new(NAN);
685 
686         return ucv_double_new(expm1(x));
687 }
688 
689 /**
690  * Calculates the sine of `x`, where `x` is given in radians.
691  *
692  * Returns the resulting sine value.
693  *
694  *  - When `x` is positive or negative infinity, a domain error occurs
695  *    and `NaN` is returned.
696  *
697  * Returns `NaN` if the `x` value can't be converted to a number.
698  *
699  * @function module:math#sin
700  *
701  * @param {number} x
702  * Radians value to calculate sine for.
703  *
704  * @returns {number}
705  */
706 static uc_value_t *
707 uc_sin(uc_vm_t *vm, size_t nargs)
708 {
709         double d = ucv_to_double(uc_fn_arg(0));
710 
711         if (isnan(d))
712                 return ucv_double_new(NAN);
713 
714         return ucv_double_new(sin(d));
715 }
716 
717 /**
718  * Calculates the non-negative square root of `x`.
719  *
720  * Returns the resulting square root value.
721  *
722  *  - If `x` is `+0` (`-0`) then `+0` (`-0`) is returned.
723  *  - If `x` is positive infinity, positive infinity is returned.
724  *  - If `x` is less than `-0`, a domain error occurs, and `NaN` is returned.
725  *
726  * Returns `NaN` if the `x` value can't be converted to a number.
727  *
728  * @function module:math#sqrt
729  *
730  * @param {number} x
731  * Value to calculate square root for.
732  *
733  * @returns {number}
734  */
735 static uc_value_t *
736 uc_sqrt(uc_vm_t *vm, size_t nargs)
737 {
738         double d = ucv_to_double(uc_fn_arg(0));
739 
740         if (isnan(d))
741                 return ucv_double_new(NAN);
742 
743         return ucv_double_new(sqrt(d));
744 }
745 
746 /**
747  * Calculates the cube root of `x`.
748  *
749  * Returns the resulting cube root value.
750  *
751  *  - If `x` is `+0` (`-0`) then `+0` (`-0`) is returned.
752  *  - If `x` is (+/-) infinity, (+/-) infinity is returned.
753  *
754  * Returns `NaN` if the `x` value can't be converted to a number.
755  *
756  * @function module:math#cbrt
757  *
758  * @param {double} x
759  * Value to calculate cube root for.
760  *
761  * @returns {double}
762  * @example
763  * cbrt(27);  // 3.0
764  * cbrt(0);   // 0.0
765  * cbrt(-27); // -3.0
766  */
767 static uc_value_t *
768 uc_cbrt(uc_vm_t *vm, size_t nargs)
769 {
770         double x = ucv_to_double(uc_fn_arg(0));
771 
772         if (isnan(x))
773                 return ucv_double_new(NAN);
774 
775         return ucv_double_new(cbrt(x));
776 }
777 
778 /**
779  * Computes the square root of the sum of the squares of `x` and `y`, i.e.
780  * the hypotenuse without undue overflow or underflow at intermediate stages of
781  * the computation.
782  *
783  * Returns the result of `sqrt(x^2 + y^2)`.
784  *
785  *  - If `x` and `y` are `+0` (`-0`) then `+0` (`-0`) is returned.
786  *  - If `x` or `y` is `+0` (`-0`) then `+y` or `+x` is returned.
787  *  - If `x` or `y` is (+/-) infinity, (+/-) infinity is returned.
788  *
789  * Returns `NaN` if the `x` or `y` value can't be converted to a number.
790  *
791  * @function module:math#hypot
792  *
793  * @param {double} x base
794  * @param {double} y height
795  *
796  * @returns {double}
797  * @example
798  * hypot(3, 3);   // 4.2426406871193
799  * hypot(2, 2);   // 2.8284271247462
800  * hypot(1, 1);   // 1.4142135623731
801  * hypot(0, 0);   // 0.0
802  * hypot(-1, -1); // -1.4142135623731
803  */
804 static uc_value_t *
805 uc_hypot(uc_vm_t *vm, size_t nargs)
806 {
807         double x = ucv_to_double(uc_fn_arg(0));
808         double y = ucv_to_double(uc_fn_arg(1));
809 
810         if (isnan(x) || isnan(y))
811                 return ucv_double_new(NAN);
812 
813         return ucv_double_new(hypot(x, y));
814 }
815 
816 /**
817  * Calculates the value of `x` raised to the power of `y`.
818  *
819  * On success, returns the value of `x` raised to the power of `y`.
820  *
821  *  - If the result overflows, a range error occurs, and the function
822  *    returns `Infinity`.
823  *  - If result underflows, and is not representable, a range error
824  *    occurs, and `0.0` with the appropriate sign is returned.
825  *  - If `x` is `+0` or `-0`, and `y` is an odd integer less than `0`,
826  *    a pole error occurs `Infinity` is returned, with the same sign
827  *    as `x`.
828  *  - If `x` is `+0` or `-0`, and `y` is less than `0` and not an odd
829  *    integer, a pole error occurs and `Infinity` is returned.
830  *  - If `x` is `+0` (`-0`), and `y` is an odd integer greater than `0`,
831  *    the result is `+0` (`-0`).
832  *  - If `x` is `0`, and `y` greater than `0` and not an odd integer,
833  *    the result is `+0`.
834  *  - If `x` is `-1`, and `y` is positive infinity or negative infinity,
835  *    the result is `1.0`.
836  *  - If `x` is `+1`, the result is `1.0` (even if `y` is `NaN`).
837  *  - If `y` is `0`, the result is `1.0` (even if `x` is `NaN`).
838  *  - If `x` is a finite value less than `0`, and `y` is a finite
839  *    non-integer, a domain error occurs, and `NaN` is returned.
840  *  - If the absolute value of `x` is less than `1`, and `y` is negative
841  *    infinity, the result is positive infinity.
842  *  - If the absolute value of `x` is greater than `1`, and `y` is
843  *    negative infinity, the result is `+0`.
844  *  - If the absolute value of `x` is less than `1`, and `y` is positive
845  *    infinity, the result is `+0`.
846  *  - If the absolute value of `x` is greater than `1`, and `y` is positive
847  *    infinity, the result is positive infinity.
848  *  - If `x` is negative infinity, and `y` is an odd integer less than `0`,
849  *    the result is `-0`.
850  *  - If `x` is negative infinity, and `y` less than `0` and not an odd
851  *    integer, the result is `+0`.
852  *  - If `x` is negative infinity, and `y` is an odd integer greater than
853  *    `0`, the result is negative infinity.
854  *  - If `x` is negative infinity, and `y` greater than `0` and not an odd
855  *    integer, the result is positive infinity.
856  *  - If `x` is positive infinity, and `y` less than `0`, the result is `+0`.
857  *  - If `x` is positive infinity, and `y` greater than `0`, the result is
858  *    positive infinity.
859  *
860  * Returns `NaN` if either the `x` or `y` value can't be converted to a number.
861  *
862  * @function module:math#pow
863  *
864  * @param {number} x
865  * The base value.
866  *
867  * @param {number} y
868  * The power value.
869  *
870  * @returns {number}
871  */
872 static uc_value_t *
873 uc_pow(uc_vm_t *vm, size_t nargs)
874 {
875         double x = ucv_to_double(uc_fn_arg(0));
876         double y = ucv_to_double(uc_fn_arg(1));
877 
878         if (isnan(x) || isnan(y))
879                 return ucv_double_new(NAN);
880 
881         return ucv_double_new(pow(x, y));
882 }
883 
884 /**
885  * Depending on the arguments, it produces a pseudo-random positive integer, 
886  * or a pseudo-random number in a supplied range.
887  *
888  * Without arguments it returns the calculated pseuo-random value. The value 
889  * is within the range `0` to `RAND_MAX` inclusive where `RAND_MAX` is a platform 
890  * specific value guaranteed to be at least `32767`.
891  * 
892  * With 2 arguments `a, b` it returns a number in the range `a` to `b` inclusive.
893  * With a single argument `a` it returns a number in the range `0` to `a` inclusive.
894  * 
895  * The {@link module:math#srand `srand()`} function sets its argument as the
896  * seed for a new sequence of pseudo-random integers to be returned by `rand()`.
897  * These sequences are repeatable by calling {@link module:math#srand `srand()`}
898  * with the same seed value.
899  *
900  * If no seed value is explicitly set by calling
901  * {@link module:math#srand `srand()`} prior to the first call to `rand()`,
902  * the math module will automatically seed the PRNG once, using the current
903  * time of day in milliseconds as seed value.
904  *
905  * @function module:math#rand
906  *
907  * @param {number} [a]
908  * End of the desired range.
909  * 
910  * @param {number} [b]
911  * The other end of the desired range.
912  * 
913  * @returns {number}
914  */
915 static uc_value_t *
916 uc_rand(uc_vm_t *vm, size_t nargs)
917 {
918         struct timeval tv;
919 
920         if (!ucv_boolean_get(uc_vm_registry_get(vm, "math.srand_called"))) {
921                 gettimeofday(&tv, NULL);
922                 srand((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
923 
924                 uc_vm_registry_set(vm, "math.srand_called", ucv_boolean_new(true));
925         }
926 
927         if (nargs == 0)
928                 return ucv_int64_new(rand());
929 
930         double a = ucv_to_double(uc_fn_arg(0)), b = 0;
931 
932         if (nargs > 1)
933                 b = ucv_to_double(uc_fn_arg(1));
934 
935         return ucv_double_new(a + ((b - a) * rand()) / RAND_MAX);
936 }
937 
938 /**
939  * Seeds the pseudo-random number generator.
940  *
941  * This functions seeds the PRNG with the given value and thus affects the
942  * pseudo-random integer sequence produced by subsequent calls to
943  * {@link module:math#rand `rand()`}.
944  *
945  * Setting the same seed value will result in the same pseudo-random numbers
946  * produced by {@link module:math#rand `rand()`}.
947  *
948  * @function module:math#srand
949  *
950  * @param {number} seed
951  * The seed value.
952  */
953 static uc_value_t *
954 uc_srand(uc_vm_t *vm, size_t nargs)
955 {
956         int64_t n = ucv_to_integer(uc_fn_arg(0));
957 
958         srand((unsigned int)n);
959         uc_vm_registry_set(vm, "math.srand_called", ucv_boolean_new(true));
960 
961         return NULL;
962 }
963 
964 /**
965  * Tests whether `x` is a `NaN` double.
966  *
967  * This functions checks whether the given argument is of type `double` with
968  * a `NaN` (not a number) value.
969  *
970  * Returns `true` if the value is `NaN`, otherwise false.
971  *
972  * Note that a value can also be checked for `NaN` with the expression
973  * `x !== x` which only evaluates to `true` if `x` is `NaN`.
974  *
975  * @function module:math#isnan
976  *
977  * @param {number} x
978  * The value to test.
979  *
980  * @returns {boolean}
981  */
982 static uc_value_t *
983 uc_isnan(uc_vm_t *vm, size_t nargs)
984 {
985         uc_value_t *v = uc_fn_arg(0);
986 
987         return ucv_boolean_new(ucv_type(v) == UC_DOUBLE && isnan(ucv_double_get(v)));
988 }
989 
990 /**
991  * Tests whether `x` is double precision `Infinity`.
992  *
993  * This functions checks whether the given argument is of type `double` of
994  * `Infinity` value. Double precision values >= 1.8e308 are considered `Infinity`.
995  *
996  * Returns `true` if the value is `Infinity`, otherwise false.
997  *
998  * @function module:math#isinf
999  *
1000  * @param {number} x
1001  * The value to test.
1002  *
1003  * @returns {boolean}
1004  */
1005 static uc_value_t *
1006 uc_isinf(uc_vm_t *vm, size_t nargs)
1007 {
1008         uc_value_t *v = uc_fn_arg(0);
1009 
1010         return ucv_boolean_new(ucv_type(v) == UC_DOUBLE && isinf(ucv_double_get(v)));
1011 }
1012 
1013 /**
1014  * Returns the radian value of the given degree value.
1015  *
1016  * @function module:math#deg2rad
1017  *
1018  * @param {double} number
1019  * The number to return the radian value for.
1020  *
1021  * @returns {number}
1022  * Returns the absolute value or `NaN` if the given argument could
1023  * not be converted to a number.
1024  * @example
1025  * deg2rad(180);   // 3.1415926535898
1026  * deg2rad("180"); // 3.1415926535898
1027  */
1028 static uc_value_t *
1029 uc_deg2rad(uc_vm_t *vm, size_t nargs)
1030 {
1031         double d = ucv_to_double(uc_fn_arg(0));
1032 
1033         return ucv_double_new(degToRad(d));
1034 }
1035 
1036 /**
1037  * Returns the degree value of the given radian value.
1038  *
1039  * @function module:math#rad2deg
1040  *
1041  * @param {double} number
1042  * The number to return the degree value for.
1043  *
1044  * @returns {number}
1045  * Returns the absolute value or `NaN` if the given argument could
1046  * not be converted to a number.
1047  * @example
1048  * rad2deg(3.1415926535898);   // 180.0
1049  * rad2deg("3.1415926535898"); // 180.0
1050  */
1051 static uc_value_t *
1052 uc_rad2deg(uc_vm_t *vm, size_t nargs)
1053 {
1054         double d = ucv_to_double(uc_fn_arg(0));
1055 
1056         return ucv_double_new(radToDeg(d));
1057 }
1058 
1059 /**
1060  * Returns the lesser of two values x and y.
1061  *
1062  * @function module:math#fmin
1063  *
1064  * @param {double} x first parameter
1065  * @param {double} y second parameter
1066  *
1067  * @returns {double}
1068  * Returns the lesser of the two values x or y or `NaN` if a given argument
1069  * could not be converted to a number. Use `(-)Infinity` or `NAN` for
1070  * comparisons involving said values.
1071  * @example
1072  * fmin("180", "-180");   // -180.0
1073  * fmin(180, -180);       // -180.0
1074  * fmin(-Infinity, 0);    // -1e309 i.e. -infinity in double type representation.
1075  */
1076 static uc_value_t *
1077 uc_fmin(uc_vm_t *vm, size_t nargs)
1078 {
1079         double x = ucv_to_double(uc_fn_arg(0));
1080         double y = ucv_to_double(uc_fn_arg(1));
1081 
1082         if (isnan(x) || isnan(y))
1083                 return ucv_double_new(NAN);
1084 
1085         return ucv_double_new(fmin(x, y));
1086 }
1087 
1088 /**
1089  * Returns the greater of two values x and y.
1090  *
1091  * @function module:math#fmax
1092  *
1093  * @param {double} x first parameter
1094  * @param {double} y second parameter
1095  *
1096  * @returns {double}
1097  * Returns the greater of the two values x or y or `NaN` if a given argument
1098  * could not be converted to a number. Use `(-)Infinity` or `NAN` for
1099  * comparisons involving said values.
1100  * @example
1101  * fmax("180", "-180");   // 180.0
1102  * fmax(180, -180);       // 180.0
1103  * fmax(Infinity, 0);     // 1e309 i.e. infinity in double type representation.
1104  */
1105 static uc_value_t *
1106 uc_fmax(uc_vm_t *vm, size_t nargs)
1107 {
1108         double x = ucv_to_double(uc_fn_arg(0));
1109         double y = ucv_to_double(uc_fn_arg(1));
1110 
1111         if (isnan(x) || isnan(y))
1112                 return ucv_double_new(NAN);
1113 
1114         return ucv_double_new(fmax(x, y));
1115 }
1116 
1117 /**
1118  * Clamps `x` to within `upper` and `lower` bounds if `x` exceeds them.
1119  *
1120  * The operation is effectively: `min(upper, max(x, lower))`.
1121  *
1122  * @function module:math#clamp
1123  *
1124  * @param {double} x number to clamp
1125  * @param {double} upper upper bound
1126  * @param {double} lower lower bound
1127  *
1128  * @returns {double}
1129  * Returns `x` if within `upper` and `lower`, otherwise one of `lower` or `upper`
1130  * if `x` exceeds those bounds, or `NaN` if any given argument
1131  * could not be converted to a number.
1132  * @example
1133  * clamp(1000, 200, 180);   // 200.0
1134  * clamp(-1000, 200, 180);  // 180.0
1135  * clamp(190, 200, 180);    // 190.0
1136  */
1137 static uc_value_t *
1138 uc_clamp(uc_vm_t *vm, size_t nargs)
1139 {
1140         double x = ucv_to_double(uc_fn_arg(0));
1141         double upper = ucv_to_double(uc_fn_arg(1));
1142         double lower = ucv_to_double(uc_fn_arg(2));
1143 
1144         if (isnan(x) || isnan(upper) || isnan(lower))
1145                 return ucv_double_new(NAN);
1146 
1147         return ucv_double_new(fmin(upper, fmax(x, lower)));
1148 }
1149 
1150 /**
1151  * Returns -1 or 1 depending on the sign of the given number, or 0 if the given
1152  * number itself is zero.
1153  *
1154  * @function module:math#sign
1155  *
1156  * @param {double} x number
1157  *
1158  * @returns {integer}
1159  * Returns -1 or 1 for negative and positive inputs respectively, 0 if the given
1160  * number is zero, or `NaN` if the given argument could not be converted to a
1161  * number.
1162  * @example
1163  * sign(2);   // 1
1164  * sign(-8);  // -1
1165  * sign(0);   // 0
1166  * sign(-0);  // 0
1167  */
1168 static uc_value_t *
1169 uc_sign(uc_vm_t *vm, size_t nargs)
1170 {
1171         double x = ucv_to_double(uc_fn_arg(0));
1172 
1173         if (isnan(x))
1174                 return ucv_double_new(NAN);
1175 
1176         return ucv_int64_new((x > 0) - (x < 0));
1177 }
1178 
1179 /**
1180  * Returns -1 or 1 depending on the sign of the given number, or 0 if the given
1181  * number itself is zero. IEEE-754 behaviour.
1182  *
1183  * @function module:math#signbit
1184  *
1185  * @param {double} x number
1186  *
1187  * @returns {integer}
1188  * Returns -1 or 1 for negative and positive inputs respectively, 0 if the given
1189  * number is zero, -1 for -0.0, or `NaN` if the given argument could not be
1190  * converted to a number.
1191  * @example
1192  * signbit(2);    // 1
1193  * signbit(-8);   // -1
1194  * signbit(0);    // 0
1195  * signbit(-0.0); // -1
1196  * signbit(-0);   // 0
1197  */
1198 static uc_value_t *
1199 uc_signbit(uc_vm_t *vm, size_t nargs)
1200 {
1201         double x = ucv_to_double(uc_fn_arg(0));
1202 
1203         if (isnan(x))
1204                 return ucv_double_new(NAN);
1205 
1206         return ucv_int64_new(signbit(x) ? -1 : (x > 0));
1207 }
1208 
1209 /**
1210  * Returns -1 or 1 depending on the sign of the given number only (no zero).
1211  * (-)Zero effectively becomes 1.
1212  *
1213  * @function module:math#signnz
1214  *
1215  * @param {double} x number
1216  *
1217  * @returns {integer}
1218  * Returns -1 or +1 for negative and positive inputs respectively, and zero is
1219  * converted to +1, or `NaN` if the given argument could not be converted to a
1220  * number.
1221  * @example
1222  * signnz(2);   // 1
1223  * signnz(-8);  // -1
1224  * signnz(0);   // 1
1225  * signnz(-0);  // 1
1226  */
1227 static uc_value_t *
1228 uc_signnz(uc_vm_t *vm, size_t nargs)
1229 {
1230         double x = ucv_to_double(uc_fn_arg(0));
1231 
1232         if (isnan(x))
1233                 return ucv_double_new(NAN);
1234 
1235         return ucv_int64_new((x >= 0) ? 1 : -1);
1236 }
1237 
1238 /**
1239  * Returns a double whose magnitude is that of `x`, but whose sign is that of
1240  * `y`.
1241  *
1242  * @function module:math#copysign
1243  *
1244  * @param {double} x number
1245  * @param {double} y number
1246  *
1247  * @returns {double}
1248  * Returns `NaN` if a given argument could not be converted to a number.
1249  * @example
1250  * copysign(-3, -5);  // -3.0
1251  * copysign(8, -5);   // -8.0
1252  * copysign(-0, 3);   // 0.0
1253  */
1254 static uc_value_t *
1255 uc_copysign(uc_vm_t *vm, size_t nargs)
1256 {
1257         double x = ucv_to_double(uc_fn_arg(0));
1258         double y = ucv_to_double(uc_fn_arg(1));
1259 
1260         if (isnan(x) || isnan(y))
1261                 return ucv_double_new(NAN);
1262 
1263         return ucv_double_new(copysign(x, y));
1264 }
1265 
1266 /**
1267  * Floors `x` to the largest integer value not greater than `x`.
1268  *
1269  * @function module:math#floor
1270  *
1271  * @param {double} x number
1272  * @param {boolean} output_type - false is `integer`, true is `double`.
1273  *
1274  * @returns {number}
1275  * Returns the largest integer value not greater than `x`, or `NaN` if the given
1276  * argument could not be converted to a number.
1277  * @example
1278  * floor(2.7);        // 2
1279  * floor(2.7, true);  // 2.0
1280  * floor(-2.7);       // -3
1281  * floor(-0.0);       // 0
1282  * floor(-0.0, true); // -0.0
1283  * floor(-Infinity);  // -1e309 i.e. -infinity in double type representation.
1284  */
1285 static uc_value_t *
1286 uc_floor(uc_vm_t *vm, size_t nargs)
1287 {
1288         double x = ucv_to_double(uc_fn_arg(0));
1289         bool ot = ucv_boolean_get(uc_fn_arg(1));
1290 
1291         if (isnan(x))
1292                 return ucv_double_new(NAN);
1293 
1294         return ot ? ucv_double_new(floor(x)) : ucv_int64_new(floor(x));
1295 }
1296 
1297 /**
1298  * Computes the smallest integer value not less than `x`.
1299  *
1300  * @function module:math#ceil
1301  *
1302  * @param {double} x number
1303  * @param {boolean} output_type - false is `integer`, true is `double`.
1304  *
1305  * @returns {number}
1306  * Returns the smallest integer value not less than `x`, or `NaN` if the given
1307  * argument could not be converted to a number.
1308  * @example
1309  * ceil(2.7);        // 3
1310  * ceil(2.7, true);  // 3.0
1311  * ceil(-2.7);       // -2
1312  * ceil(-0.0);       // 0
1313  * ceil(-0.0, true); // -0.0
1314  * ceil(-Infinity);  // -1e309 i.e. -infinity in double type representation.
1315  */
1316 static uc_value_t *
1317 uc_ceil(uc_vm_t *vm, size_t nargs)
1318 {
1319         double x = ucv_to_double(uc_fn_arg(0));
1320         bool ot = ucv_boolean_get(uc_fn_arg(1));
1321 
1322         if (isnan(x))
1323                 return ucv_double_new(NAN);
1324 
1325         return ot ? ucv_double_new(ceil(x)) : ucv_int64_new(ceil(x));
1326 }
1327 
1328 /**
1329  * Returns the integral value nearest to x rounding half-way cases away
1330  * from zero, regardless of the current rounding direction.
1331  *
1332  * @function module:math#round
1333  *
1334  * @param {double} x number
1335  * @param {boolean} output_type - false is `integer`, true is `double`.
1336  *
1337  * @returns {number}
1338  * Returns the rounded integer value of `x`, or `NaN` if the given
1339  * argument could not be converted to a number.
1340  *
1341  * @example
1342  * round(2.4);        // 2
1343  * round(2.5);        // 3
1344  * round(2.7);        // 3
1345  * round(2.7, true);  // 3.0
1346  * round(-2.4);       // -2
1347  * round(-2.5);       // -3
1348  * round(-2.7);       // -3
1349  * round(-0.0);       // 0
1350  * round(-0.0, true); // -0.0
1351  * round(-Infinity);  // -1e309 i.e. -infinity in double type representation.
1352  */
1353 static uc_value_t *
1354 uc_round(uc_vm_t *vm, size_t nargs)
1355 {
1356         double x = ucv_to_double(uc_fn_arg(0));
1357         bool ot = ucv_boolean_get(uc_fn_arg(1));
1358 
1359         if (isnan(x))
1360                 return ucv_double_new(NAN);
1361 
1362         return ot ? ucv_double_new(round(x)) : ucv_int64_new(round(x));
1363 }
1364 
1365 /**
1366  * Truncate away the decimal portion to produce the nearest integer not greater
1367  * in magnitude than x.
1368  *
1369  * @function module:math#trunc
1370  *
1371  * @param {number} x number
1372  * @param {boolean} output_type - false is `integer`, true is `double`.
1373  *
1374  * @returns {number}
1375  * The integral portion remaining after the decimal portion is truncated, or
1376  * `NaN` if the given argument could not be converted to a number.
1377  *
1378  * @example
1379  * trunc(2.4);        // 2
1380  * trunc(2.5);        // 2
1381  * trunc(2.7);        // 2
1382  * trunc(2.7, true);  // 2.0
1383  * trunc(-2.4);       // -2
1384  * trunc(-2.5);       // -2
1385  * trunc(-2.7);       // -2
1386  * trunc(-0.0);       // 0
1387  * trunc(-0.0, true); // -0.0
1388  */
1389 static uc_value_t *
1390 uc_trunc(uc_vm_t *vm, size_t nargs)
1391 {
1392         double x = ucv_to_double(uc_fn_arg(0));
1393         bool ot = ucv_boolean_get(uc_fn_arg(1));
1394 
1395         if (isnan(x))
1396                 return ucv_double_new(NAN);
1397 
1398         return ot ? ucv_double_new(trunc(x)) : ucv_int64_new(trunc(x));
1399 }
1400 
1401 static const uc_function_list_t math_fns[] = {
1402         { "abs",                uc_abs },
1403         { "acos",               uc_arccos },
1404         { "asin",               uc_arcsin },
1405         { "atan",               uc_arctan },
1406         { "atan2",              uc_atan2 },
1407         { "cosh",               uc_cosh },
1408         { "sinh",               uc_sinh },
1409         { "tanh",               uc_tanh },
1410         { "tan",                uc_tan },
1411         { "cos",                uc_cos },
1412         { "exp",                uc_exp },
1413         { "expm1",              uc_expm1 },
1414         { "log",                uc_log },
1415         { "log1p",              uc_log1p },
1416         { "log10",              uc_log10 },
1417         { "log2",               uc_log2 },
1418         { "sin",                uc_sin },
1419         { "sqrt",               uc_sqrt },
1420         { "hypot",              uc_hypot },
1421         { "cbrt",               uc_cbrt },
1422         { "pow",                uc_pow },
1423         { "rand",               uc_rand },
1424         { "srand",              uc_srand },
1425         { "isnan",              uc_isnan },
1426         { "isinf",              uc_isinf },
1427         { "deg2rad",    uc_deg2rad },
1428         { "rad2deg",    uc_rad2deg },
1429         { "fmin",               uc_fmin },
1430         { "fmax",               uc_fmax },
1431         { "clamp",              uc_clamp },
1432         { "sign",               uc_sign },
1433         { "signbit",    uc_signbit },
1434         { "signnz",             uc_signnz },
1435         { "copysign",   uc_copysign },
1436         { "floor",              uc_floor },
1437         { "ceil",               uc_ceil },
1438         { "round",              uc_round },
1439         { "trunc",              uc_trunc },
1440 };
1441 
1442 void uc_module_init(uc_vm_t *vm, uc_value_t *scope)
1443 {
1444         uc_function_list_register(scope, math_fns);
1445 
1446         ucv_object_add(scope, "PI", ucv_double_new(M_PI));
1447         ucv_object_add(scope, "E", ucv_double_new(M_E));
1448         ucv_object_add(scope, "SQRT2", ucv_double_new(M_SQRT2));
1449         ucv_object_add(scope, "SQRT1_2", ucv_double_new(M_SQRT1_2));
1450         ucv_object_add(scope, "LN2", ucv_double_new(M_LN2));
1451         ucv_object_add(scope, "LN10", ucv_double_new(M_LN10));
1452         ucv_object_add(scope, "LOG2E", ucv_double_new(M_LOG2E));
1453         ucv_object_add(scope, "LOG10E", ucv_double_new(M_LOG10E));
1454         ucv_object_add(scope, "LOG2_10", ucv_double_new(M_LOG2_10));
1455         ucv_object_add(scope, "LOG10_2", ucv_double_new(M_LOG10_2));
1456         ucv_object_add(scope, "PI_2", ucv_double_new(M_PI_2));
1457         ucv_object_add(scope, "PI_4", ucv_double_new(M_PI_4));
1458         ucv_object_add(scope, "INV_PI", ucv_double_new(M_1_PI));
1459         ucv_object_add(scope, "INV_2PI", ucv_double_new(M_2_PI));
1460         ucv_object_add(scope, "INV_SQRT2PI", ucv_double_new(M_2_SQRTPI));
1461 
1462         uc_vm_registry_set(vm, "math.srand_called", ucv_boolean_new(false));
1463 }
1464 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt