Coverage Report

Created: 2026-03-16 06:17

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
26.8k
{
44
26.8k
  txSlot* slot;
45
26.8k
  mxPush(mxObjectPrototype);
46
26.8k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
47
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_abs), 1, mxID(_abs), XS_DONT_ENUM_FLAG);
48
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acos), 1, mxID(_acos), XS_DONT_ENUM_FLAG);
49
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acosh), 1, mxID(_acosh), XS_DONT_ENUM_FLAG);
50
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asin), 1, mxID(_asin), XS_DONT_ENUM_FLAG);
51
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asinh), 1, mxID(_asinh), XS_DONT_ENUM_FLAG);
52
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan), 1, mxID(_atan), XS_DONT_ENUM_FLAG);
53
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atanh), 1, mxID(_atanh), XS_DONT_ENUM_FLAG);
54
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan2), 2, mxID(_atan2), XS_DONT_ENUM_FLAG);
55
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cbrt), 1, mxID(_cbrt), XS_DONT_ENUM_FLAG);
56
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_ceil), 1, mxID(_ceil), XS_DONT_ENUM_FLAG);
57
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_clz32), 1, mxID(_clz32), XS_DONT_ENUM_FLAG);
58
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cos), 1, mxID(_cos), XS_DONT_ENUM_FLAG);
59
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cosh), 1, mxID(_cosh), XS_DONT_ENUM_FLAG);
60
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_exp), 1, mxID(_exp), XS_DONT_ENUM_FLAG);
61
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_expm1), 1, mxID(_expm1), XS_DONT_ENUM_FLAG);
62
26.8k
#if mxFloat16
63
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_f16round), 1, mxID(_f16round), XS_DONT_ENUM_FLAG);
64
26.8k
#endif
65
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_floor), 1, mxID(_floor), XS_DONT_ENUM_FLAG);
66
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_fround), 1, mxID(_fround), XS_DONT_ENUM_FLAG);
67
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_hypot), 2, mxID(_hypot_), XS_DONT_ENUM_FLAG);
68
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idiv), 2, mxID(_idiv), XS_DONT_ENUM_FLAG);
69
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idivmod), 2, mxID(_idivmod), XS_DONT_ENUM_FLAG);
70
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imod), 2, mxID(_imod), XS_DONT_ENUM_FLAG);
71
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imul), 2, mxID(_imul), XS_DONT_ENUM_FLAG);
72
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imuldiv), 2, mxID(_imuldiv), XS_DONT_ENUM_FLAG);
73
26.8k
#if mxECMAScript2023
74
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irandom), 0, mxID(_irandom), XS_DONT_ENUM_FLAG);
75
26.8k
#endif
76
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irem), 2, mxID(_irem), XS_DONT_ENUM_FLAG);
77
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log), 1, mxID(_log), XS_DONT_ENUM_FLAG);
78
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log1p), 1, mxID(_log1p), XS_DONT_ENUM_FLAG);
79
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log10), 1, mxID(_log10), XS_DONT_ENUM_FLAG);
80
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log2), 1, mxID(_log2), XS_DONT_ENUM_FLAG);
81
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_max), 2, mxID(_max), XS_DONT_ENUM_FLAG);
82
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_min), 2, mxID(_min), XS_DONT_ENUM_FLAG);
83
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_mod), 2, mxID(_mod), XS_DONT_ENUM_FLAG);
84
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_pow), 2, mxID(_pow), XS_DONT_ENUM_FLAG);
85
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_random), 0, mxID(_random), XS_DONT_ENUM_FLAG);
86
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_round), 1, mxID(_round), XS_DONT_ENUM_FLAG);
87
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sign), 1, mxID(_sign), XS_DONT_ENUM_FLAG);
88
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sin), 1, mxID(_sin), XS_DONT_ENUM_FLAG);
89
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sinh), 1, mxID(_sinh), XS_DONT_ENUM_FLAG);
90
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sqrt), 1, mxID(_sqrt), XS_DONT_ENUM_FLAG);
91
26.8k
#if mxECMAScript2026
92
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sumPrecise), 1, mxID(_sumPrecise), XS_DONT_ENUM_FLAG);
93
26.8k
#endif
94
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tan), 1, mxID(_tan), XS_DONT_ENUM_FLAG);
95
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tanh), 1, mxID(_tanh), XS_DONT_ENUM_FLAG);
96
26.8k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_trunc), 1, mxID(_trunc), XS_DONT_ENUM_FLAG);
97
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_E, mxID(_E), XS_GET_ONLY);
98
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_LN10, mxID(_LN10), XS_GET_ONLY);
99
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_LN2, mxID(_LN2), XS_GET_ONLY);
100
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_LOG10E, mxID(_LOG10E), XS_GET_ONLY);
101
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_LOG2E, mxID(_LOG2E), XS_GET_ONLY);
102
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_PI, mxID(_PI), XS_GET_ONLY);
103
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT1_2, mxID(_SQRT1_2), XS_GET_ONLY);
104
26.8k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT2, mxID(_SQRT2), XS_GET_ONLY);
105
26.8k
  slot = fxNextStringXProperty(the, slot, "Math", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
106
26.8k
  mxPull(mxMathObject);
107
//@@  c_srand((unsigned)c_time(0));
108
26.8k
}
109
110
#define mxNanResultIfNoArg \
111
1.47M
  if (mxArgc < 1) {  \
112
11.2k
    mxResult->kind = XS_NUMBER_KIND; \
113
11.2k
    mxResult->value.number = C_NAN; \
114
11.2k
    return; \
115
11.2k
  }
116
117
#define mxNanResultIfNoArg2 \
118
579k
  if (mxArgc < 2) {  \
119
20
    mxResult->kind = XS_NUMBER_KIND; \
120
20
    mxResult->value.number = C_NAN; \
121
20
    return; \
122
20
  }
123
124
void fx_Math_abs(txMachine* the)
125
18
{
126
18
  mxNanResultIfNoArg;
127
18
  fxToNumber(the, mxArgv(0));
128
18
  mxResult->kind = XS_NUMBER_KIND;
129
18
  mxResult->value.number = c_fabs(mxArgv(0)->value.number);
130
18
}
131
132
void fx_Math_acos(txMachine* the)
133
9.73k
{
134
9.73k
  mxNanResultIfNoArg;
135
5.67k
  fxToNumber(the, mxArgv(0));
136
5.67k
  mxResult->kind = XS_NUMBER_KIND;
137
5.67k
  mxResult->value.number = c_acos(mxArgv(0)->value.number);
138
5.67k
}
139
140
void fx_Math_acosh(txMachine* the)
141
6.45k
{
142
6.45k
  mxNanResultIfNoArg;
143
5.90k
  fxToNumber(the, mxArgv(0));
144
5.90k
  mxResult->kind = XS_NUMBER_KIND;
145
5.90k
  mxResult->value.number = c_acosh(mxArgv(0)->value.number);
146
5.90k
}
147
148
void fx_Math_asin(txMachine* the)
149
5.26k
{
150
5.26k
  mxNanResultIfNoArg;
151
4.24k
  fxToNumber(the, mxArgv(0));
152
4.24k
  mxResult->kind = XS_NUMBER_KIND;
153
4.24k
  mxResult->value.number = c_asin(mxArgv(0)->value.number);
154
4.24k
}
155
156
void fx_Math_asinh(txMachine* the)
157
57.2k
{
158
57.2k
  mxNanResultIfNoArg;
159
57.2k
  fxToNumber(the, mxArgv(0));
160
57.2k
  mxResult->kind = XS_NUMBER_KIND;
161
57.2k
  mxResult->value.number = c_asinh(mxArgv(0)->value.number);
162
57.2k
}
163
164
void fx_Math_atan(txMachine* the)
165
3.41k
{
166
3.41k
  mxNanResultIfNoArg;
167
1.81k
  fxToNumber(the, mxArgv(0));
168
1.81k
  mxResult->kind = XS_NUMBER_KIND;
169
1.81k
  mxResult->value.number = c_atan(mxArgv(0)->value.number);
170
1.81k
}
171
172
void fx_Math_atanh(txMachine* the)
173
255k
{
174
255k
  mxNanResultIfNoArg;
175
255k
  fxToNumber(the, mxArgv(0));
176
255k
  mxResult->kind = XS_NUMBER_KIND;
177
255k
  mxResult->value.number = c_atanh(mxArgv(0)->value.number);
178
255k
}
179
180
void fx_Math_atan2(txMachine* the)
181
518k
{
182
518k
  mxNanResultIfNoArg2;
183
518k
  fxToNumber(the, mxArgv(0));
184
518k
  fxToNumber(the, mxArgv(1));
185
518k
  mxResult->kind = XS_NUMBER_KIND;
186
518k
  mxResult->value.number = c_atan2(mxArgv(0)->value.number, mxArgv(1)->value.number);
187
518k
}
188
189
void fx_Math_cbrt(txMachine* the)
190
11
{
191
11
  mxNanResultIfNoArg;
192
11
  fxToNumber(the, mxArgv(0));
193
11
  mxResult->kind = XS_NUMBER_KIND;
194
11
  mxResult->value.number = c_cbrt(mxArgv(0)->value.number);
195
11
}
196
197
void fx_Math_ceil(txMachine* the)
198
77.3k
{
199
77.3k
  mxNanResultIfNoArg;
200
77.3k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
201
3
    mxResult->kind = XS_INTEGER_KIND;
202
3
    mxResult->value.integer = mxArgv(0)->value.integer;
203
3
    return;
204
3
  }
205
77.3k
  fxToNumber(the, mxArgv(0));
206
77.3k
  mxResult->kind = XS_NUMBER_KIND;
207
77.3k
  mxResult->value.number = c_ceil(mxArgv(0)->value.number);
208
77.3k
  fx_Math_toInteger(the);
209
77.3k
}
210
211
void fx_Math_clz32(txMachine* the)
212
57
{
213
57
  txUnsigned x = (mxArgc > 0) ? fxToUnsigned(the, mxArgv(0)) : 0;
214
57
  txInteger r;
215
57
  if (x)
216
#if mxWindows
217
  {
218
    _BitScanReverse(&r, x);
219
    r = 31 - r;
220
  }
221
#else
222
45
    r = __builtin_clz(x);
223
12
#endif  
224
12
  else
225
12
    r = 32;
226
57
  mxResult->kind = XS_INTEGER_KIND;
227
57
  mxResult->value.integer = r;
228
57
}
229
230
void fx_Math_cos(txMachine* the)
231
448k
{
232
448k
  mxNanResultIfNoArg;
233
448k
  fxToNumber(the, mxArgv(0));
234
448k
  mxResult->kind = XS_NUMBER_KIND;
235
448k
  mxResult->value.number = c_cos(mxArgv(0)->value.number);
236
448k
}
237
238
void fx_Math_cosh(txMachine* the)
239
3.39k
{
240
3.39k
  mxNanResultIfNoArg;
241
2.94k
  fxToNumber(the, mxArgv(0));
242
2.94k
  mxResult->kind = XS_NUMBER_KIND;
243
2.94k
  mxResult->value.number = c_cosh(mxArgv(0)->value.number);
244
2.94k
}
245
246
void fx_Math_exp(txMachine* the)
247
401k
{
248
401k
  mxNanResultIfNoArg;
249
400k
  fxToNumber(the, mxArgv(0));
250
400k
  mxResult->kind = XS_NUMBER_KIND;
251
400k
  mxResult->value.number = c_exp(mxArgv(0)->value.number);
252
400k
}
253
254
void fx_Math_expm1(txMachine* the)
255
20
{
256
20
  mxNanResultIfNoArg;
257
20
  fxToNumber(the, mxArgv(0));
258
20
  mxResult->kind = XS_NUMBER_KIND;
259
20
  mxResult->value.number = c_expm1(mxArgv(0)->value.number);
260
20
}
261
262
void fx_Math_floor(txMachine* the)
263
83.3k
{
264
83.3k
  mxNanResultIfNoArg;
265
83.3k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
266
10
    mxResult->kind = XS_INTEGER_KIND;
267
10
    mxResult->value.integer = mxArgv(0)->value.integer;
268
10
    return;
269
10
  }
270
83.3k
  fxToNumber(the, mxArgv(0));
271
83.3k
  mxResult->kind = XS_NUMBER_KIND;
272
83.3k
  mxResult->value.number = c_floor(mxArgv(0)->value.number);
273
83.3k
  fx_Math_toInteger(the);
274
83.3k
}
275
276
void fx_Math_fround(txMachine* the)
277
56
{
278
56
  float arg;
279
56
  mxNanResultIfNoArg;
280
53
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
281
6
    mxResult->kind = XS_INTEGER_KIND;
282
6
    mxResult->value.integer = mxArgv(0)->value.integer;
283
6
    return;
284
6
  }
285
47
  arg = (float)fxToNumber(the, mxArgv(0));
286
47
  mxResult->kind = XS_NUMBER_KIND;
287
47
  mxResult->value.number = arg;
288
47
}
289
290
void fx_Math_hypot(txMachine* the)
291
48
{
292
48
  if (mxArgc == 2) {
293
16
    fxToNumber(the, mxArgv(0));
294
16
    fxToNumber(the, mxArgv(1));
295
16
    mxResult->kind = XS_NUMBER_KIND;
296
16
    mxResult->value.number = c_hypot(mxArgv(0)->value.number, mxArgv(1)->value.number);
297
16
  }
298
32
  else {
299
32
    txInteger c = mxArgc, i;
300
32
    txNumber result = 0;
301
67
    for (i = 0; i < c; i++) {
302
35
      txNumber argument = fxToNumber(the, mxArgv(i));
303
35
      result += argument * argument;
304
35
    }
305
32
    mxResult->kind = XS_NUMBER_KIND;
306
32
    mxResult->value.number = c_sqrt(result);
307
32
  }
308
48
}
309
310
void fx_Math_idiv(txMachine* the)
311
26.2k
{
312
26.2k
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
313
26.2k
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
314
26.2k
  if (y == 0) {
315
25.7k
    mxResult->kind = XS_NUMBER_KIND;
316
25.7k
    mxResult->value.number = C_NAN;
317
25.7k
  }
318
524
  else {
319
524
    mxResult->kind = XS_INTEGER_KIND;
320
524
#if mxIntegerDivideOverflowException
321
524
    if ((x == (txInteger)0x80000000) && (y == -1))
322
5
      mxResult->value.integer = x;
323
519
    else
324
519
#endif
325
519
      mxResult->value.integer = x / y;
326
524
  }
327
26.2k
}
328
329
void fx_Math_idivmod(txMachine* the)
330
0
{
331
0
  mxTypeError("not available");
332
0
}
333
334
void fx_Math_imod(txMachine* the)
335
0
{
336
0
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
337
0
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
338
0
  if (y == 0) {
339
0
    mxResult->kind = XS_NUMBER_KIND;
340
0
    mxResult->value.number = C_NAN;
341
0
  }
342
0
  else {
343
0
    mxResult->kind = XS_INTEGER_KIND;
344
0
#if mxIntegerDivideOverflowException
345
0
    if ((x == (txInteger)0x80000000) && (y == -1))
346
0
      mxResult->value.integer = 0;
347
0
    else
348
0
#endif
349
0
      mxResult->value.integer = (x % y + y) % y;
350
0
  }
351
0
}
352
353
void fx_Math_imul(txMachine* the)
354
26.1k
{
355
26.1k
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
356
26.1k
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
357
26.1k
  mxResult->kind = XS_INTEGER_KIND;
358
26.1k
  mxResult->value.integer = x * y;
359
26.1k
}
360
361
void fx_Math_imuldiv(txMachine* the)
362
52.3k
{
363
52.3k
  txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
364
52.3k
  txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
365
52.3k
  txS8 z = (mxArgc > 2) ? fxToInteger(the, mxArgv(2)) : 0;
366
52.3k
  if (z == 0) {
367
26.0k
    mxResult->kind = XS_NUMBER_KIND;
368
26.0k
    mxResult->value.number = C_NAN;
369
26.0k
  }
370
26.2k
  else {
371
26.2k
    txS8 r = (x * y) / z;
372
26.2k
    mxResult->kind = XS_INTEGER_KIND;
373
26.2k
    mxResult->value.integer = (txInteger)(r & 0x00000000FFFFFFFF);
374
26.2k
  }
375
52.3k
}
376
377
void fx_Math_irandom(txMachine* the)
378
0
{
379
0
  double min = 0;
380
0
  double max = 2147483647;
381
0
  uint32_t result;
382
0
  if (mxArgc > 1) {
383
0
    min = (double)fxToInteger(the, mxArgv(0));
384
0
    max = (double)fxToInteger(the, mxArgv(1));
385
0
  }
386
0
  else if (mxArgc > 0) {
387
0
    max = (double)fxToInteger(the, mxArgv(0));
388
0
  }
389
0
  result = c_rand();
390
0
  while (result == C_RAND_MAX)
391
0
    result = c_rand();
392
0
  if (max < min)
393
0
    mxResult->value.integer = (txInteger)c_ceil(min + (((double)result / (double)C_RAND_MAX) * (max - min)));
394
0
  else
395
0
    mxResult->value.integer = (txInteger)c_floor(min + (((double)result / (double)C_RAND_MAX) * (max - min)));
396
0
  mxResult->kind = XS_INTEGER_KIND;
397
0
}
398
399
void fx_Math_irandom_secure(txMachine* the)
400
0
{
401
0
  mxTypeError("secure mode");
402
0
}
403
404
void fx_Math_irem(txMachine* the)
405
0
{
406
0
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
407
0
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
408
0
  if (y == 0) {
409
0
    mxResult->kind = XS_NUMBER_KIND;
410
0
    mxResult->value.number = C_NAN;
411
0
  }
412
0
  else {
413
0
    mxResult->kind = XS_INTEGER_KIND;
414
0
#if mxIntegerDivideOverflowException
415
0
    if ((x == (txInteger)0x80000000) && (y == -1))
416
0
      mxResult->value.integer = 0;
417
0
    else
418
0
#endif
419
0
      mxResult->value.integer = x % y;
420
0
  }
421
0
}
422
423
void fx_Math_log(txMachine* the)
424
26
{
425
26
  mxNanResultIfNoArg;
426
25
  fxToNumber(the, mxArgv(0));
427
25
  mxResult->kind = XS_NUMBER_KIND;
428
25
  mxResult->value.number = c_log(mxArgv(0)->value.number);
429
25
}
430
431
void fx_Math_log1p(txMachine* the)
432
960
{
433
960
  mxNanResultIfNoArg;
434
959
  fxToNumber(the, mxArgv(0));
435
959
  mxResult->kind = XS_NUMBER_KIND;
436
959
  mxResult->value.number = c_log1p(mxArgv(0)->value.number);
437
959
}
438
439
void fx_Math_log10(txMachine* the)
440
156
{
441
156
  mxNanResultIfNoArg;
442
155
  fxToNumber(the, mxArgv(0));
443
155
  mxResult->kind = XS_NUMBER_KIND;
444
155
  mxResult->value.number = c_log10(mxArgv(0)->value.number);
445
155
}
446
447
void fx_Math_log2(txMachine* the)
448
26
{
449
26
  mxNanResultIfNoArg;
450
26
  fxToNumber(the, mxArgv(0));
451
26
  mxResult->kind = XS_NUMBER_KIND;
452
#if mxAndroid
453
  mxResult->value.number = c_log(mxArgv(0)->value.number) / c_log(2);
454
#else
455
26
  mxResult->value.number = c_log2(mxArgv(0)->value.number);
456
26
#endif
457
26
}
458
459
void fx_Math_max(txMachine* the)
460
6.87k
{
461
6.87k
  txInteger c = mxArgc, i = 0;
462
6.87k
  mxResult->kind = XS_NUMBER_KIND;
463
6.87k
  mxResult->value.number = -((txNumber)C_INFINITY);
464
6.87k
  if (0 == c)
465
212
    return;
466
467
6.66k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
468
1.29k
    mxResult->kind = XS_INTEGER_KIND;
469
1.29k
    mxResult->value.integer = mxArgv(0)->value.integer;
470
1.29k
    i = 1;
471
1.29k
  }
472
473
22.6k
  for (; i < c; i++) {
474
16.6k
    txSlot *slot = mxArgv(i);
475
16.6k
    if (XS_INTEGER_KIND == mxResult->kind) {
476
1.52k
      if (XS_INTEGER_KIND == slot->kind) {
477
1.24k
        if (mxResult->value.integer < slot->value.integer) {
478
799
          mxResult->value.integer = slot->value.integer;
479
799
        }
480
1.24k
        continue;
481
1.24k
      }
482
279
      mxResult->kind = XS_NUMBER_KIND;
483
279
      mxResult->value.number = mxResult->value.integer;
484
279
    }
485
486
15.3k
    txNumber n = fxToNumber(the, slot);
487
15.3k
    if (c_isnan(n)) {
488
1.93k
      for (; i < c; i++)
489
1.29k
        fxToNumber(the, mxArgv(i));
490
642
      mxResult->value.number = C_NAN;
491
642
      return;
492
642
    }
493
14.7k
    if (mxResult->value.number < n)
494
9.44k
      mxResult->value.number = n;
495
5.29k
    else if ((mxResult->value.number == 0) && (n == 0)) {
496
344
      if (c_signbit(mxResult->value.number) != c_signbit(n))
497
41
        mxResult->value.number = 0;
498
344
    }
499
14.7k
  }
500
6.66k
}
501
502
void fx_Math_min(txMachine* the)
503
2.86k
{
504
2.86k
  txInteger c = mxArgc, i = 0;
505
2.86k
  mxResult->kind = XS_NUMBER_KIND;
506
2.86k
  mxResult->value.number = (txNumber)C_INFINITY;
507
2.86k
  if (0 == c)
508
1.50k
    return;
509
510
1.36k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
511
804
    mxResult->kind = XS_INTEGER_KIND;
512
804
    mxResult->value.integer = mxArgv(0)->value.integer;
513
804
    i = 1;
514
804
  }
515
516
3.19k
  for (; i < c; i++) {
517
2.70k
    txSlot *slot = mxArgv(i);
518
2.70k
    if (XS_INTEGER_KIND == mxResult->kind) {
519
883
      if (XS_INTEGER_KIND == slot->kind) {
520
361
        if (mxResult->value.integer > slot->value.integer)
521
163
          mxResult->value.integer = slot->value.integer;
522
361
        continue;
523
361
      }
524
522
      mxResult->kind = XS_NUMBER_KIND;
525
522
      mxResult->value.number = mxResult->value.integer;
526
522
    }
527
  
528
2.33k
    txNumber n = fxToNumber(the, slot);
529
2.33k
    if (c_isnan(n)) {
530
2.13k
      for (; i < c; i++)
531
1.27k
        fxToNumber(the, mxArgv(i));
532
867
      mxResult->value.number = C_NAN;
533
867
      return;
534
867
    }
535
1.47k
    if (mxResult->value.number > n)
536
901
      mxResult->value.number = n;
537
571
    else if ((mxResult->value.number == 0) && (n == 0)) {
538
140
      if (c_signbit(mxResult->value.number) != c_signbit(n))
539
64
        mxResult->value.number = -0.0;
540
140
    }
541
1.47k
  }
542
1.36k
}
543
544
void fx_Math_mod(txMachine* the)
545
0
{
546
0
  txNumber x = (mxArgc > 0) ? fxToNumber(the, mxArgv(0)) : 0;
547
0
  txNumber y = (mxArgc > 1) ? fxToNumber(the, mxArgv(1)) : 0;
548
0
  mxResult->kind = XS_NUMBER_KIND;
549
0
  mxResult->value.number = c_fmod((c_fmod(x, y) + y), y);
550
0
}
551
552
txNumber fx_pow(txNumber x, txNumber y)
553
2.50M
{
554
2.50M
  if (!c_isfinite(y) && (c_fabs(x) == 1.0))
555
183
    return C_NAN;
556
2.50M
  return c_pow(x, y);
557
2.50M
}
558
559
void fx_Math_pow(txMachine* the)
560
61.1k
{
561
61.1k
  txNumber x, y;
562
61.1k
  mxNanResultIfNoArg2;
563
61.1k
  x = fxToNumber(the, mxArgv(0));
564
61.1k
  y = fxToNumber(the, mxArgv(1));
565
61.1k
  mxResult->kind = XS_NUMBER_KIND;
566
61.1k
  mxResult->value.number = fx_pow(x, y);
567
61.1k
}
568
569
void fx_Math_random(txMachine* the)
570
103
{
571
103
  uint32_t result;
572
103
  do {
573
103
    result = c_rand();
574
103
  } while (result == C_RAND_MAX);
575
103
  mxResult->kind = XS_NUMBER_KIND;
576
103
  mxResult->value.number = (double)result / (double)C_RAND_MAX;
577
103
}
578
579
void fx_Math_random_secure(txMachine* the)
580
0
{
581
0
  mxTypeError("secure mode");
582
0
}
583
584
void fx_Math_round(txMachine* the)
585
6.06k
{
586
6.06k
    txNumber arg;
587
6.06k
  mxNanResultIfNoArg;
588
6.06k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
589
6
    mxResult->kind = XS_INTEGER_KIND;
590
6
    mxResult->value.integer = mxArgv(0)->value.integer;
591
6
    return;
592
6
  }
593
6.06k
  arg = fxToNumber(the, mxArgv(0));
594
6.06k
  if (c_isnormal(arg) && (-4503599627370495 < arg) && (arg < 4503599627370495)) { // 2 ** 52 - 1
595
5.02k
    if ((arg < -0.5) || (0.5 <= arg))
596
4.91k
      arg = c_floor(arg + 0.5);
597
105
    else if (arg < 0)
598
49
      arg = -0.0;
599
56
    else if (arg > 0)
600
56
      arg = 0.0;
601
5.02k
    }
602
6.06k
  mxResult->kind = XS_NUMBER_KIND;
603
6.06k
  mxResult->value.number = arg;
604
6.06k
  fx_Math_toInteger(the);
605
6.06k
}
606
607
void fx_Math_sqrt(txMachine* the)
608
1.26k
{
609
1.26k
  mxNanResultIfNoArg;
610
1.26k
  fxToNumber(the, mxArgv(0));
611
1.26k
  mxResult->kind = XS_NUMBER_KIND;
612
1.26k
  mxResult->value.number = c_sqrt(mxArgv(0)->value.number);
613
1.26k
}
614
615
void fx_Math_sumPrecise(txMachine* the)
616
755
{
617
755
  txSlot *iterable, *iterator, *next, *value;
618
755
  xsum_small_accumulator accumulator;
619
755
  txInteger count = 0;
620
755
  txBoolean flag = 1;
621
755
  if (mxArgc < 1)
622
1
    mxTypeError("no items");
623
754
  iterable = mxArgv(0);
624
754
  fxToInstance(the, iterable);
625
754
  mxTemporary(iterator);
626
754
  mxTemporary(next);
627
754
  fxGetIterator(the, iterable, iterator, next, 0);  
628
754
  xsum_small_init(&accumulator);
629
754
  mxTemporary(value);
630
3.25k
  while (fxIteratorNext(the, iterator, next, value)) {
631
2.53k
    mxTry(the) {
632
2.53k
      if (value->kind == XS_INTEGER_KIND) {
633
242
        flag = 0;
634
242
        xsum_small_add1(&accumulator, value->value.integer);
635
242
      }
636
2.28k
      else if (value->kind == XS_NUMBER_KIND) {
637
2.27k
        if (value->value.number != -0.0) {
638
2.26k
          flag = 0;
639
2.26k
          xsum_small_add1(&accumulator, value->value.number);
640
2.26k
        }
641
2.27k
      }
642
13
      else
643
13
        mxTypeError("items[%d]: not a number", count);
644
2.51k
      count++;
645
2.51k
    }
646
2.51k
    mxCatch(the) {
647
13
      fxIteratorReturn(the, iterator, 1);
648
13
      fxJump(the);
649
13
    }
650
2.53k
  }
651
728
  if (flag)
652
8
    mxResult->value.number = -0.0;
653
720
  else
654
720
    mxResult->value.number = xsum_small_round(&accumulator);
655
728
  mxResult->kind = XS_NUMBER_KIND;
656
728
}
657
658
void fx_Math_sign(txMachine* the)
659
9.67k
{
660
9.67k
  txNumber arg;
661
9.67k
  mxNanResultIfNoArg;
662
9.21k
  arg = fxToNumber(the, mxArgv(0));
663
9.21k
  mxResult->kind = XS_NUMBER_KIND;
664
9.21k
  if (c_isnan(arg))
665
5.29k
    mxResult->value.number = C_NAN;
666
3.92k
  else if (arg < 0)
667
285
    mxResult->value.number = -1;
668
3.63k
  else if (arg > 0)
669
1.35k
    mxResult->value.number = 1;
670
2.28k
  else
671
2.28k
    mxResult->value.number = arg;
672
9.21k
  fx_Math_toInteger(the);
673
9.21k
}
674
675
void fx_Math_sin(txMachine* the)
676
14.6k
{
677
14.6k
  mxNanResultIfNoArg;
678
13.0k
  fxToNumber(the, mxArgv(0));
679
13.0k
  mxResult->kind = XS_NUMBER_KIND;
680
13.0k
  mxResult->value.number = c_sin(mxArgv(0)->value.number);
681
13.0k
}
682
683
void fx_Math_sinh(txMachine* the)
684
77.8k
{
685
77.8k
  mxNanResultIfNoArg;
686
77.8k
  fxToNumber(the, mxArgv(0));
687
77.8k
  mxResult->kind = XS_NUMBER_KIND;
688
77.8k
  mxResult->value.number = c_sinh(mxArgv(0)->value.number);
689
77.8k
}
690
691
void fx_Math_tan(txMachine* the)
692
9.58k
{
693
9.58k
  mxNanResultIfNoArg;
694
9.42k
  fxToNumber(the, mxArgv(0));
695
9.42k
  mxResult->kind = XS_NUMBER_KIND;
696
9.42k
  mxResult->value.number = c_tan(mxArgv(0)->value.number);
697
9.42k
}
698
699
void fx_Math_tanh(txMachine* the)
700
4.69k
{
701
4.69k
  mxNanResultIfNoArg;
702
4.63k
  fxToNumber(the, mxArgv(0));
703
4.63k
  mxResult->kind = XS_NUMBER_KIND;
704
4.63k
  mxResult->value.number = c_tanh(mxArgv(0)->value.number);
705
4.63k
}
706
707
void fx_Math_trunc(txMachine* the)
708
95
{
709
95
  mxNanResultIfNoArg;
710
92
  fxToNumber(the, mxArgv(0));
711
92
  mxResult->kind = XS_NUMBER_KIND;
712
92
  mxResult->value.number = c_trunc(mxArgv(0)->value.number);
713
92
  fx_Math_toInteger(the);
714
92
}
715
716
void fx_Math_toInteger(txMachine* the)
717
640k
{
718
640k
  txNumber number = mxResult->value.number;
719
640k
  txInteger integer = (txInteger)number;
720
640k
  txNumber check = integer;
721
640k
  if ((number == check) && (number || !c_signbit(number))) {
722
260k
    mxResult->value.integer = integer;
723
260k
    mxResult->kind = XS_INTEGER_KIND;
724
260k
  }
725
640k
}