ReactOS 0.4.15-dev-8058-ga7cbb60
math.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20#ifdef __REACTOS__
21#include <wine/config.h>
22#include <wine/port.h>
23#endif
24
25#include <math.h>
26#include <limits.h>
27
28#include "jscript.h"
29#include "ntsecapi.h"
30
31#include "wine/debug.h"
32
34
35static const WCHAR EW[] = {'E',0};
36static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
37static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
38static const WCHAR LN2W[] = {'L','N','2',0};
39static const WCHAR LN10W[] = {'L','N','1','0',0};
40static const WCHAR PIW[] = {'P','I',0};
41static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
42static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
43static const WCHAR absW[] = {'a','b','s',0};
44static const WCHAR acosW[] = {'a','c','o','s',0};
45static const WCHAR asinW[] = {'a','s','i','n',0};
46static const WCHAR atanW[] = {'a','t','a','n',0};
47static const WCHAR atan2W[] = {'a','t','a','n','2',0};
48static const WCHAR ceilW[] = {'c','e','i','l',0};
49static const WCHAR cosW[] = {'c','o','s',0};
50static const WCHAR expW[] = {'e','x','p',0};
51static const WCHAR floorW[] = {'f','l','o','o','r',0};
52static const WCHAR logW[] = {'l','o','g',0};
53static const WCHAR maxW[] = {'m','a','x',0};
54static const WCHAR minW[] = {'m','i','n',0};
55static const WCHAR powW[] = {'p','o','w',0};
56static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
57static const WCHAR roundW[] = {'r','o','u','n','d',0};
58static const WCHAR sinW[] = {'s','i','n',0};
59static const WCHAR sqrtW[] = {'s','q','r','t',0};
60static const WCHAR tanW[] = {'t','a','n',0};
61
62/* ECMA-262 3rd Edition 15.8.2.12 */
64 jsval_t *r)
65{
66 double d;
68
69 TRACE("\n");
70
71 if(!argc) {
72 if(r)
73 *r = jsval_number(NAN);
74 return S_OK;
75 }
76
77 hres = to_number(ctx, argv[0], &d);
78 if(FAILED(hres))
79 return hres;
80
81 if(r)
82 *r = jsval_number(d < 0.0 ? -d : d);
83 return S_OK;
84}
85
87 jsval_t *r)
88{
89 double x;
91
92 TRACE("\n");
93
94 if(!argc) {
95 if(r)
96 *r = jsval_number(NAN);
97 return S_OK;
98 }
99
100 hres = to_number(ctx, argv[0], &x);
101 if(FAILED(hres))
102 return hres;
103
104 if(r)
105 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
106 return S_OK;
107}
108
110 jsval_t *r)
111{
112 double x;
114
115 TRACE("\n");
116
117 if(!argc) {
118 if(r)
119 *r = jsval_number(NAN);
120 return S_OK;
121 }
122
123 hres = to_number(ctx, argv[0], &x);
124 if(FAILED(hres))
125 return hres;
126
127 if(r)
128 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
129 return S_OK;
130}
131
133 jsval_t *r)
134{
135 double x;
137
138 TRACE("\n");
139
140 if(!argc) {
141 if(r)
142 *r = jsval_number(NAN);
143 return S_OK;
144 }
145
146 hres = to_number(ctx, argv[0], &x);
147 if(FAILED(hres))
148 return hres;
149
150 if(r)
151 *r = jsval_number(atan(x));
152 return S_OK;
153}
154
156 jsval_t *r)
157{
158 double x, y;
160
161 TRACE("\n");
162
163 if(argc<2) {
164 if(r)
165 *r = jsval_number(NAN);
166 return S_OK;
167 }
168
169 hres = to_number(ctx, argv[0], &y);
170 if(FAILED(hres))
171 return hres;
172
173 hres = to_number(ctx, argv[1], &x);
174 if(FAILED(hres))
175 return hres;
176
177 if(r)
178 *r = jsval_number(atan2(y, x));
179 return S_OK;
180}
181
182/* ECMA-262 3rd Edition 15.8.2.6 */
184 jsval_t *r)
185{
186 double x;
188
189 TRACE("\n");
190
191 if(!argc) {
192 if(r)
193 *r = jsval_number(NAN);
194 return S_OK;
195 }
196
197 hres = to_number(ctx, argv[0], &x);
198 if(FAILED(hres))
199 return hres;
200
201 if(r)
202 *r = jsval_number(ceil(x));
203 return S_OK;
204}
205
207 jsval_t *r)
208{
209 double x;
211
212 TRACE("\n");
213
214 if(!argc) {
215 if(r)
216 *r = jsval_number(NAN);
217 return S_OK;
218 }
219
220 hres = to_number(ctx, argv[0], &x);
221 if(FAILED(hres))
222 return hres;
223
224 if(r)
225 *r = jsval_number(cos(x));
226 return S_OK;
227}
228
230 jsval_t *r)
231{
232 double x;
234
235 TRACE("\n");
236
237 if(!argc) {
238 if(r)
239 *r = jsval_number(NAN);
240 return S_OK;
241 }
242
243 hres = to_number(ctx, argv[0], &x);
244 if(FAILED(hres))
245 return hres;
246
247 if(r)
248 *r = jsval_number(exp(x));
249 return S_OK;
250}
251
253 jsval_t *r)
254{
255 double x;
257
258 TRACE("\n");
259
260 if(!argc) {
261 if(r)
262 *r = jsval_number(NAN);
263 return S_OK;
264 }
265
266 hres = to_number(ctx, argv[0], &x);
267 if(FAILED(hres))
268 return hres;
269
270 if(r)
271 *r = jsval_number(floor(x));
272 return S_OK;
273}
274
276 jsval_t *r)
277{
278 double x;
280
281 TRACE("\n");
282
283 if(!argc) {
284 if(r)
285 *r = jsval_number(NAN);
286 return S_OK;
287 }
288
289 hres = to_number(ctx, argv[0], &x);
290 if(FAILED(hres))
291 return hres;
292
293 if(r)
294 *r = jsval_number(x < -0.0 ? NAN : log(x));
295 return S_OK;
296}
297
298/* ECMA-262 3rd Edition 15.8.2.11 */
300 jsval_t *r)
301{
302 DOUBLE max, d;
303 DWORD i;
305
306 TRACE("\n");
307
308 if(!argc) {
309 if(r)
311 return S_OK;
312 }
313
314 hres = to_number(ctx, argv[0], &max);
315 if(FAILED(hres))
316 return hres;
317
318 for(i=1; i < argc; i++) {
319 hres = to_number(ctx, argv[i], &d);
320 if(FAILED(hres))
321 return hres;
322
323 if(d > max || isnan(d))
324 max = d;
325 }
326
327 if(r)
328 *r = jsval_number(max);
329 return S_OK;
330}
331
332/* ECMA-262 3rd Edition 15.8.2.12 */
334 jsval_t *r)
335{
336 DOUBLE min, d;
337 DWORD i;
339
340 TRACE("\n");
341
342 if(!argc) {
343 if(r)
345 return S_OK;
346 }
347
348 hres = to_number(ctx, argv[0], &min);
349 if(FAILED(hres))
350 return hres;
351
352 for(i=1; i < argc; i++) {
353 hres = to_number(ctx, argv[i], &d);
354 if(FAILED(hres))
355 return hres;
356
357 if(d < min || isnan(d))
358 min = d;
359 }
360
361 if(r)
362 *r = jsval_number(min);
363 return S_OK;
364}
365
366/* ECMA-262 3rd Edition 15.8.2.13 */
368 jsval_t *r)
369{
370 double x, y;
372
373 TRACE("\n");
374
375 if(argc < 2) {
376 if(r)
377 *r = jsval_number(NAN);
378 return S_OK;
379 }
380
381 hres = to_number(ctx, argv[0], &x);
382 if(FAILED(hres))
383 return hres;
384
385 hres = to_number(ctx, argv[1], &y);
386 if(FAILED(hres))
387 return hres;
388
389 if(r)
390 *r = jsval_number(pow(x, y));
391 return S_OK;
392}
393
394/* ECMA-262 3rd Edition 15.8.2.14 */
396 jsval_t *r)
397{
398 UINT x;
399
400 TRACE("\n");
401
402 if(!RtlGenRandom(&x, sizeof(x)))
403 return E_UNEXPECTED;
404
405 if(r)
406 *r = jsval_number((double)x/(double)UINT_MAX);
407 return S_OK;
408}
409
410/* ECMA-262 3rd Edition 15.8.2.15 */
412 jsval_t *r)
413{
414 double x;
416
417 TRACE("\n");
418
419 if(!argc) {
420 if(r)
421 *r = jsval_number(NAN);
422 return S_OK;
423 }
424
425 hres = to_number(ctx, argv[0], &x);
426 if(FAILED(hres))
427 return hres;
428
429 if(r)
430 *r = jsval_number(floor(x+0.5));
431 return S_OK;
432}
433
435 jsval_t *r)
436{
437 double x;
439
440 TRACE("\n");
441
442 if(!argc) {
443 if(r)
444 *r = jsval_number(NAN);
445 return S_OK;
446 }
447
448 hres = to_number(ctx, argv[0], &x);
449 if(FAILED(hres))
450 return hres;
451
452 if(r)
453 *r = jsval_number(sin(x));
454 return S_OK;
455}
456
458 jsval_t *r)
459{
460 double x;
462
463 TRACE("\n");
464
465 if(!argc) {
466 if(r)
467 *r = jsval_number(NAN);
468 return S_OK;
469 }
470
471 hres = to_number(ctx, argv[0], &x);
472 if(FAILED(hres))
473 return hres;
474
475 if(r)
476 *r = jsval_number(sqrt(x));
477 return S_OK;
478}
479
481 jsval_t *r)
482{
483 double x;
485
486 TRACE("\n");
487
488 if(!argc) {
489 if(r)
490 *r = jsval_number(NAN);
491 return S_OK;
492 }
493
494 hres = to_number(ctx, argv[0], &x);
495 if(FAILED(hres))
496 return hres;
497
498 if(r)
499 *r = jsval_number(tan(x));
500 return S_OK;
501}
502
503static const builtin_prop_t Math_props[] = {
522};
523
524static const builtin_info_t Math_info = {
526 {NULL, NULL, 0},
529 NULL,
530 NULL
531};
532
534{
535 jsdisp_t *math;
536 unsigned i;
538
539 struct {
540 const WCHAR *name;
541 DOUBLE val;
542 }constants[] = {
543 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
544 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
545 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
546 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
547 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
548 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
549 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
550 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
551 };
552
553 math = heap_alloc_zero(sizeof(jsdisp_t));
554 if(!math)
555 return E_OUTOFMEMORY;
556
557 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
558 if(FAILED(hres)) {
559 heap_free(math);
560 return hres;
561 }
562
563 for(i=0; i < ARRAY_SIZE(constants); i++) {
566 if(FAILED(hres)) {
567 jsdisp_release(math);
568 return hres;
569 }
570 }
571
572 *ret = math;
573 return S_OK;
574}
static int argc
Definition: ServiceArgs.c:12
_STLP_DECLSPEC complex< float > _STLP_CALL cos(const complex< float > &)
_STLP_DECLSPEC complex< float > _STLP_CALL tan(const complex< float > &)
_STLP_DECLSPEC complex< float > _STLP_CALL sin(const complex< float > &)
_STLP_DECLSPEC complex< float > _STLP_CALL sqrt(const complex< float > &)
Definition: complex.cpp:188
valarray< _Tp > acos(const valarray< _Tp > &__x)
Definition: _valarray.h:901
valarray< _Tp > atan(const valarray< _Tp > &__x)
Definition: _valarray.h:919
valarray< _Tp > asin(const valarray< _Tp > &__x)
Definition: _valarray.h:910
valarray< _Tp > atan2(const valarray< _Tp > &__x, const valarray< _Tp > &__y)
Definition: _valarray.h:928
static BOOL heap_free(void *mem)
Definition: appwiz.h:76
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ARRAY_SIZE(A)
Definition: main.h:33
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define NULL
Definition: types.h:112
static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:367
static const WCHAR roundW[]
Definition: math.c:57
static const WCHAR LOG10EW[]
Definition: math.c:37
static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:411
static const WCHAR atan2W[]
Definition: math.c:47
static const WCHAR sqrtW[]
Definition: math.c:59
static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:63
static const WCHAR ceilW[]
Definition: math.c:48
static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:299
static const WCHAR powW[]
Definition: math.c:55
static const builtin_info_t Math_info
Definition: math.c:524
static const WCHAR tanW[]
Definition: math.c:60
static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:183
static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:395
static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:206
static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:333
static const WCHAR SQRT2W[]
Definition: math.c:41
static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:480
static const WCHAR minW[]
Definition: math.c:54
static const WCHAR atanW[]
Definition: math.c:46
static const WCHAR LOG2EW[]
Definition: math.c:36
static const WCHAR LN2W[]
Definition: math.c:38
static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:275
static const WCHAR expW[]
Definition: math.c:50
static const WCHAR cosW[]
Definition: math.c:49
static const WCHAR PIW[]
Definition: math.c:40
static const WCHAR absW[]
Definition: math.c:43
static const WCHAR LN10W[]
Definition: math.c:39
static const WCHAR SQRT1_2W[]
Definition: math.c:42
static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:434
HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
Definition: math.c:533
static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:252
static const WCHAR randomW[]
Definition: math.c:56
static const WCHAR logW[]
Definition: math.c:52
static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:229
static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:457
static const WCHAR acosW[]
Definition: math.c:44
static const WCHAR asinW[]
Definition: math.c:45
static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:132
static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:86
static const builtin_prop_t Math_props[]
Definition: math.c:503
static const WCHAR EW[]
Definition: math.c:35
static const WCHAR sinW[]
Definition: math.c:58
static const WCHAR floorW[]
Definition: math.c:51
static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:109
static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:155
static const WCHAR maxW[]
Definition: math.c:53
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
double pow(double x, double y)
Definition: freeldr.c:113
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLbitfield flags
Definition: glext.h:7161
GLuint GLfloat * val
Definition: glext.h:7180
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define UINT_MAX
Definition: limits.h:41
_Check_return_ _CRTIMP double __cdecl floor(_In_ double x)
_Check_return_ _CRTIMP double __cdecl ceil(_In_ double x)
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
HRESULT jsdisp_define_data_property(jsdisp_t *obj, const WCHAR *name, unsigned flags, jsval_t value)
Definition: dispex.c:1801
HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
Definition: dispex.c:1030
static void jsdisp_release(jsdisp_t *jsdisp)
Definition: jscript.h:268
HRESULT to_number(script_ctx_t *, jsval_t, double *) DECLSPEC_HIDDEN
Definition: jsutils.c:609
#define PROPF_METHOD
Definition: jscript.h:97
@ JSCLASS_MATH
Definition: jscript.h:128
static jsval_t jsval_number(double n)
Definition: jsval.h:144
#define d
Definition: ke_i.h:81
#define M_PI
Definition: macros.h:263
#define isnan(x)
Definition: mingw_math.h:133
#define NAN
Definition: mesh.c:39
HRESULT hres
Definition: protocol.c:465
constants
Definition: resource.c:29
#define INFINITY
Definition: misc.c:36
DWORD exp
Definition: msg.c:16058
#define min(a, b)
Definition: monoChain.cc:55
#define M_SQRT2
#define argv
Definition: mplay32.c:18
unsigned int UINT
Definition: ndis.h:50
#define RtlGenRandom
Definition: ntsecapi.h:691
#define log(outFile, fmt,...)
Definition: util.h:15
#define TRACE(s)
Definition: solgame.cpp:4
Definition: jsval.h:54
Definition: name.c:39
#define max(a, b)
Definition: svc.c:63
double DOUBLE
Definition: typedefs.h:70
int ret
#define E_UNEXPECTED
Definition: winerror.h:2456
__wchar_t WCHAR
Definition: xmlstorage.h:180