Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_bi_math.c File Reference
#include "duk_internal.h"

Go to the source code of this file.

Typedefs

typedef double(* duk__one_arg_func) (double)
 
typedef double(* duk__two_arg_func) (double, double)
 

Functions

DUK_LOCAL duk_ret_t duk__math_minmax (duk_context *ctx, duk_double_t initial, duk__two_arg_func min_max)
 
DUK_LOCAL double duk__fmin_fixed (double x, double y)
 
DUK_LOCAL double duk__fmax_fixed (double x, double y)
 
DUK_LOCAL double duk__round_fixed (double x)
 
DUK_LOCAL double duk__pow_fixed (double x, double y)
 
DUK_LOCAL double duk__fabs (double x)
 
DUK_LOCAL double duk__acos (double x)
 
DUK_LOCAL double duk__asin (double x)
 
DUK_LOCAL double duk__atan (double x)
 
DUK_LOCAL double duk__ceil (double x)
 
DUK_LOCAL double duk__cos (double x)
 
DUK_LOCAL double duk__exp (double x)
 
DUK_LOCAL double duk__floor (double x)
 
DUK_LOCAL double duk__log (double x)
 
DUK_LOCAL double duk__sin (double x)
 
DUK_LOCAL double duk__sqrt (double x)
 
DUK_LOCAL double duk__tan (double x)
 
DUK_LOCAL double duk__atan2 (double x, double y)
 
DUK_INTERNAL duk_ret_t duk_bi_math_object_onearg_shared (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_math_object_twoarg_shared (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_math_object_max (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_math_object_min (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_math_object_random (duk_context *ctx)
 

Variables

DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs []
 
DUK_LOCAL const duk__two_arg_func duk__two_arg_funcs []
 

Typedef Documentation

◆ duk__one_arg_func

typedef double(* duk__one_arg_func) (double)

Definition at line 18 of file duktape-1.8.0/src-separate/duk_bi_math.c.

◆ duk__two_arg_func

typedef double(* duk__two_arg_func) (double, double)

Definition at line 19 of file duktape-1.8.0/src-separate/duk_bi_math.c.

Function Documentation

◆ duk__acos()

DUK_LOCAL double duk__acos ( double x)

Definition at line 202 of file duktape-1.8.0/src-separate/duk_bi_math.c.

202 {
203 return DUK_ACOS(x);
204}

References DUK_ACOS.

◆ duk__asin()

DUK_LOCAL double duk__asin ( double x)

Definition at line 205 of file duktape-1.8.0/src-separate/duk_bi_math.c.

205 {
206 return DUK_ASIN(x);
207}

References DUK_ASIN.

◆ duk__atan()

DUK_LOCAL double duk__atan ( double x)

Definition at line 208 of file duktape-1.8.0/src-separate/duk_bi_math.c.

208 {
209 return DUK_ATAN(x);
210}

References DUK_ATAN.

◆ duk__atan2()

DUK_LOCAL double duk__atan2 ( double x,
double y )

Definition at line 235 of file duktape-1.8.0/src-separate/duk_bi_math.c.

235 {
236 return DUK_ATAN2(x, y);
237}

References DUK_ATAN2.

◆ duk__ceil()

DUK_LOCAL double duk__ceil ( double x)

Definition at line 211 of file duktape-1.8.0/src-separate/duk_bi_math.c.

211 {
212 return DUK_CEIL(x);
213}

References DUK_CEIL.

◆ duk__cos()

DUK_LOCAL double duk__cos ( double x)

Definition at line 214 of file duktape-1.8.0/src-separate/duk_bi_math.c.

214 {
215 return DUK_COS(x);
216}

References DUK_COS.

◆ duk__exp()

DUK_LOCAL double duk__exp ( double x)

Definition at line 217 of file duktape-1.8.0/src-separate/duk_bi_math.c.

217 {
218 return DUK_EXP(x);
219}

References DUK_EXP.

◆ duk__fabs()

DUK_LOCAL double duk__fabs ( double x)

Definition at line 199 of file duktape-1.8.0/src-separate/duk_bi_math.c.

199 {
200 return DUK_FABS(x);
201}

References DUK_FABS.

◆ duk__floor()

DUK_LOCAL double duk__floor ( double x)

Definition at line 220 of file duktape-1.8.0/src-separate/duk_bi_math.c.

220 {
221 return DUK_FLOOR(x);
222}

References DUK_FLOOR.

◆ duk__fmax_fixed()

DUK_LOCAL double duk__fmax_fixed ( double x,
double y )

Definition at line 71 of file duktape-1.8.0/src-separate/duk_bi_math.c.

71 {
72 /* fmax() with args -0 and +0 is not guaranteed to return
73 * +0 as Ecmascript requires.
74 */
75 if (x == 0 && y == 0) {
76 if (DUK_SIGNBIT(x) == 0 || DUK_SIGNBIT(y) == 0) {
77 return +0.0;
78 } else {
79 return -0.0;
80 }
81 }
82#ifdef DUK_USE_MATH_FMAX
83 return DUK_FMAX(x, y);
84#else
85 return (x > y ? x : y);
86#endif
87}

References DUK_FMAX, and DUK_SIGNBIT.

Referenced by duk_bi_math_object_max().

◆ duk__fmin_fixed()

DUK_LOCAL double duk__fmin_fixed ( double x,
double y )

Definition at line 52 of file duktape-1.8.0/src-separate/duk_bi_math.c.

52 {
53 /* fmin() with args -0 and +0 is not guaranteed to return
54 * -0 as Ecmascript requires.
55 */
56 if (x == 0 && y == 0) {
57 /* XXX: what's the safest way of creating a negative zero? */
58 if (DUK_SIGNBIT(x) != 0 || DUK_SIGNBIT(y) != 0) {
59 return -0.0;
60 } else {
61 return +0.0;
62 }
63 }
64#ifdef DUK_USE_MATH_FMIN
65 return DUK_FMIN(x, y);
66#else
67 return (x < y ? x : y);
68#endif
69}

References DUK_FMIN, and DUK_SIGNBIT.

Referenced by duk_bi_math_object_min().

◆ duk__log()

DUK_LOCAL double duk__log ( double x)

Definition at line 223 of file duktape-1.8.0/src-separate/duk_bi_math.c.

223 {
224 return DUK_LOG(x);
225}

References DUK_LOG.

◆ duk__math_minmax()

DUK_LOCAL duk_ret_t duk__math_minmax ( duk_context * ctx,
duk_double_t initial,
duk__two_arg_func min_max )

Definition at line 21 of file duktape-1.8.0/src-separate/duk_bi_math.c.

21 {
22 duk_idx_t n = duk_get_top(ctx);
23 duk_idx_t i;
24 duk_double_t res = initial;
26
27 /*
28 * Note: fmax() does not match the E5 semantics. E5 requires
29 * that if -any- input to Math.max() is a NaN, the result is a
30 * NaN. fmax() will return a NaN only if -both- inputs are NaN.
31 * Same applies to fmin().
32 *
33 * Note: every input value must be coerced with ToNumber(), even
34 * if we know the result will be a NaN anyway: ToNumber() may have
35 * side effects for which even order of evaluation matters.
36 */
37
38 for (i = 0; i < n; i++) {
39 t = duk_to_number(ctx, i);
41 /* Note: not normalized, but duk_push_number() will normalize */
43 } else {
44 res = (duk_double_t) min_max(res, (double) t);
45 }
46 }
47
48 duk_push_number(ctx, res);
49 return 1;
50}
DUK_EXTERNAL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx)
DUK_EXTERNAL void duk_push_number(duk_context *ctx, duk_double_t val)

References DUK_DOUBLE_NAN, DUK_FP_NAN, DUK_FPCLASSIFY, duk_get_top(), duk_push_number(), and duk_to_number().

Referenced by duk_bi_math_object_max(), and duk_bi_math_object_min().

◆ duk__pow_fixed()

DUK_LOCAL double duk__pow_fixed ( double x,
double y )

Definition at line 130 of file duktape-1.8.0/src-separate/duk_bi_math.c.

130 {
131 /* The ANSI C pow() semantics differ from Ecmascript.
132 *
133 * E.g. when x==1 and y is +/- infinite, the Ecmascript required
134 * result is NaN, while at least Linux pow() returns 1.
135 */
136
137 duk_small_int_t cx, cy, sx;
138
139 DUK_UNREF(cx);
140 DUK_UNREF(sx);
142
143 if (cy == DUK_FP_NAN) {
144 goto ret_nan;
145 }
146 if (DUK_FABS(x) == 1.0 && cy == DUK_FP_INFINITE) {
147 goto ret_nan;
148 }
149#if defined(DUK_USE_POW_NETBSD_WORKAROUND)
150 /* See test-bug-netbsd-math-pow.js: NetBSD 6.0 on x86 (at least) does not
151 * correctly handle some cases where x=+/-0. Specific fixes to these
152 * here.
153 */
155 if (cx == DUK_FP_ZERO && y < 0.0) {
157 if (sx == 0) {
158 /* Math.pow(+0,y) should be Infinity when y<0. NetBSD pow()
159 * returns -Infinity instead when y is <0 and finite. The
160 * if-clause also catches y == -Infinity (which works even
161 * without the fix).
162 */
163 return DUK_DOUBLE_INFINITY;
164 } else {
165 /* Math.pow(-0,y) where y<0 should be:
166 * - -Infinity if y<0 and an odd integer
167 * - Infinity otherwise
168 * NetBSD pow() returns -Infinity for all finite y<0. The
169 * if-clause also catches y == -Infinity (which works even
170 * without the fix).
171 */
172
173 /* fmod() return value has same sign as input (negative) so
174 * the result here will be in the range ]-2,0], 1 indicates
175 * odd. If x is -Infinity, NaN is returned and the odd check
176 * always concludes "not odd" which results in desired outcome.
177 */
178 double tmp = DUK_FMOD(y, 2);
179 if (tmp == -1.0) {
180 return -DUK_DOUBLE_INFINITY;
181 } else {
182 /* Not odd, or y == -Infinity */
183 return DUK_DOUBLE_INFINITY;
184 }
185 }
186 }
187#endif
188 return DUK_POW(x, y);
189
190 ret_nan:
191 return DUK_DOUBLE_NAN;
192}

References DUK_DOUBLE_INFINITY, DUK_DOUBLE_NAN, DUK_FABS, DUK_FMOD, DUK_FP_INFINITE, DUK_FP_NAN, DUK_FP_ZERO, DUK_FPCLASSIFY, DUK_POW, DUK_SIGNBIT, and DUK_UNREF.

◆ duk__round_fixed()

DUK_LOCAL double duk__round_fixed ( double x)

Definition at line 89 of file duktape-1.8.0/src-separate/duk_bi_math.c.

89 {
90 /* Numbers half-way between integers must be rounded towards +Infinity,
91 * e.g. -3.5 must be rounded to -3 (not -4). When rounded to zero, zero
92 * sign must be set appropriately. E5.1 Section 15.8.2.15.
93 *
94 * Note that ANSI C round() is "round to nearest integer, away from zero",
95 * which is incorrect for negative values. Here we make do with floor().
96 */
97
99 if (c == DUK_FP_NAN || c == DUK_FP_INFINITE || c == DUK_FP_ZERO) {
100 return x;
101 }
102
103 /*
104 * x is finite and non-zero
105 *
106 * -1.6 -> floor(-1.1) -> -2
107 * -1.5 -> floor(-1.0) -> -1 (towards +Inf)
108 * -1.4 -> floor(-0.9) -> -1
109 * -0.5 -> -0.0 (special case)
110 * -0.1 -> -0.0 (special case)
111 * +0.1 -> +0.0 (special case)
112 * +0.5 -> floor(+1.0) -> 1 (towards +Inf)
113 * +1.4 -> floor(+1.9) -> 1
114 * +1.5 -> floor(+2.0) -> 2 (towards +Inf)
115 * +1.6 -> floor(+2.1) -> 2
116 */
117
118 if (x >= -0.5 && x < 0.5) {
119 /* +0.5 is handled by floor, this is on purpose */
120 if (x < 0.0) {
121 return -0.0;
122 } else {
123 return +0.0;
124 }
125 }
126
127 return DUK_FLOOR(x + 0.5);
128}

References DUK_FLOOR, DUK_FP_INFINITE, DUK_FP_NAN, DUK_FP_ZERO, and DUK_FPCLASSIFY.

◆ duk__sin()

DUK_LOCAL double duk__sin ( double x)

Definition at line 226 of file duktape-1.8.0/src-separate/duk_bi_math.c.

226 {
227 return DUK_SIN(x);
228}

References DUK_SIN.

◆ duk__sqrt()

DUK_LOCAL double duk__sqrt ( double x)

Definition at line 229 of file duktape-1.8.0/src-separate/duk_bi_math.c.

229 {
230 return DUK_SQRT(x);
231}

References DUK_SQRT.

◆ duk__tan()

DUK_LOCAL double duk__tan ( double x)

Definition at line 232 of file duktape-1.8.0/src-separate/duk_bi_math.c.

232 {
233 return DUK_TAN(x);
234}

References DUK_TAN.

◆ duk_bi_math_object_max()

DUK_INTERNAL duk_ret_t duk_bi_math_object_max ( duk_context * ctx)

Definition at line 306 of file duktape-1.8.0/src-separate/duk_bi_math.c.

306 {
308}
DUK_LOCAL double duk__fmax_fixed(double x, double y)
DUK_LOCAL duk_ret_t duk__math_minmax(duk_context *ctx, duk_double_t initial, duk__two_arg_func min_max)

References duk__fmax_fixed(), duk__math_minmax(), and DUK_DOUBLE_INFINITY.

◆ duk_bi_math_object_min()

DUK_INTERNAL duk_ret_t duk_bi_math_object_min ( duk_context * ctx)

Definition at line 310 of file duktape-1.8.0/src-separate/duk_bi_math.c.

310 {
312}
DUK_LOCAL double duk__fmin_fixed(double x, double y)

References duk__fmin_fixed(), duk__math_minmax(), and DUK_DOUBLE_INFINITY.

◆ duk_bi_math_object_onearg_shared()

DUK_INTERNAL duk_ret_t duk_bi_math_object_onearg_shared ( duk_context * ctx)

Definition at line 284 of file duktape-1.8.0/src-separate/duk_bi_math.c.

284 {
287
288 DUK_ASSERT(fun_idx >= 0);
289 DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__one_arg_funcs) / sizeof(duk__one_arg_func)));
290 fun = duk__one_arg_funcs[fun_idx];
291 duk_push_number(ctx, (duk_double_t) fun((double) duk_to_number(ctx, 0)));
292 return 1;
293}
DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_context *ctx)
DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs[]
double(* duk__one_arg_func)(double)

References duk__one_arg_funcs, DUK_ASSERT, duk_get_current_magic(), duk_push_number(), and duk_to_number().

◆ duk_bi_math_object_random()

DUK_INTERNAL duk_ret_t duk_bi_math_object_random ( duk_context * ctx)

Definition at line 314 of file duktape-1.8.0/src-separate/duk_bi_math.c.

314 {
316 return 1;
317}
DUK_INTERNAL_DECL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr)

References duk_push_number(), and duk_util_tinyrandom_get_double().

◆ duk_bi_math_object_twoarg_shared()

DUK_INTERNAL duk_ret_t duk_bi_math_object_twoarg_shared ( duk_context * ctx)

Definition at line 295 of file duktape-1.8.0/src-separate/duk_bi_math.c.

295 {
298
299 DUK_ASSERT(fun_idx >= 0);
300 DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__two_arg_funcs) / sizeof(duk__two_arg_func)));
301 fun = duk__two_arg_funcs[fun_idx];
302 duk_push_number(ctx, (duk_double_t) fun((double) duk_to_number(ctx, 0), (double) duk_to_number(ctx, 1)));
303 return 1;
304}
DUK_LOCAL const duk__two_arg_func duk__two_arg_funcs[]
double(* duk__two_arg_func)(double, double)

References duk__two_arg_funcs, DUK_ASSERT, duk_get_current_magic(), duk_push_number(), and duk_to_number().

Variable Documentation

◆ duk__one_arg_funcs

DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs[]

Definition at line 241 of file duktape-1.8.0/src-separate/duk_bi_math.c.

241 {
242#if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
243 duk__fabs,
244 duk__acos,
245 duk__asin,
246 duk__atan,
247 duk__ceil,
248 duk__cos,
249 duk__exp,
251 duk__log,
253 duk__sin,
254 duk__sqrt,
256#else
257 DUK_FABS,
258 DUK_ACOS,
259 DUK_ASIN,
260 DUK_ATAN,
261 DUK_CEIL,
262 DUK_COS,
263 DUK_EXP,
264 DUK_FLOOR,
265 DUK_LOG,
267 DUK_SIN,
268 DUK_SQRT,
269 DUK_TAN
270#endif
271};
DUK_LOCAL double duk__asin(double x)
DUK_LOCAL double duk__atan(double x)
DUK_LOCAL double duk__log(double x)
DUK_LOCAL double duk__sin(double x)
DUK_LOCAL double duk__ceil(double x)
DUK_LOCAL double duk__floor(double x)
DUK_LOCAL double duk__exp(double x)
DUK_LOCAL double duk__round_fixed(double x)
DUK_LOCAL double duk__fabs(double x)
DUK_LOCAL double duk__acos(double x)
DUK_LOCAL double duk__tan(double x)
DUK_LOCAL double duk__cos(double x)
DUK_LOCAL double duk__sqrt(double x)

Referenced by duk_bi_math_object_onearg_shared().

◆ duk__two_arg_funcs

DUK_LOCAL const duk__two_arg_func duk__two_arg_funcs[]
Initial value:
= {
}
DUK_LOCAL double duk__pow_fixed(double x, double y)
DUK_LOCAL double duk__atan2(double x, double y)

Definition at line 274 of file duktape-1.8.0/src-separate/duk_bi_math.c.

274 {
275#if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
278#else
279 DUK_ATAN2,
281#endif
282};

Referenced by duk_bi_math_object_twoarg_shared().