1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qv4mathobject_p.h"
41 #include "qv4objectproto_p.h"
42 #include "qv4symbol_p.h"
43
44 #include <QtCore/qdatetime.h>
45 #include <QtCore/qmath.h>
46 #include <QtCore/qrandom.h>
47 #include <QtCore/private/qnumeric_p.h>
48 #include <QtCore/qthreadstorage.h>
49
50 #include <math.h>
51 #include <cmath>
52
53 using namespace QV4;
54
55 DEFINE_OBJECT_VTABLE(MathObject);
56
init()57 void Heap::MathObject::init()
58 {
59 Object::init();
60 Scope scope(internalClass->engine);
61 ScopedObject m(scope, this);
62
63 m->defineReadonlyProperty(QStringLiteral("E"), Value::fromDouble(M_E));
64 m->defineReadonlyProperty(QStringLiteral("LN2"), Value::fromDouble(M_LN2));
65 m->defineReadonlyProperty(QStringLiteral("LN10"), Value::fromDouble(M_LN10));
66 m->defineReadonlyProperty(QStringLiteral("LOG2E"), Value::fromDouble(M_LOG2E));
67 m->defineReadonlyProperty(QStringLiteral("LOG10E"), Value::fromDouble(M_LOG10E));
68 m->defineReadonlyProperty(QStringLiteral("PI"), Value::fromDouble(M_PI));
69 m->defineReadonlyProperty(QStringLiteral("SQRT1_2"), Value::fromDouble(M_SQRT1_2));
70 m->defineReadonlyProperty(QStringLiteral("SQRT2"), Value::fromDouble(M_SQRT2));
71
72 m->defineDefaultProperty(QStringLiteral("abs"), QV4::MathObject::method_abs, 1);
73 m->defineDefaultProperty(QStringLiteral("acos"), QV4::MathObject::method_acos, 1);
74 m->defineDefaultProperty(QStringLiteral("acosh"), QV4::MathObject::method_acosh, 1);
75 m->defineDefaultProperty(QStringLiteral("asin"), QV4::MathObject::method_asin, 1);
76 m->defineDefaultProperty(QStringLiteral("asinh"), QV4::MathObject::method_asinh, 1);
77 m->defineDefaultProperty(QStringLiteral("atan"), QV4::MathObject::method_atan, 1);
78 m->defineDefaultProperty(QStringLiteral("atanh"), QV4::MathObject::method_atanh, 1);
79 m->defineDefaultProperty(QStringLiteral("atan2"), QV4::MathObject::method_atan2, 2);
80 m->defineDefaultProperty(QStringLiteral("cbrt"), QV4::MathObject::method_cbrt, 1);
81 m->defineDefaultProperty(QStringLiteral("ceil"), QV4::MathObject::method_ceil, 1);
82 m->defineDefaultProperty(QStringLiteral("clz32"), QV4::MathObject::method_clz32, 1);
83 m->defineDefaultProperty(QStringLiteral("cos"), QV4::MathObject::method_cos, 1);
84 m->defineDefaultProperty(QStringLiteral("cosh"), QV4::MathObject::method_cosh, 1);
85 m->defineDefaultProperty(QStringLiteral("exp"), QV4::MathObject::method_exp, 1);
86 m->defineDefaultProperty(QStringLiteral("expm1"), QV4::MathObject::method_expm1, 1);
87 m->defineDefaultProperty(QStringLiteral("floor"), QV4::MathObject::method_floor, 1);
88 m->defineDefaultProperty(QStringLiteral("fround"), QV4::MathObject::method_fround, 1);
89 m->defineDefaultProperty(QStringLiteral("hypot"), QV4::MathObject::method_hypot, 2);
90 m->defineDefaultProperty(QStringLiteral("imul"), QV4::MathObject::method_imul, 2);
91 m->defineDefaultProperty(QStringLiteral("log"), QV4::MathObject::method_log, 1);
92 m->defineDefaultProperty(QStringLiteral("log10"), QV4::MathObject::method_log10, 1);
93 m->defineDefaultProperty(QStringLiteral("log1p"), QV4::MathObject::method_log1p, 1);
94 m->defineDefaultProperty(QStringLiteral("log2"), QV4::MathObject::method_log2, 1);
95 m->defineDefaultProperty(QStringLiteral("max"), QV4::MathObject::method_max, 2);
96 m->defineDefaultProperty(QStringLiteral("min"), QV4::MathObject::method_min, 2);
97 m->defineDefaultProperty(QStringLiteral("pow"), QV4::MathObject::method_pow, 2);
98 m->defineDefaultProperty(QStringLiteral("random"), QV4::MathObject::method_random, 0);
99 m->defineDefaultProperty(QStringLiteral("round"), QV4::MathObject::method_round, 1);
100 m->defineDefaultProperty(QStringLiteral("sign"), QV4::MathObject::method_sign, 1);
101 m->defineDefaultProperty(QStringLiteral("sin"), QV4::MathObject::method_sin, 1);
102 m->defineDefaultProperty(QStringLiteral("sinh"), QV4::MathObject::method_sinh, 1);
103 m->defineDefaultProperty(QStringLiteral("sqrt"), QV4::MathObject::method_sqrt, 1);
104 m->defineDefaultProperty(QStringLiteral("tan"), QV4::MathObject::method_tan, 1);
105 m->defineDefaultProperty(QStringLiteral("tanh"), QV4::MathObject::method_tanh, 1);
106 m->defineDefaultProperty(QStringLiteral("trunc"), QV4::MathObject::method_trunc, 1);
107
108 ScopedString name(scope, scope.engine->newString(QStringLiteral("Math")));
109 m->defineReadonlyConfigurableProperty(scope.engine->symbol_toStringTag(), name);
110 }
111
copySign(double x,double y)112 static Q_ALWAYS_INLINE double copySign(double x, double y)
113 {
114 return ::copysign(x, y);
115 }
116
method_abs(const FunctionObject *,const Value *,const Value * argv,int argc)117 ReturnedValue MathObject::method_abs(const FunctionObject *, const Value *, const Value *argv, int argc)
118 {
119 if (!argc)
120 RETURN_RESULT(Encode(qt_qnan()));
121
122 if (argv[0].isInteger()) {
123 int i = argv[0].integerValue();
124 RETURN_RESULT(Encode(i < 0 ? - i : i));
125 }
126
127 double v = argv[0].toNumber();
128 if (v == 0) // 0 | -0
129 RETURN_RESULT(Encode(0));
130
131 RETURN_RESULT(Encode(v < 0 ? -v : v));
132 }
133
method_acos(const FunctionObject *,const Value *,const Value * argv,int argc)134 ReturnedValue MathObject::method_acos(const FunctionObject *, const Value *, const Value *argv, int argc)
135 {
136 double v = argc ? argv[0].toNumber() : 2;
137 if (v > 1)
138 RETURN_RESULT(Encode(qt_qnan()));
139
140 RETURN_RESULT(Encode(std::acos(v)));
141 }
142
method_acosh(const FunctionObject *,const Value *,const Value * argv,int argc)143 ReturnedValue MathObject::method_acosh(const FunctionObject *, const Value *, const Value *argv, int argc)
144 {
145 double v = argc ? argv[0].toNumber() : 2;
146 if (v < 1)
147 RETURN_RESULT(Encode(qt_qnan()));
148
149 #ifdef Q_OS_ANDROID // incomplete std :-(
150 RETURN_RESULT(Encode(std::log(v +std::sqrt(v + 1) * std::sqrt(v - 1))));
151 #else
152 RETURN_RESULT(Encode(std::acosh(v)));
153 #endif
154 }
155
method_asin(const FunctionObject *,const Value *,const Value * argv,int argc)156 ReturnedValue MathObject::method_asin(const FunctionObject *, const Value *, const Value *argv, int argc)
157 {
158 double v = argc ? argv[0].toNumber() : 2;
159 if (v > 1)
160 RETURN_RESULT(Encode(qt_qnan()));
161 else
162 RETURN_RESULT(Encode(std::asin(v)));
163 }
164
method_asinh(const FunctionObject *,const Value *,const Value * argv,int argc)165 ReturnedValue MathObject::method_asinh(const FunctionObject *, const Value *, const Value *argv, int argc)
166 {
167 double v = argc ? argv[0].toNumber() : 2;
168 if (v == 0.0)
169 RETURN_RESULT(Encode(v));
170
171 #ifdef Q_OS_ANDROID // incomplete std :-(
172 RETURN_RESULT(Encode(std::log(v +std::sqrt(1 + v * v))));
173 #else
174 RETURN_RESULT(Encode(std::asinh(v)));
175 #endif
176 }
177
method_atan(const FunctionObject *,const Value *,const Value * argv,int argc)178 ReturnedValue MathObject::method_atan(const FunctionObject *, const Value *, const Value *argv, int argc)
179 {
180 double v = argc ? argv[0].toNumber() : qt_qnan();
181 if (v == 0.0)
182 RETURN_RESULT(Encode(v));
183 else
184 RETURN_RESULT(Encode(std::atan(v)));
185 }
186
method_atanh(const FunctionObject *,const Value *,const Value * argv,int argc)187 ReturnedValue MathObject::method_atanh(const FunctionObject *, const Value *, const Value *argv, int argc)
188 {
189 double v = argc ? argv[0].toNumber() : qt_qnan();
190 if (v == 0.0)
191 RETURN_RESULT(Encode(v));
192
193 #ifdef Q_OS_ANDROID // incomplete std :-(
194 if (-1 < v && v < 1)
195 RETURN_RESULT(Encode(0.5 * (std::log(v + 1) - std::log(v - 1))));
196
197 if (v > 1 || v < -1)
198 RETURN_RESULT(Encode(qt_qnan()));
199
200 RETURN_RESULT(Encode(copySign(qt_inf(), v)));
201 #else
202 RETURN_RESULT(Encode(std::atanh(v)));
203 #endif
204 }
205
method_atan2(const FunctionObject *,const Value *,const Value * argv,int argc)206 ReturnedValue MathObject::method_atan2(const FunctionObject *, const Value *, const Value *argv, int argc)
207 {
208 double v1 = argc ? argv[0].toNumber() : qt_qnan();
209 double v2 = argc > 1 ? argv[1].toNumber() : qt_qnan();
210
211 if ((v1 < 0) && qt_is_finite(v1) && qt_is_inf(v2) && (copySign(1.0, v2) == 1.0))
212 RETURN_RESULT(Encode(copySign(0, -1.0)));
213
214 if ((v1 == 0.0) && (v2 == 0.0)) {
215 if ((copySign(1.0, v1) == 1.0) && (copySign(1.0, v2) == -1.0)) {
216 RETURN_RESULT(Encode(M_PI));
217 } else if ((copySign(1.0, v1) == -1.0) && (copySign(1.0, v2) == -1.0)) {
218 RETURN_RESULT(Encode(-M_PI));
219 }
220 }
221 RETURN_RESULT(Encode(std::atan2(v1, v2)));
222 }
223
method_cbrt(const FunctionObject *,const Value *,const Value * argv,int argc)224 ReturnedValue MathObject::method_cbrt(const FunctionObject *, const Value *, const Value *argv, int argc)
225 {
226 double v = argc ? argv[0].toNumber() : qt_qnan();
227 #ifdef Q_OS_ANDROID // incomplete std :-(
228 RETURN_RESULT(Encode(copySign(std::exp(std::log(std::abs(v)) / 3), v)));
229 #else
230 RETURN_RESULT(Encode(std::cbrt(v))); // cube root
231 #endif
232 }
233
method_ceil(const FunctionObject *,const Value *,const Value * argv,int argc)234 ReturnedValue MathObject::method_ceil(const FunctionObject *, const Value *, const Value *argv, int argc)
235 {
236 double v = argc ? argv[0].toNumber() : qt_qnan();
237 if (v < 0.0 && v > -1.0)
238 RETURN_RESULT(Encode(copySign(0, -1.0)));
239 else
240 RETURN_RESULT(Encode(std::ceil(v)));
241 }
242
method_clz32(const FunctionObject *,const Value *,const Value * argv,int argc)243 ReturnedValue MathObject::method_clz32(const FunctionObject *, const Value *, const Value *argv, int argc)
244 {
245 quint32 v = argc ? argv[0].toUInt32() : 0;
246 RETURN_RESULT(Encode(qint32(qCountLeadingZeroBits(v))));
247 }
248
method_cos(const FunctionObject *,const Value *,const Value * argv,int argc)249 ReturnedValue MathObject::method_cos(const FunctionObject *, const Value *, const Value *argv, int argc)
250 {
251 double v = argc ? argv[0].toNumber() : qt_qnan();
252 RETURN_RESULT(Encode(std::cos(v)));
253 }
254
method_cosh(const FunctionObject *,const Value *,const Value * argv,int argc)255 ReturnedValue MathObject::method_cosh(const FunctionObject *, const Value *, const Value *argv, int argc)
256 {
257 double v = argc ? argv[0].toNumber() : qt_qnan();
258 RETURN_RESULT(Encode(std::cosh(v)));
259 }
260
method_exp(const FunctionObject *,const Value *,const Value * argv,int argc)261 ReturnedValue MathObject::method_exp(const FunctionObject *, const Value *, const Value *argv, int argc)
262 {
263 double v = argc ? argv[0].toNumber() : qt_qnan();
264 if (qt_is_inf(v)) {
265 if (copySign(1.0, v) == -1.0)
266 RETURN_RESULT(Encode(0));
267 else
268 RETURN_RESULT(Encode(qt_inf()));
269 } else {
270 RETURN_RESULT(Encode(std::exp(v)));
271 }
272 }
273
method_expm1(const FunctionObject *,const Value *,const Value * argv,int argc)274 ReturnedValue MathObject::method_expm1(const FunctionObject *, const Value *, const Value *argv, int argc)
275 {
276 double v = argc ? argv[0].toNumber() : qt_qnan();
277 if (std::isnan(v) || qIsNull(v)) {
278 RETURN_RESULT(Encode(v));
279 } else if (qt_is_inf(v)) {
280 if (copySign(1.0, v) == -1.0)
281 RETURN_RESULT(Encode(-1.0));
282 else
283 RETURN_RESULT(Encode(qt_inf()));
284 } else {
285 #ifdef Q_OS_ANDROID // incomplete std :-(
286 RETURN_RESULT(Encode(std::exp(v) - 1));
287 #else
288 RETURN_RESULT(Encode(std::expm1(v)));
289 #endif
290 }
291 }
292
method_floor(const FunctionObject *,const Value *,const Value * argv,int argc)293 ReturnedValue MathObject::method_floor(const FunctionObject *, const Value *, const Value *argv, int argc)
294 {
295 double v = argc ? argv[0].toNumber() : qt_qnan();
296 Value result = Value::fromDouble(std::floor(v));
297 result.isInt32();
298 RETURN_RESULT(result);
299 }
300
method_fround(const FunctionObject *,const Value *,const Value * argv,int argc)301 ReturnedValue MathObject::method_fround(const FunctionObject *, const Value *, const Value *argv, int argc)
302 {
303 double v = argc ? argv[0].toNumber() : qt_qnan();
304 if (std::isnan(v) || qt_is_inf(v) || qIsNull(v))
305 RETURN_RESULT(Encode(v));
306 else // convert to 32-bit float using roundTiesToEven, then convert back to 64-bit double
307 RETURN_RESULT(Encode(double(float(v))));
308 }
309
method_hypot(const FunctionObject *,const Value *,const Value * argv,int argc)310 ReturnedValue MathObject::method_hypot(const FunctionObject *, const Value *, const Value *argv, int argc)
311 {
312 // ES6 Math.hypot(v1, ..., vn) -> sqrt(sum(vi**2)) but "should take care to
313 // avoid the loss of precision from overflows and underflows" (as std::hypot does).
314 double v = argc ? argv[0].toNumber() : 0;
315 // Spec mandates +0 on no args; and says nothing about what to do if toNumber() signals ...
316 #ifdef Q_OS_ANDROID // incomplete std :-(
317 bool big = qt_is_inf(v), bad = std::isnan(v);
318 v *= v;
319 for (int i = 1; !big && i < argc; i++) {
320 double u = argv[i].toNumber();
321 if (qt_is_inf(u))
322 big = true;
323 if (std::isnan(u))
324 bad = true;
325 v += u * u;
326 }
327 if (big)
328 RETURN_RESULT(Encode(qt_inf()));
329 if (bad)
330 RETURN_RESULT(Encode(qt_qnan()));
331 // Should actually check for {und,ov}erflow, but too fiddly !
332 RETURN_RESULT(Value::fromDouble(sqrt(v)));
333 #else
334 for (int i = 1; i < argc; i++)
335 v = std::hypot(v, argv[i].toNumber());
336 #endif
337 RETURN_RESULT(Value::fromDouble(v));
338 }
339
method_imul(const FunctionObject *,const Value *,const Value * argv,int argc)340 ReturnedValue MathObject::method_imul(const FunctionObject *, const Value *, const Value *argv, int argc)
341 {
342 quint32 a = argc ? argv[0].toUInt32() : 0;
343 quint32 b = argc > 0 ? argv[1].toUInt32() : 0;
344 qint32 product = a * b;
345 RETURN_RESULT(Encode(product));
346 }
347
method_log(const FunctionObject *,const Value *,const Value * argv,int argc)348 ReturnedValue MathObject::method_log(const FunctionObject *, const Value *, const Value *argv, int argc)
349 {
350 double v = argc ? argv[0].toNumber() : qt_qnan();
351 if (v < 0)
352 RETURN_RESULT(Encode(qt_qnan()));
353 else
354 RETURN_RESULT(Encode(std::log(v)));
355 }
356
method_log10(const FunctionObject *,const Value *,const Value * argv,int argc)357 ReturnedValue MathObject::method_log10(const FunctionObject *, const Value *, const Value *argv, int argc)
358 {
359 double v = argc ? argv[0].toNumber() : qt_qnan();
360 if (v < 0)
361 RETURN_RESULT(Encode(qt_qnan()));
362 else
363 RETURN_RESULT(Encode(std::log10(v)));
364 }
365
method_log1p(const FunctionObject *,const Value *,const Value * argv,int argc)366 ReturnedValue MathObject::method_log1p(const FunctionObject *, const Value *, const Value *argv, int argc)
367 {
368 #if !defined(__ANDROID__)
369 using std::log1p;
370 #endif
371 double v = argc ? argv[0].toNumber() : qt_qnan();
372 if (v < -1)
373 RETURN_RESULT(Encode(qt_qnan()));
374 else
375 RETURN_RESULT(Encode(log1p(v)));
376 }
377
method_log2(const FunctionObject *,const Value *,const Value * argv,int argc)378 ReturnedValue MathObject::method_log2(const FunctionObject *, const Value *, const Value *argv, int argc)
379 {
380 double v = argc ? argv[0].toNumber() : qt_qnan();
381 if (v < 0) {
382 RETURN_RESULT(Encode(qt_qnan()));
383 } else {
384 #ifdef Q_OS_ANDROID // incomplete std :-(
385 // Android ndk r10e doesn't have std::log2, so fall back.
386 const double ln2 = std::log(2.0);
387 RETURN_RESULT(Encode(std::log(v) / ln2));
388 #else
389 RETURN_RESULT(Encode(std::log2(v)));
390 #endif
391 }
392 }
393
method_max(const FunctionObject *,const Value *,const Value * argv,int argc)394 ReturnedValue MathObject::method_max(const FunctionObject *, const Value *, const Value *argv, int argc)
395 {
396 double mx = -qt_inf();
397 for (int i = 0, ei = argc; i < ei; ++i) {
398 double x = argv[i].toNumber();
399 if ((x == 0 && mx == x && copySign(1.0, x) == 1.0)
400 || (x > mx) || std::isnan(x)) {
401 mx = x;
402 }
403 }
404 RETURN_RESULT(Encode::smallestNumber(mx));
405 }
406
method_min(const FunctionObject *,const Value *,const Value * argv,int argc)407 ReturnedValue MathObject::method_min(const FunctionObject *, const Value *, const Value *argv, int argc)
408 {
409 double mx = qt_inf();
410 for (int i = 0, ei = argc; i < ei; ++i) {
411 double x = argv[i].toNumber();
412 if ((x == 0 && mx == x && copySign(1.0, x) == -1.0)
413 || (x < mx) || std::isnan(x)) {
414 mx = x;
415 }
416 }
417 RETURN_RESULT(Encode::smallestNumber(mx));
418 }
419
method_pow(const FunctionObject *,const Value *,const Value * argv,int argc)420 ReturnedValue MathObject::method_pow(const FunctionObject *, const Value *, const Value *argv, int argc)
421 {
422 double x = argc > 0 ? argv[0].toNumber() : qt_qnan();
423 double y = argc > 1 ? argv[1].toNumber() : qt_qnan();
424
425 if (std::isnan(y))
426 RETURN_RESULT(Encode(qt_qnan()));
427
428 if (y == 0) {
429 RETURN_RESULT(Encode(1));
430 } else if (((x == 1) || (x == -1)) && std::isinf(y)) {
431 RETURN_RESULT(Encode(qt_qnan()));
432 } else if (((x == 0) && copySign(1.0, x) == 1.0) && (y < 0)) {
433 RETURN_RESULT(Encode(qInf()));
434 } else if ((x == 0) && copySign(1.0, x) == -1.0) {
435 if (y < 0) {
436 if (std::fmod(-y, 2.0) == 1.0)
437 RETURN_RESULT(Encode(-qt_inf()));
438 else
439 RETURN_RESULT(Encode(qt_inf()));
440 } else if (y > 0) {
441 if (std::fmod(y, 2.0) == 1.0)
442 RETURN_RESULT(Encode(copySign(0, -1.0)));
443 else
444 RETURN_RESULT(Encode(0));
445 }
446 }
447
448 #ifdef Q_OS_AIX
449 else if (qt_is_inf(x) && copySign(1.0, x) == -1.0) {
450 if (y > 0) {
451 if (std::fmod(y, 2.0) == 1.0)
452 RETURN_RESULT(Encode(-qt_inf()));
453 else
454 RETURN_RESULT(Encode(qt_inf()));
455 } else if (y < 0) {
456 if (std::fmod(-y, 2.0) == 1.0)
457 RETURN_RESULT(Encode(copySign(0, -1.0)));
458 else
459 RETURN_RESULT(Encode(0));
460 }
461 }
462 #endif
463 else {
464 RETURN_RESULT(Encode(std::pow(x, y)));
465 }
466 // ###
467 RETURN_RESULT(Encode(qt_qnan()));
468 }
469
method_random(const FunctionObject *,const Value *,const Value *,int)470 ReturnedValue MathObject::method_random(const FunctionObject *, const Value *, const Value *, int)
471 {
472 RETURN_RESULT(Encode(QRandomGenerator::global()->generateDouble()));
473 }
474
method_round(const FunctionObject *,const Value *,const Value * argv,int argc)475 ReturnedValue MathObject::method_round(const FunctionObject *, const Value *, const Value *argv, int argc)
476 {
477 double v = argc ? argv[0].toNumber() : qt_qnan();
478 if (std::isnan(v) || qt_is_inf(v) || qIsNull(v))
479 RETURN_RESULT(Encode(v));
480
481 v = copySign(std::floor(v + 0.5), v);
482 RETURN_RESULT(Encode(v));
483 }
484
method_sign(const FunctionObject *,const Value *,const Value * argv,int argc)485 ReturnedValue MathObject::method_sign(const FunctionObject *, const Value *, const Value *argv, int argc)
486 {
487 double v = argc ? argv[0].toNumber() : qt_qnan();
488
489 if (std::isnan(v))
490 RETURN_RESULT(Encode(qt_qnan()));
491
492 if (qIsNull(v))
493 RETURN_RESULT(Encode(v));
494
495 RETURN_RESULT(Encode(std::signbit(v) ? -1 : 1));
496 }
497
method_sin(const FunctionObject *,const Value *,const Value * argv,int argc)498 ReturnedValue MathObject::method_sin(const FunctionObject *, const Value *, const Value *argv, int argc)
499 {
500 double v = argc ? argv[0].toNumber() : qt_qnan();
501 if (v == 0.0)
502 RETURN_RESULT(Encode(v));
503 else
504 RETURN_RESULT(Encode(std::sin(v)));
505 }
506
method_sinh(const FunctionObject *,const Value *,const Value * argv,int argc)507 ReturnedValue MathObject::method_sinh(const FunctionObject *, const Value *, const Value *argv, int argc)
508 {
509 double v = argc ? argv[0].toNumber() : qt_qnan();
510 if (v == 0.0)
511 RETURN_RESULT(Encode(v));
512 else
513 RETURN_RESULT(Encode(std::sinh(v)));
514 }
515
method_sqrt(const FunctionObject *,const Value *,const Value * argv,int argc)516 ReturnedValue MathObject::method_sqrt(const FunctionObject *, const Value *, const Value *argv, int argc)
517 {
518 double v = argc ? argv[0].toNumber() : qt_qnan();
519 RETURN_RESULT(Encode(std::sqrt(v)));
520 }
521
method_tan(const FunctionObject *,const Value *,const Value * argv,int argc)522 ReturnedValue MathObject::method_tan(const FunctionObject *, const Value *, const Value *argv, int argc)
523 {
524 double v = argc ? argv[0].toNumber() : qt_qnan();
525 if (v == 0.0)
526 RETURN_RESULT(Encode(v));
527 else
528 RETURN_RESULT(Encode(std::tan(v)));
529 }
530
method_tanh(const FunctionObject *,const Value *,const Value * argv,int argc)531 ReturnedValue MathObject::method_tanh(const FunctionObject *, const Value *, const Value *argv, int argc)
532 {
533 double v = argc ? argv[0].toNumber() : qt_qnan();
534 if (v == 0.0)
535 RETURN_RESULT(Encode(v));
536 else
537 RETURN_RESULT(Encode(std::tanh(v)));
538 }
539
method_trunc(const FunctionObject *,const Value *,const Value * argv,int argc)540 ReturnedValue MathObject::method_trunc(const FunctionObject *, const Value *, const Value *argv, int argc)
541 {
542 double v = argc ? argv[0].toNumber() : qt_qnan();
543 #ifdef Q_OS_ANDROID // incomplete std :-(
544 if (std::isnan(v) || qt_is_inf(v) || qIsNull(v))
545 RETURN_RESULT(Encode(v));
546 // Nearest integer not greater in magnitude:
547 quint64 whole = std::abs(v);
548 RETURN_RESULT(Encode(copySign(whole, v)));
549 #else
550 RETURN_RESULT(Encode(std::trunc(v)));
551 #endif
552 }
553