Coverage Report

Created: 2026-11-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsMath.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2025  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime 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
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * This file incorporates work covered by the following copyright and  
20
 * permission notice:  
21
 *
22
 *       Copyright (C) 2010-2016 Marvell International Ltd.
23
 *       Copyright (C) 2002-2010 Kinoma, Inc.
24
 *
25
 *       Licensed under the Apache License, Version 2.0 (the "License");
26
 *       you may not use this file except in compliance with the License.
27
 *       You may obtain a copy of the License at
28
 *
29
 *        http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 *       Unless required by applicable law or agreed to in writing, software
32
 *       distributed under the License is distributed on an "AS IS" BASIS,
33
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 *       See the License for the specific language governing permissions and
35
 *       limitations under the License.
36
 */
37
38
#include "xsAll.h"
39
40
#include "xsum.c"
41
42
void fxBuildMath(txMachine* the)
43
6.45k
{
44
6.45k
  txSlot* slot;
45
6.45k
  mxPush(mxObjectPrototype);
46
6.45k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
47
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_abs), 1, mxID(_abs), XS_DONT_ENUM_FLAG);
48
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acos), 1, mxID(_acos), XS_DONT_ENUM_FLAG);
49
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acosh), 1, mxID(_acosh), XS_DONT_ENUM_FLAG);
50
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asin), 1, mxID(_asin), XS_DONT_ENUM_FLAG);
51
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asinh), 1, mxID(_asinh), XS_DONT_ENUM_FLAG);
52
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan), 1, mxID(_atan), XS_DONT_ENUM_FLAG);
53
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atanh), 1, mxID(_atanh), XS_DONT_ENUM_FLAG);
54
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan2), 2, mxID(_atan2), XS_DONT_ENUM_FLAG);
55
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cbrt), 1, mxID(_cbrt), XS_DONT_ENUM_FLAG);
56
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_ceil), 1, mxID(_ceil), XS_DONT_ENUM_FLAG);
57
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_clz32), 1, mxID(_clz32), XS_DONT_ENUM_FLAG);
58
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cos), 1, mxID(_cos), XS_DONT_ENUM_FLAG);
59
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cosh), 1, mxID(_cosh), XS_DONT_ENUM_FLAG);
60
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_exp), 1, mxID(_exp), XS_DONT_ENUM_FLAG);
61
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_expm1), 1, mxID(_expm1), XS_DONT_ENUM_FLAG);
62
6.45k
#if mxFloat16
63
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_f16round), 1, mxID(_f16round), XS_DONT_ENUM_FLAG);
64
6.45k
#endif
65
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_floor), 1, mxID(_floor), XS_DONT_ENUM_FLAG);
66
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_fround), 1, mxID(_fround), XS_DONT_ENUM_FLAG);
67
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_hypot), 2, mxID(_hypot_), XS_DONT_ENUM_FLAG);
68
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idiv), 2, mxID(_idiv), XS_DONT_ENUM_FLAG);
69
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idivmod), 2, mxID(_idivmod), XS_DONT_ENUM_FLAG);
70
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imod), 2, mxID(_imod), XS_DONT_ENUM_FLAG);
71
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imul), 2, mxID(_imul), XS_DONT_ENUM_FLAG);
72
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imuldiv), 2, mxID(_imuldiv), XS_DONT_ENUM_FLAG);
73
6.45k
#if mxECMAScript2023
74
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irandom), 0, mxID(_irandom), XS_DONT_ENUM_FLAG);
75
6.45k
#endif
76
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irem), 2, mxID(_irem), XS_DONT_ENUM_FLAG);
77
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log), 1, mxID(_log), XS_DONT_ENUM_FLAG);
78
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log1p), 1, mxID(_log1p), XS_DONT_ENUM_FLAG);
79
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log10), 1, mxID(_log10), XS_DONT_ENUM_FLAG);
80
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log2), 1, mxID(_log2), XS_DONT_ENUM_FLAG);
81
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_max), 2, mxID(_max), XS_DONT_ENUM_FLAG);
82
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_min), 2, mxID(_min), XS_DONT_ENUM_FLAG);
83
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_mod), 2, mxID(_mod), XS_DONT_ENUM_FLAG);
84
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_pow), 2, mxID(_pow), XS_DONT_ENUM_FLAG);
85
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_random), 0, mxID(_random), XS_DONT_ENUM_FLAG);
86
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_round), 1, mxID(_round), XS_DONT_ENUM_FLAG);
87
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sign), 1, mxID(_sign), XS_DONT_ENUM_FLAG);
88
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sin), 1, mxID(_sin), XS_DONT_ENUM_FLAG);
89
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sinh), 1, mxID(_sinh), XS_DONT_ENUM_FLAG);
90
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sqrt), 1, mxID(_sqrt), XS_DONT_ENUM_FLAG);
91
6.45k
#if mxECMAScript2026
92
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sumPrecise), 1, mxID(_sumPrecise), XS_DONT_ENUM_FLAG);
93
6.45k
#endif
94
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tan), 1, mxID(_tan), XS_DONT_ENUM_FLAG);
95
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tanh), 1, mxID(_tanh), XS_DONT_ENUM_FLAG);
96
6.45k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_trunc), 1, mxID(_trunc), XS_DONT_ENUM_FLAG);
97
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_E, mxID(_E), XS_GET_ONLY);
98
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_LN10, mxID(_LN10), XS_GET_ONLY);
99
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_LN2, mxID(_LN2), XS_GET_ONLY);
100
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_LOG10E, mxID(_LOG10E), XS_GET_ONLY);
101
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_LOG2E, mxID(_LOG2E), XS_GET_ONLY);
102
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_PI, mxID(_PI), XS_GET_ONLY);
103
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT1_2, mxID(_SQRT1_2), XS_GET_ONLY);
104
6.45k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT2, mxID(_SQRT2), XS_GET_ONLY);
105
6.45k
  slot = fxNextStringXProperty(the, slot, "Math", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
106
6.45k
  mxPull(mxMathObject);
107
//@@  c_srand((unsigned)c_time(0));
108
6.45k
}
109
110
#define mxNanResultIfNoArg \
111
0
  if (mxArgc < 1) {  \
112
0
    mxResult->kind = XS_NUMBER_KIND; \
113
0
    mxResult->value.number = C_NAN; \
114
0
    return; \
115
0
  }
116
117
#define mxNanResultIfNoArg2 \
118
0
  if (mxArgc < 2) {  \
119
0
    mxResult->kind = XS_NUMBER_KIND; \
120
0
    mxResult->value.number = C_NAN; \
121
0
    return; \
122
0
  }
123
124
void fx_Math_abs(txMachine* the)
125
0
{
126
0
  mxNanResultIfNoArg;
127
0
  fxToNumber(the, mxArgv(0));
128
0
  mxResult->kind = XS_NUMBER_KIND;
129
0
  mxResult->value.number = c_fabs(mxArgv(0)->value.number);
130
0
}
131
132
void fx_Math_acos(txMachine* the)
133
0
{
134
0
  mxNanResultIfNoArg;
135
0
  fxToNumber(the, mxArgv(0));
136
0
  mxResult->kind = XS_NUMBER_KIND;
137
0
  mxResult->value.number = c_acos(mxArgv(0)->value.number);
138
0
}
139
140
void fx_Math_acosh(txMachine* the)
141
0
{
142
0
  mxNanResultIfNoArg;
143
0
  fxToNumber(the, mxArgv(0));
144
0
  mxResult->kind = XS_NUMBER_KIND;
145
0
  mxResult->value.number = c_acosh(mxArgv(0)->value.number);
146
0
}
147
148
void fx_Math_asin(txMachine* the)
149
0
{
150
0
  mxNanResultIfNoArg;
151
0
  fxToNumber(the, mxArgv(0));
152
0
  mxResult->kind = XS_NUMBER_KIND;
153
0
  mxResult->value.number = c_asin(mxArgv(0)->value.number);
154
0
}
155
156
void fx_Math_asinh(txMachine* the)
157
0
{
158
0
  mxNanResultIfNoArg;
159
0
  fxToNumber(the, mxArgv(0));
160
0
  mxResult->kind = XS_NUMBER_KIND;
161
0
  mxResult->value.number = c_asinh(mxArgv(0)->value.number);
162
0
}
163
164
void fx_Math_atan(txMachine* the)
165
0
{
166
0
  mxNanResultIfNoArg;
167
0
  fxToNumber(the, mxArgv(0));
168
0
  mxResult->kind = XS_NUMBER_KIND;
169
0
  mxResult->value.number = c_atan(mxArgv(0)->value.number);
170
0
}
171
172
void fx_Math_atanh(txMachine* the)
173
0
{
174
0
  mxNanResultIfNoArg;
175
0
  fxToNumber(the, mxArgv(0));
176
0
  mxResult->kind = XS_NUMBER_KIND;
177
0
  mxResult->value.number = c_atanh(mxArgv(0)->value.number);
178
0
}
179
180
void fx_Math_atan2(txMachine* the)
181
0
{
182
0
  mxNanResultIfNoArg;
183
0
  fxToNumber(the, mxArgv(0));
184
0
  mxNanResultIfNoArg2;
185
0
  fxToNumber(the, mxArgv(1));
186
0
  mxResult->kind = XS_NUMBER_KIND;
187
0
  mxResult->value.number = c_atan2(mxArgv(0)->value.number, mxArgv(1)->value.number);
188
0
}
189
190
void fx_Math_cbrt(txMachine* the)
191
0
{
192
0
  mxNanResultIfNoArg;
193
0
  fxToNumber(the, mxArgv(0));
194
0
  mxResult->kind = XS_NUMBER_KIND;
195
0
  mxResult->value.number = c_cbrt(mxArgv(0)->value.number);
196
0
}
197
198
void fx_Math_ceil(txMachine* the)
199
0
{
200
0
  mxNanResultIfNoArg;
201
0
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
202
0
    mxResult->kind = XS_INTEGER_KIND;
203
0
    mxResult->value.integer = mxArgv(0)->value.integer;
204
0
    return;
205
0
  }
206
0
  fxToNumber(the, mxArgv(0));
207
0
  mxResult->kind = XS_NUMBER_KIND;
208
0
  mxResult->value.number = c_ceil(mxArgv(0)->value.number);
209
0
  fx_Math_toInteger(the);
210
0
}
211
212
void fx_Math_clz32(txMachine* the)
213
0
{
214
0
  txUnsigned x = (mxArgc > 0) ? fxToUnsigned(the, mxArgv(0)) : 0;
215
0
  txInteger r;
216
0
  if (x)
217
#if mxWindows
218
  {
219
    _BitScanReverse(&r, x);
220
    r = 31 - r;
221
  }
222
#else
223
0
    r = __builtin_clz(x);
224
0
#endif  
225
0
  else
226
0
    r = 32;
227
0
  mxResult->kind = XS_INTEGER_KIND;
228
0
  mxResult->value.integer = r;
229
0
}
230
231
void fx_Math_cos(txMachine* the)
232
0
{
233
0
  mxNanResultIfNoArg;
234
0
  fxToNumber(the, mxArgv(0));
235
0
  mxResult->kind = XS_NUMBER_KIND;
236
0
  mxResult->value.number = c_cos(mxArgv(0)->value.number);
237
0
}
238
239
void fx_Math_cosh(txMachine* the)
240
0
{
241
0
  mxNanResultIfNoArg;
242
0
  fxToNumber(the, mxArgv(0));
243
0
  mxResult->kind = XS_NUMBER_KIND;
244
0
  mxResult->value.number = c_cosh(mxArgv(0)->value.number);
245
0
}
246
247
void fx_Math_exp(txMachine* the)
248
0
{
249
0
  mxNanResultIfNoArg;
250
0
  fxToNumber(the, mxArgv(0));
251
0
  mxResult->kind = XS_NUMBER_KIND;
252
0
  mxResult->value.number = c_exp(mxArgv(0)->value.number);
253
0
}
254
255
void fx_Math_expm1(txMachine* the)
256
0
{
257
0
  mxNanResultIfNoArg;
258
0
  fxToNumber(the, mxArgv(0));
259
0
  mxResult->kind = XS_NUMBER_KIND;
260
0
  mxResult->value.number = c_expm1(mxArgv(0)->value.number);
261
0
}
262
263
void fx_Math_floor(txMachine* the)
264
0
{
265
0
  mxNanResultIfNoArg;
266
0
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
267
0
    mxResult->kind = XS_INTEGER_KIND;
268
0
    mxResult->value.integer = mxArgv(0)->value.integer;
269
0
    return;
270
0
  }
271
0
  fxToNumber(the, mxArgv(0));
272
0
  mxResult->kind = XS_NUMBER_KIND;
273
0
  mxResult->value.number = c_floor(mxArgv(0)->value.number);
274
0
  fx_Math_toInteger(the);
275
0
}
276
277
void fx_Math_fround(txMachine* the)
278
0
{
279
0
  float arg;
280
0
  mxNanResultIfNoArg;
281
0
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
282
0
    mxResult->kind = XS_INTEGER_KIND;
283
0
    mxResult->value.integer = mxArgv(0)->value.integer;
284
0
    return;
285
0
  }
286
0
  arg = (float)fxToNumber(the, mxArgv(0));
287
0
  mxResult->kind = XS_NUMBER_KIND;
288
0
  mxResult->value.number = arg;
289
0
}
290
291
void fx_Math_hypot(txMachine* the)
292
0
{
293
0
  if (mxArgc == 2) {
294
0
    fxToNumber(the, mxArgv(0));
295
0
    fxToNumber(the, mxArgv(1));
296
0
    mxResult->kind = XS_NUMBER_KIND;
297
0
    mxResult->value.number = c_hypot(mxArgv(0)->value.number, mxArgv(1)->value.number);
298
0
  }
299
0
  else {
300
0
    txInteger c = mxArgc, i;
301
0
    txNumber result = 0;
302
0
    for (i = 0; i < c; i++) {
303
0
      txNumber argument = fxToNumber(the, mxArgv(i));
304
0
      result += argument * argument;
305
0
    }
306
0
    mxResult->kind = XS_NUMBER_KIND;
307
0
    mxResult->value.number = c_sqrt(result);
308
0
  }
309
0
}
310
311
void fx_Math_idiv(txMachine* the)
312
0
{
313
0
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
314
0
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
315
0
  if (y == 0) {
316
0
    mxResult->kind = XS_NUMBER_KIND;
317
0
    mxResult->value.number = C_NAN;
318
0
  }
319
0
  else {
320
0
    mxResult->kind = XS_INTEGER_KIND;
321
0
#if mxIntegerDivideOverflowException
322
0
    if ((x == (txInteger)0x80000000) && (y == -1))
323
0
      mxResult->value.integer = x;
324
0
    else
325
0
#endif
326
0
      mxResult->value.integer = x / y;
327
0
  }
328
0
}
329
330
void fx_Math_idivmod(txMachine* the)
331
0
{
332
0
  mxTypeError("not available");
333
0
}
334
335
void fx_Math_imod(txMachine* the)
336
0
{
337
0
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
338
0
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
339
0
  if (y == 0) {
340
0
    mxResult->kind = XS_NUMBER_KIND;
341
0
    mxResult->value.number = C_NAN;
342
0
  }
343
0
  else {
344
0
    mxResult->kind = XS_INTEGER_KIND;
345
0
#if mxIntegerDivideOverflowException
346
0
    if ((x == (txInteger)0x80000000) && (y == -1))
347
0
      mxResult->value.integer = 0;
348
0
    else
349
0
#endif
350
0
    {
351
0
      txInteger r = x % y;
352
0
      if (r && ((r ^ y) < 0))
353
0
        r += y;
354
0
      mxResult->value.integer = r;
355
0
    }
356
0
  }
357
0
}
358
359
void fx_Math_imul(txMachine* the)
360
0
{
361
0
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
362
0
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
363
0
  mxResult->kind = XS_INTEGER_KIND;
364
0
  mxResult->value.integer = (txInteger)((uint32_t)x * (uint32_t)y);
365
0
}
366
367
void fx_Math_imuldiv(txMachine* the)
368
0
{
369
0
  txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
370
0
  txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
371
0
  txS8 z = (mxArgc > 2) ? fxToInteger(the, mxArgv(2)) : 0;
372
0
  if (z == 0) {
373
0
    mxResult->kind = XS_NUMBER_KIND;
374
0
    mxResult->value.number = C_NAN;
375
0
  }
376
0
  else {
377
0
    txS8 r = (x * y) / z;
378
0
    mxResult->kind = XS_INTEGER_KIND;
379
0
    mxResult->value.integer = (txInteger)(r & 0x00000000FFFFFFFF);
380
0
  }
381
0
}
382
383
void fx_Math_irandom(txMachine* the)
384
0
{
385
0
  txInteger min = 0;
386
0
  txInteger max = 2147483647;
387
0
  uint32_t result, range, delta;
388
0
  if (mxArgc > 1) {
389
0
    min = fxToInteger(the, mxArgv(0));
390
0
    max = fxToInteger(the, mxArgv(1));
391
0
  }
392
0
  else if (mxArgc > 0)
393
0
    max = fxToInteger(the, mxArgv(0));
394
0
  do  {
395
0
    result = c_rand();
396
0
  } while (result == C_RAND_MAX);
397
0
  if (max >= min) {
398
0
    range = (uint32_t)max - (uint32_t)min;
399
0
    delta = (uint32_t)(((uint64_t)result * (uint64_t)range) / (uint64_t)C_RAND_MAX);
400
0
    mxResult->value.integer = (txInteger)((uint32_t)min + delta);
401
0
  }
402
0
  else {
403
0
    range = (uint32_t)min - (uint32_t)max;
404
0
    delta = (uint32_t)(((uint64_t)result * (uint64_t)range) / (uint64_t)C_RAND_MAX);
405
0
    mxResult->value.integer = (txInteger)((uint32_t)min - delta);
406
0
  }
407
0
  mxResult->kind = XS_INTEGER_KIND;
408
0
}
409
410
void fx_Math_irandom_secure(txMachine* the)
411
0
{
412
0
  mxTypeError("secure mode");
413
0
}
414
415
void fx_Math_irem(txMachine* the)
416
0
{
417
0
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
418
0
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
419
0
  if (y == 0) {
420
0
    mxResult->kind = XS_NUMBER_KIND;
421
0
    mxResult->value.number = C_NAN;
422
0
  }
423
0
  else {
424
0
    mxResult->kind = XS_INTEGER_KIND;
425
0
#if mxIntegerDivideOverflowException
426
0
    if ((x == (txInteger)0x80000000) && (y == -1))
427
0
      mxResult->value.integer = 0;
428
0
    else
429
0
#endif
430
0
      mxResult->value.integer = x % y;
431
0
  }
432
0
}
433
434
void fx_Math_log(txMachine* the)
435
0
{
436
0
  mxNanResultIfNoArg;
437
0
  fxToNumber(the, mxArgv(0));
438
0
  mxResult->kind = XS_NUMBER_KIND;
439
0
  mxResult->value.number = c_log(mxArgv(0)->value.number);
440
0
}
441
442
void fx_Math_log1p(txMachine* the)
443
0
{
444
0
  mxNanResultIfNoArg;
445
0
  fxToNumber(the, mxArgv(0));
446
0
  mxResult->kind = XS_NUMBER_KIND;
447
0
  mxResult->value.number = c_log1p(mxArgv(0)->value.number);
448
0
}
449
450
void fx_Math_log10(txMachine* the)
451
0
{
452
0
  mxNanResultIfNoArg;
453
0
  fxToNumber(the, mxArgv(0));
454
0
  mxResult->kind = XS_NUMBER_KIND;
455
0
  mxResult->value.number = c_log10(mxArgv(0)->value.number);
456
0
}
457
458
void fx_Math_log2(txMachine* the)
459
0
{
460
0
  mxNanResultIfNoArg;
461
0
  fxToNumber(the, mxArgv(0));
462
0
  mxResult->kind = XS_NUMBER_KIND;
463
#if mxAndroid
464
  mxResult->value.number = c_log(mxArgv(0)->value.number) / c_log(2);
465
#else
466
0
  mxResult->value.number = c_log2(mxArgv(0)->value.number);
467
0
#endif
468
0
}
469
470
void fx_Math_max(txMachine* the)
471
0
{
472
0
  txInteger c = mxArgc, i = 0;
473
0
  mxResult->kind = XS_NUMBER_KIND;
474
0
  mxResult->value.number = -((txNumber)C_INFINITY);
475
0
  if (0 == c)
476
0
    return;
477
478
0
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
479
0
    mxResult->kind = XS_INTEGER_KIND;
480
0
    mxResult->value.integer = mxArgv(0)->value.integer;
481
0
    i = 1;
482
0
  }
483
484
0
  for (; i < c; i++) {
485
0
    txSlot *slot = mxArgv(i);
486
0
    if (XS_INTEGER_KIND == mxResult->kind) {
487
0
      if (XS_INTEGER_KIND == slot->kind) {
488
0
        if (mxResult->value.integer < slot->value.integer) {
489
0
          mxResult->value.integer = slot->value.integer;
490
0
        }
491
0
        continue;
492
0
      }
493
0
      mxResult->kind = XS_NUMBER_KIND;
494
0
      mxResult->value.number = mxResult->value.integer;
495
0
    }
496
497
0
    txNumber n = fxToNumber(the, slot);
498
0
    if (c_isnan(n)) {
499
0
      for (; i < c; i++)
500
0
        fxToNumber(the, mxArgv(i));
501
0
      mxResult->value.number = C_NAN;
502
0
      return;
503
0
    }
504
0
    if (mxResult->value.number < n)
505
0
      mxResult->value.number = n;
506
0
    else if ((mxResult->value.number == 0) && (n == 0)) {
507
0
      if (c_signbit(mxResult->value.number) != c_signbit(n))
508
0
        mxResult->value.number = 0;
509
0
    }
510
0
  }
511
0
}
512
513
void fx_Math_min(txMachine* the)
514
0
{
515
0
  txInteger c = mxArgc, i = 0;
516
0
  mxResult->kind = XS_NUMBER_KIND;
517
0
  mxResult->value.number = (txNumber)C_INFINITY;
518
0
  if (0 == c)
519
0
    return;
520
521
0
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
522
0
    mxResult->kind = XS_INTEGER_KIND;
523
0
    mxResult->value.integer = mxArgv(0)->value.integer;
524
0
    i = 1;
525
0
  }
526
527
0
  for (; i < c; i++) {
528
0
    txSlot *slot = mxArgv(i);
529
0
    if (XS_INTEGER_KIND == mxResult->kind) {
530
0
      if (XS_INTEGER_KIND == slot->kind) {
531
0
        if (mxResult->value.integer > slot->value.integer)
532
0
          mxResult->value.integer = slot->value.integer;
533
0
        continue;
534
0
      }
535
0
      mxResult->kind = XS_NUMBER_KIND;
536
0
      mxResult->value.number = mxResult->value.integer;
537
0
    }
538
  
539
0
    txNumber n = fxToNumber(the, slot);
540
0
    if (c_isnan(n)) {
541
0
      for (; i < c; i++)
542
0
        fxToNumber(the, mxArgv(i));
543
0
      mxResult->value.number = C_NAN;
544
0
      return;
545
0
    }
546
0
    if (mxResult->value.number > n)
547
0
      mxResult->value.number = n;
548
0
    else if ((mxResult->value.number == 0) && (n == 0)) {
549
0
      if (c_signbit(mxResult->value.number) != c_signbit(n))
550
0
        mxResult->value.number = -0.0;
551
0
    }
552
0
  }
553
0
}
554
555
void fx_Math_mod(txMachine* the)
556
0
{
557
0
  txNumber x = (mxArgc > 0) ? fxToNumber(the, mxArgv(0)) : 0;
558
0
  txNumber y = (mxArgc > 1) ? fxToNumber(the, mxArgv(1)) : 0;
559
0
  mxResult->kind = XS_NUMBER_KIND;
560
0
  mxResult->value.number = c_fmod((c_fmod(x, y) + y), y);
561
0
}
562
563
txNumber fx_pow(txNumber x, txNumber y)
564
0
{
565
0
  if (!c_isfinite(y) && (c_fabs(x) == 1.0))
566
0
    return C_NAN;
567
0
  return c_pow(x, y);
568
0
}
569
570
void fx_Math_pow(txMachine* the)
571
0
{
572
0
  txNumber x, y;
573
0
  mxNanResultIfNoArg;
574
0
  x = fxToNumber(the, mxArgv(0));
575
0
  mxNanResultIfNoArg2;
576
0
  y = fxToNumber(the, mxArgv(1));
577
0
  mxResult->kind = XS_NUMBER_KIND;
578
0
  mxResult->value.number = fx_pow(x, y);
579
0
}
580
581
void fx_Math_random(txMachine* the)
582
0
{
583
0
  uint32_t result;
584
0
  do {
585
0
    result = c_rand();
586
0
  } while (result == C_RAND_MAX);
587
0
  mxResult->kind = XS_NUMBER_KIND;
588
0
  mxResult->value.number = (double)result / (double)C_RAND_MAX;
589
0
}
590
591
void fx_Math_random_secure(txMachine* the)
592
0
{
593
0
  mxTypeError("secure mode");
594
0
}
595
596
void fx_Math_round(txMachine* the)
597
0
{
598
0
  txNumber arg;
599
0
  mxNanResultIfNoArg;
600
0
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
601
0
    mxResult->kind = XS_INTEGER_KIND;
602
0
    mxResult->value.integer = mxArgv(0)->value.integer;
603
0
    return;
604
0
  }
605
0
  arg = fxToNumber(the, mxArgv(0));
606
0
  if (c_isfinite(arg) && (-4503599627370495 < arg) && (arg < 4503599627370495)) { // 2 ** 52 - 1
607
0
    if ((arg < -0.5) || (0.5 <= arg))
608
0
      arg = c_floor(arg + 0.5);
609
0
    else if (arg < 0)
610
0
      arg = -0.0;
611
0
    else if (arg > 0)
612
0
      arg = 0.0;
613
0
    }
614
0
  mxResult->kind = XS_NUMBER_KIND;
615
0
  mxResult->value.number = arg;
616
0
  fx_Math_toInteger(the);
617
0
}
618
619
void fx_Math_sqrt(txMachine* the)
620
0
{
621
0
  mxNanResultIfNoArg;
622
0
  fxToNumber(the, mxArgv(0));
623
0
  mxResult->kind = XS_NUMBER_KIND;
624
0
  mxResult->value.number = c_sqrt(mxArgv(0)->value.number);
625
0
}
626
627
void fx_Math_sumPrecise(txMachine* the)
628
0
{
629
0
  txSlot *iterable, *iterator, *next, *value;
630
0
  xsum_small_accumulator accumulator;
631
0
  txInteger count = 0;
632
0
  txBoolean flag = 1;
633
0
  if (mxArgc < 1)
634
0
    mxTypeError("no items");
635
0
  iterable = mxArgv(0);
636
0
  fxToInstance(the, iterable);
637
0
  mxTemporary(iterator);
638
0
  mxTemporary(next);
639
0
  fxGetIterator(the, iterable, iterator, next, 0);  
640
0
  xsum_small_init(&accumulator);
641
0
  mxTemporary(value);
642
0
  while (fxIteratorNext(the, iterator, next, value)) {
643
0
    mxTry(the) {
644
0
      if (value->kind == XS_INTEGER_KIND) {
645
0
        flag = 0;
646
0
        xsum_small_add1(&accumulator, value->value.integer);
647
0
      }
648
0
      else if (value->kind == XS_NUMBER_KIND) {
649
0
        if (value->value.number != -0.0) {
650
0
          flag = 0;
651
0
          xsum_small_add1(&accumulator, value->value.number);
652
0
        }
653
0
      }
654
0
      else
655
0
        mxTypeError("items[%d]: not a number", count);
656
0
      count++;
657
0
    }
658
0
    mxCatch(the) {
659
0
      fxIteratorReturn(the, iterator, 1);
660
0
      fxJump(the);
661
0
    }
662
0
  }
663
0
  if (flag)
664
0
    mxResult->value.number = -0.0;
665
0
  else
666
0
    mxResult->value.number = xsum_small_round(&accumulator);
667
0
  mxResult->kind = XS_NUMBER_KIND;
668
0
}
669
670
void fx_Math_sign(txMachine* the)
671
0
{
672
0
  txNumber arg;
673
0
  mxNanResultIfNoArg;
674
0
  arg = fxToNumber(the, mxArgv(0));
675
0
  mxResult->kind = XS_NUMBER_KIND;
676
0
  if (c_isnan(arg))
677
0
    mxResult->value.number = C_NAN;
678
0
  else if (arg < 0)
679
0
    mxResult->value.number = -1;
680
0
  else if (arg > 0)
681
0
    mxResult->value.number = 1;
682
0
  else
683
0
    mxResult->value.number = arg;
684
0
  fx_Math_toInteger(the);
685
0
}
686
687
void fx_Math_sin(txMachine* the)
688
0
{
689
0
  mxNanResultIfNoArg;
690
0
  fxToNumber(the, mxArgv(0));
691
0
  mxResult->kind = XS_NUMBER_KIND;
692
0
  mxResult->value.number = c_sin(mxArgv(0)->value.number);
693
0
}
694
695
void fx_Math_sinh(txMachine* the)
696
0
{
697
0
  mxNanResultIfNoArg;
698
0
  fxToNumber(the, mxArgv(0));
699
0
  mxResult->kind = XS_NUMBER_KIND;
700
0
  mxResult->value.number = c_sinh(mxArgv(0)->value.number);
701
0
}
702
703
void fx_Math_tan(txMachine* the)
704
0
{
705
0
  mxNanResultIfNoArg;
706
0
  fxToNumber(the, mxArgv(0));
707
0
  mxResult->kind = XS_NUMBER_KIND;
708
0
  mxResult->value.number = c_tan(mxArgv(0)->value.number);
709
0
}
710
711
void fx_Math_tanh(txMachine* the)
712
0
{
713
0
  mxNanResultIfNoArg;
714
0
  fxToNumber(the, mxArgv(0));
715
0
  mxResult->kind = XS_NUMBER_KIND;
716
0
  mxResult->value.number = c_tanh(mxArgv(0)->value.number);
717
0
}
718
719
void fx_Math_trunc(txMachine* the)
720
0
{
721
0
  mxNanResultIfNoArg;
722
0
  fxToNumber(the, mxArgv(0));
723
0
  mxResult->kind = XS_NUMBER_KIND;
724
0
  mxResult->value.number = c_trunc(mxArgv(0)->value.number);
725
0
  fx_Math_toInteger(the);
726
0
}
727
728
void fx_Math_toInteger(txMachine* the)
729
0
{
730
0
  txNumber number = mxResult->value.number;
731
0
  txInteger integer = (txInteger)number;
732
0
  txNumber check = integer;
733
0
  if ((number == check) && (number || !c_signbit(number))) {
734
0
    mxResult->value.integer = integer;
735
0
    mxResult->kind = XS_INTEGER_KIND;
736
0
  }
737
0
}