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

source code of qtdeclarative/src/qml/jsruntime/qv4mathobject.cpp