Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qv4mathobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qv4mathobject_p.h"
5#include "qv4symbol_p.h"
6
7#include <QtCore/qdatetime.h>
8#include <QtCore/qmath.h>
9#include <QtCore/qrandom.h>
10#include <QtCore/private/qnumeric_p.h>
11#include <QtCore/qthreadstorage.h>
12
13#include <cmath>
14
15using namespace QV4;
16
18
20{
21 Object::init();
23 ScopedObject m(scope, this);
24
25 m->defineReadonlyProperty(QStringLiteral("E"), Value::fromDouble(M_E));
26 m->defineReadonlyProperty(QStringLiteral("LN2"), Value::fromDouble(M_LN2));
27 m->defineReadonlyProperty(QStringLiteral("LN10"), Value::fromDouble(M_LN10));
28 m->defineReadonlyProperty(QStringLiteral("LOG2E"), Value::fromDouble(M_LOG2E));
29 m->defineReadonlyProperty(QStringLiteral("LOG10E"), Value::fromDouble(M_LOG10E));
30 m->defineReadonlyProperty(QStringLiteral("PI"), Value::fromDouble(M_PI));
31 m->defineReadonlyProperty(QStringLiteral("SQRT1_2"), Value::fromDouble(M_SQRT1_2));
32 m->defineReadonlyProperty(QStringLiteral("SQRT2"), Value::fromDouble(M_SQRT2));
33
34 m->defineDefaultProperty(QStringLiteral("abs"), QV4::MathObject::method_abs, 1);
35 m->defineDefaultProperty(QStringLiteral("acos"), QV4::MathObject::method_acos, 1);
36 m->defineDefaultProperty(QStringLiteral("acosh"), QV4::MathObject::method_acosh, 1);
37 m->defineDefaultProperty(QStringLiteral("asin"), QV4::MathObject::method_asin, 1);
38 m->defineDefaultProperty(QStringLiteral("asinh"), QV4::MathObject::method_asinh, 1);
39 m->defineDefaultProperty(QStringLiteral("atan"), QV4::MathObject::method_atan, 1);
40 m->defineDefaultProperty(QStringLiteral("atanh"), QV4::MathObject::method_atanh, 1);
41 m->defineDefaultProperty(QStringLiteral("atan2"), QV4::MathObject::method_atan2, 2);
42 m->defineDefaultProperty(QStringLiteral("cbrt"), QV4::MathObject::method_cbrt, 1);
43 m->defineDefaultProperty(QStringLiteral("ceil"), QV4::MathObject::method_ceil, 1);
44 m->defineDefaultProperty(QStringLiteral("clz32"), QV4::MathObject::method_clz32, 1);
45 m->defineDefaultProperty(QStringLiteral("cos"), QV4::MathObject::method_cos, 1);
46 m->defineDefaultProperty(QStringLiteral("cosh"), QV4::MathObject::method_cosh, 1);
47 m->defineDefaultProperty(QStringLiteral("exp"), QV4::MathObject::method_exp, 1);
48 m->defineDefaultProperty(QStringLiteral("expm1"), QV4::MathObject::method_expm1, 1);
49 m->defineDefaultProperty(QStringLiteral("floor"), QV4::MathObject::method_floor, 1);
50 m->defineDefaultProperty(QStringLiteral("fround"), QV4::MathObject::method_fround, 1);
51 m->defineDefaultProperty(QStringLiteral("hypot"), QV4::MathObject::method_hypot, 2);
52 m->defineDefaultProperty(QStringLiteral("imul"), QV4::MathObject::method_imul, 2);
53 m->defineDefaultProperty(QStringLiteral("log"), QV4::MathObject::method_log, 1);
54 m->defineDefaultProperty(QStringLiteral("log10"), QV4::MathObject::method_log10, 1);
55 m->defineDefaultProperty(QStringLiteral("log1p"), QV4::MathObject::method_log1p, 1);
56 m->defineDefaultProperty(QStringLiteral("log2"), QV4::MathObject::method_log2, 1);
57 m->defineDefaultProperty(QStringLiteral("max"), QV4::MathObject::method_max, 2);
58 m->defineDefaultProperty(QStringLiteral("min"), QV4::MathObject::method_min, 2);
59 m->defineDefaultProperty(QStringLiteral("pow"), QV4::MathObject::method_pow, 2);
60 m->defineDefaultProperty(QStringLiteral("random"), QV4::MathObject::method_random, 0);
61 m->defineDefaultProperty(QStringLiteral("round"), QV4::MathObject::method_round, 1);
62 m->defineDefaultProperty(QStringLiteral("sign"), QV4::MathObject::method_sign, 1);
63 m->defineDefaultProperty(QStringLiteral("sin"), QV4::MathObject::method_sin, 1);
64 m->defineDefaultProperty(QStringLiteral("sinh"), QV4::MathObject::method_sinh, 1);
65 m->defineDefaultProperty(QStringLiteral("sqrt"), QV4::MathObject::method_sqrt, 1);
66 m->defineDefaultProperty(QStringLiteral("tan"), QV4::MathObject::method_tan, 1);
67 m->defineDefaultProperty(QStringLiteral("tanh"), QV4::MathObject::method_tanh, 1);
68 m->defineDefaultProperty(QStringLiteral("trunc"), QV4::MathObject::method_trunc, 1);
69
70 ScopedString name(scope, scope.engine->newString(QStringLiteral("Math")));
71 m->defineReadonlyConfigurableProperty(scope.engine->symbol_toStringTag(), name);
72}
73
74static Q_ALWAYS_INLINE double copySign(double x, double y)
75{
76 return ::copysign(x, y);
77}
78
79ReturnedValue MathObject::method_abs(const FunctionObject *, const Value *, const Value *argv, int argc)
80{
81 if (!argc)
83
84 if (argv[0].isInteger()) {
85 int i = argv[0].integerValue();
86 RETURN_RESULT(Encode(i < 0 ? - i : i));
87 }
88
89 double v = argv[0].toNumber();
90 if (v == 0) // 0 | -0
92
93 RETURN_RESULT(Encode(v < 0 ? -v : v));
94}
95
96ReturnedValue MathObject::method_acos(const FunctionObject *, const Value *, const Value *argv, int argc)
97{
98 double v = argc ? argv[0].toNumber() : 2;
99 if (v > 1)
101
102 RETURN_RESULT(Encode(std::acos(v)));
103}
104
105ReturnedValue MathObject::method_acosh(const FunctionObject *, const Value *, const Value *argv, int argc)
106{
107 double v = argc ? argv[0].toNumber() : 2;
108 if (v < 1)
110
111#ifdef Q_CC_MINGW
112 // Mingw has a broken std::acosh(). It returns NaN when passed Infinity.
113 if (std::isinf(v))
115#endif
116 RETURN_RESULT(Encode(std::acosh(v)));
117}
118
119ReturnedValue MathObject::method_asin(const FunctionObject *, const Value *, const Value *argv, int argc)
120{
121 double v = argc ? argv[0].toNumber() : 2;
122 if (v > 1)
124 else
125 RETURN_RESULT(Encode(std::asin(v)));
126}
127
128ReturnedValue MathObject::method_asinh(const FunctionObject *, const Value *, const Value *argv, int argc)
129{
130 double v = argc ? argv[0].toNumber() : 2;
131 if (v == 0.0)
133 RETURN_RESULT(Encode(std::asinh(v)));
134}
135
136ReturnedValue MathObject::method_atan(const FunctionObject *, const Value *, const Value *argv, int argc)
137{
138 double v = argc ? argv[0].toNumber() : qt_qnan();
139 if (v == 0.0)
141 else
142 RETURN_RESULT(Encode(std::atan(v)));
143}
144
145ReturnedValue MathObject::method_atanh(const FunctionObject *, const Value *, const Value *argv, int argc)
146{
147 double v = argc ? argv[0].toNumber() : qt_qnan();
148 if (v == 0.0)
150
151 RETURN_RESULT(Encode(std::atanh(v)));
152}
153
154ReturnedValue MathObject::method_atan2(const FunctionObject *, const Value *, const Value *argv, int argc)
155{
156 double v1 = argc ? argv[0].toNumber() : qt_qnan();
157 double v2 = argc > 1 ? argv[1].toNumber() : qt_qnan();
158
159 if ((v1 < 0) && qt_is_finite(v1) && qt_is_inf(v2) && (copySign(1.0, v2) == 1.0))
160 RETURN_RESULT(Encode(copySign(0, -1.0)));
161
162 if ((v1 == 0.0) && (v2 == 0.0)) {
163 if ((copySign(1.0, v1) == 1.0) && (copySign(1.0, v2) == -1.0)) {
165 } else if ((copySign(1.0, v1) == -1.0) && (copySign(1.0, v2) == -1.0)) {
167 }
168 }
169 RETURN_RESULT(Encode(std::atan2(v1, v2)));
170}
171
172ReturnedValue MathObject::method_cbrt(const FunctionObject *, const Value *, const Value *argv, int argc)
173{
174 double v = argc ? argv[0].toNumber() : qt_qnan();
175 RETURN_RESULT(Encode(std::cbrt(v))); // cube root
176}
177
178ReturnedValue MathObject::method_ceil(const FunctionObject *, const Value *, const Value *argv, int argc)
179{
180 double v = argc ? argv[0].toNumber() : qt_qnan();
181 if (v < 0.0 && v > -1.0)
182 RETURN_RESULT(Encode(copySign(0, -1.0)));
183 else
184 RETURN_RESULT(Encode(std::ceil(v)));
185}
186
187ReturnedValue MathObject::method_clz32(const FunctionObject *, const Value *, const Value *argv, int argc)
188{
189 quint32 v = argc ? argv[0].toUInt32() : 0;
191}
192
193ReturnedValue MathObject::method_cos(const FunctionObject *, const Value *, const Value *argv, int argc)
194{
195 double v = argc ? argv[0].toNumber() : qt_qnan();
196 RETURN_RESULT(Encode(std::cos(v)));
197}
198
199ReturnedValue MathObject::method_cosh(const FunctionObject *, const Value *, const Value *argv, int argc)
200{
201 double v = argc ? argv[0].toNumber() : qt_qnan();
202 RETURN_RESULT(Encode(std::cosh(v)));
203}
204
205ReturnedValue MathObject::method_exp(const FunctionObject *, const Value *, const Value *argv, int argc)
206{
207 double v = argc ? argv[0].toNumber() : qt_qnan();
208 if (qt_is_inf(v)) {
209 if (copySign(1.0, v) == -1.0)
211 else
213 } else {
214 RETURN_RESULT(Encode(std::exp(v)));
215 }
216}
217
218ReturnedValue MathObject::method_expm1(const FunctionObject *, const Value *, const Value *argv, int argc)
219{
220 double v = argc ? argv[0].toNumber() : qt_qnan();
221 if (std::isnan(v) || qIsNull(v)) {
223 } else if (qt_is_inf(v)) {
224 if (copySign(1.0, v) == -1.0)
225 RETURN_RESULT(Encode(-1.0));
226 else
228 } else {
229 RETURN_RESULT(Encode(std::expm1(v)));
230 }
231}
232
233ReturnedValue MathObject::method_floor(const FunctionObject *, const Value *, const Value *argv, int argc)
234{
235 double v = argc ? argv[0].toNumber() : qt_qnan();
236 Value result = Value::fromDouble(std::floor(v));
237 result.isInt32();
239}
240
241ReturnedValue MathObject::method_fround(const FunctionObject *, const Value *, const Value *argv, int argc)
242{
243 double v = argc ? argv[0].toNumber() : qt_qnan();
244 if (std::isnan(v) || qt_is_inf(v) || qIsNull(v))
246 else // convert to 32-bit float using roundTiesToEven, then convert back to 64-bit double
247 RETURN_RESULT(Encode(double(float(v))));
248}
249
250ReturnedValue MathObject::method_hypot(const FunctionObject *, const Value *, const Value *argv, int argc)
251{
252 // ES6 Math.hypot(v1, ..., vn) -> sqrt(sum(vi**2)) but "should take care to
253 // avoid the loss of precision from overflows and underflows" (as std::hypot does).
254 double v = 0;
255 // Spec mandates +0 on no args; and says nothing about what to do if toNumber() signals ...
256 if (argc > 0) {
258 for (int i = 1; i < argc; i++)
259 h = h.add(argv[i].toNumber());
260 v = h.result();
261 }
263}
264
265ReturnedValue MathObject::method_imul(const FunctionObject *, const Value *, const Value *argv, int argc)
266{
267 quint32 a = argc ? argv[0].toUInt32() : 0;
268 quint32 b = argc > 0 ? argv[1].toUInt32() : 0;
269 qint32 product = a * b;
270 RETURN_RESULT(Encode(product));
271}
272
273ReturnedValue MathObject::method_log(const FunctionObject *, const Value *, const Value *argv, int argc)
274{
275 double v = argc ? argv[0].toNumber() : qt_qnan();
276 if (v < 0)
278 else
279 RETURN_RESULT(Encode(std::log(v)));
280}
281
282ReturnedValue MathObject::method_log10(const FunctionObject *, const Value *, const Value *argv, int argc)
283{
284 double v = argc ? argv[0].toNumber() : qt_qnan();
285 if (v < 0)
287 else
288 RETURN_RESULT(Encode(std::log10(v)));
289}
290
291ReturnedValue MathObject::method_log1p(const FunctionObject *, const Value *, const Value *argv, int argc)
292{
293#if !defined(__ANDROID__)
294 using std::log1p;
295#endif
296 double v = argc ? argv[0].toNumber() : qt_qnan();
297 if (v < -1)
299 else
300 RETURN_RESULT(Encode(log1p(v)));
301}
302
303ReturnedValue MathObject::method_log2(const FunctionObject *, const Value *, const Value *argv, int argc)
304{
305 double v = argc ? argv[0].toNumber() : qt_qnan();
306 if (v < 0) {
308 } else {
309 RETURN_RESULT(Encode(std::log2(v)));
310 }
311}
312
313ReturnedValue MathObject::method_max(const FunctionObject *, const Value *, const Value *argv, int argc)
314{
315 double mx = -qt_inf();
316 for (int i = 0, ei = argc; i < ei; ++i) {
317 double x = argv[i].toNumber();
318 if ((x == 0 && mx == x && copySign(1.0, x) == 1.0)
319 || (x > mx) || std::isnan(x)) {
320 mx = x;
321 }
322 }
324}
325
326ReturnedValue MathObject::method_min(const FunctionObject *, const Value *, const Value *argv, int argc)
327{
328 double mx = qt_inf();
329 for (int i = 0, ei = argc; i < ei; ++i) {
330 double x = argv[i].toNumber();
331 if ((x == 0 && mx == x && copySign(1.0, x) == -1.0)
332 || (x < mx) || std::isnan(x)) {
333 mx = x;
334 }
335 }
337}
338
339ReturnedValue MathObject::method_pow(const FunctionObject *, const Value *, const Value *argv, int argc)
340{
341 double x = argc > 0 ? argv[0].toNumber() : qt_qnan();
342 double y = argc > 1 ? argv[1].toNumber() : qt_qnan();
343
345}
346
351
352ReturnedValue MathObject::method_round(const FunctionObject *, const Value *, const Value *argv, int argc)
353{
354 double v = argc ? argv[0].toNumber() : qt_qnan();
355 if (!std::isfinite(v))
357
358 if (v < 0.5 && v >= -0.5)
359 v = std::copysign(0.0, v);
360 else
361 v = std::floor(v + 0.5);
363}
364
365ReturnedValue MathObject::method_sign(const FunctionObject *, const Value *, const Value *argv, int argc)
366{
367 double v = argc ? argv[0].toNumber() : qt_qnan();
368
369 if (std::isnan(v))
371
372 if (qIsNull(v))
374
375 RETURN_RESULT(Encode(std::signbit(v) ? -1 : 1));
376}
377
378ReturnedValue MathObject::method_sin(const FunctionObject *, const Value *, const Value *argv, int argc)
379{
380 double v = argc ? argv[0].toNumber() : qt_qnan();
381 if (v == 0.0)
383 else
384 RETURN_RESULT(Encode(std::sin(v)));
385}
386
387ReturnedValue MathObject::method_sinh(const FunctionObject *, const Value *, const Value *argv, int argc)
388{
389 double v = argc ? argv[0].toNumber() : qt_qnan();
390 if (v == 0.0)
392 else
393 RETURN_RESULT(Encode(std::sinh(v)));
394}
395
396ReturnedValue MathObject::method_sqrt(const FunctionObject *, const Value *, const Value *argv, int argc)
397{
398 double v = argc ? argv[0].toNumber() : qt_qnan();
399 RETURN_RESULT(Encode(std::sqrt(v)));
400}
401
402ReturnedValue MathObject::method_tan(const FunctionObject *, const Value *, const Value *argv, int argc)
403{
404 double v = argc ? argv[0].toNumber() : qt_qnan();
405 if (v == 0.0)
407 else
408 RETURN_RESULT(Encode(std::tan(v)));
409}
410
411ReturnedValue MathObject::method_tanh(const FunctionObject *, const Value *, const Value *argv, int argc)
412{
413 double v = argc ? argv[0].toNumber() : qt_qnan();
414 if (v == 0.0)
416 else
417 RETURN_RESULT(Encode(std::tanh(v)));
418}
419
420ReturnedValue MathObject::method_trunc(const FunctionObject *, const Value *, const Value *argv, int argc)
421{
422 double v = argc ? argv[0].toNumber() : qt_qnan();
423 RETURN_RESULT(Encode(std::trunc(v)));
424}
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
Definition qrandom.h:275
double jsExponentiate(double base, double exponent)
quint64 ReturnedValue
Scoped< String > ScopedString
QT_POPCOUNT_RELAXED_CONSTEXPR uint qCountLeadingZeroBits(quint32 v) noexcept
#define Q_ALWAYS_INLINE
bool qIsNull(qfloat16 f) noexcept
Definition qfloat16.h:354
#define M_LN10
Definition qmath.h:205
#define M_SQRT2
Definition qmath.h:233
#define M_LN2
Definition qmath.h:201
#define M_E
Definition qmath.h:189
#define M_LOG10E
Definition qmath.h:197
#define M_LOG2E
Definition qmath.h:193
#define M_SQRT1_2
Definition qmath.h:237
#define M_PI
Definition qmath.h:209
constexpr static Q_DECL_CONST_FUNCTION double qt_qnan() noexcept
Definition qnumeric_p.h:100
constexpr static Q_DECL_CONST_FUNCTION double qt_inf() noexcept
Definition qnumeric_p.h:83
static Q_DECL_CONST_FUNCTION bool qt_is_inf(double d)
Definition qnumeric_p.h:107
static Q_DECL_CONST_FUNCTION bool qt_is_finite(double d)
Definition qnumeric_p.h:117
GLint GLfloat GLfloat GLfloat v2
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
const GLfloat * m
GLboolean GLboolean GLboolean GLboolean a
[7]
GLint GLfloat GLfloat v1
GLuint name
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLuint64EXT * result
[6]
#define QStringLiteral(str)
unsigned int quint32
Definition qtypes.h:50
int qint32
Definition qtypes.h:49
static Q_ALWAYS_INLINE double copySign(double x, double y)
#define RETURN_RESULT(r)
#define DEFINE_OBJECT_VTABLE(classname)
return generateDouble() *highest
[11]
static ReturnedValue smallestNumber(double d)
Symbol * symbol_toStringTag() const
Heap::String * newString(char16_t c)
Heap::InternalClass * internalClass() const
static ReturnedValue method_sin(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_max(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_asinh(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_tanh(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_tan(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_abs(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_floor(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_asin(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_cos(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_sqrt(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_log2(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_atanh(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_imul(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_hypot(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_expm1(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_atan2(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_atan(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_random(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_log1p(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_sinh(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_log10(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_sign(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_ceil(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_min(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_acosh(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_cosh(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_fround(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_acos(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_pow(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_log(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_exp(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_cbrt(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_clz32(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_round(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_trunc(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
ExecutionEngine * engine
bool isInteger() const
int integerValue() const
unsigned int toUInt32() const
Definition qv4value_p.h:364
static Value fromDouble(double d)
Definition qv4value_p.h:199
double toNumber() const
Definition qv4value_p.h:323