Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7641 views
1
#include "jsi.h"
2
#include "jsvalue.h"
3
#include "jsbuiltin.h"
4
5
static void Math_abs(js_State *J)
6
{
7
js_pushnumber(J, fabs(js_tonumber(J, 1)));
8
}
9
10
static void Math_acos(js_State *J)
11
{
12
js_pushnumber(J, acos(js_tonumber(J, 1)));
13
}
14
15
static void Math_asin(js_State *J)
16
{
17
js_pushnumber(J, asin(js_tonumber(J, 1)));
18
}
19
20
static void Math_atan(js_State *J)
21
{
22
js_pushnumber(J, atan(js_tonumber(J, 1)));
23
}
24
25
static void Math_atan2(js_State *J)
26
{
27
double y = js_tonumber(J, 1);
28
double x = js_tonumber(J, 2);
29
js_pushnumber(J, atan2(y, x));
30
}
31
32
static void Math_ceil(js_State *J)
33
{
34
js_pushnumber(J, ceil(js_tonumber(J, 1)));
35
}
36
37
static void Math_cos(js_State *J)
38
{
39
js_pushnumber(J, cos(js_tonumber(J, 1)));
40
}
41
42
static void Math_exp(js_State *J)
43
{
44
js_pushnumber(J, exp(js_tonumber(J, 1)));
45
}
46
47
static void Math_floor(js_State *J)
48
{
49
js_pushnumber(J, floor(js_tonumber(J, 1)));
50
}
51
52
static void Math_log(js_State *J)
53
{
54
js_pushnumber(J, log(js_tonumber(J, 1)));
55
}
56
57
static void Math_pow(js_State *J)
58
{
59
double x = js_tonumber(J, 1);
60
double y = js_tonumber(J, 2);
61
if (!isfinite(y) && fabs(x) == 1)
62
js_pushnumber(J, NAN);
63
else
64
js_pushnumber(J, pow(x,y));
65
}
66
67
static void Math_random(js_State *J)
68
{
69
js_pushnumber(J, rand() / (RAND_MAX + 1.0));
70
}
71
72
static void Math_round(js_State *J)
73
{
74
double x = js_tonumber(J, 1);
75
double r = round(x);
76
if (r - x == -0.5)
77
js_pushnumber(J, x == -0.5 ? -0.0 : r + 1.0);
78
else
79
js_pushnumber(J, r);
80
}
81
82
static void Math_sin(js_State *J)
83
{
84
js_pushnumber(J, sin(js_tonumber(J, 1)));
85
}
86
87
static void Math_sqrt(js_State *J)
88
{
89
js_pushnumber(J, sqrt(js_tonumber(J, 1)));
90
}
91
92
static void Math_tan(js_State *J)
93
{
94
js_pushnumber(J, tan(js_tonumber(J, 1)));
95
}
96
97
static void Math_max(js_State *J)
98
{
99
unsigned int i, n = js_gettop(J);
100
double x = -INFINITY;
101
for (i = 1; i < n; ++i) {
102
double y = js_tonumber(J, i);
103
if (isnan(y)) {
104
x = y;
105
break;
106
}
107
if (signbit(x) == signbit(y))
108
x = x > y ? x : y;
109
else if (signbit(x))
110
x = y;
111
}
112
js_pushnumber(J, x);
113
}
114
115
static void Math_min(js_State *J)
116
{
117
unsigned int i, n = js_gettop(J);
118
double x = INFINITY;
119
for (i = 1; i < n; ++i) {
120
double y = js_tonumber(J, i);
121
if (isnan(y)) {
122
x = y;
123
break;
124
}
125
if (signbit(x) == signbit(y))
126
x = x < y ? x : y;
127
else if (signbit(y))
128
x = y;
129
}
130
js_pushnumber(J, x);
131
}
132
133
void jsB_initmath(js_State *J)
134
{
135
js_pushobject(J, jsV_newobject(J, JS_CMATH, J->Object_prototype));
136
{
137
jsB_propn(J, "E", 2.7182818284590452354);
138
jsB_propn(J, "LN10", 2.302585092994046);
139
jsB_propn(J, "LN2", 0.6931471805599453);
140
jsB_propn(J, "LOG2E", 1.4426950408889634);
141
jsB_propn(J, "LOG10E", 0.4342944819032518);
142
jsB_propn(J, "PI", 3.1415926535897932);
143
jsB_propn(J, "SQRT1_2", 0.7071067811865476);
144
jsB_propn(J, "SQRT2", 1.4142135623730951);
145
146
jsB_propf(J, "abs", Math_abs, 1);
147
jsB_propf(J, "acos", Math_acos, 1);
148
jsB_propf(J, "asin", Math_asin, 1);
149
jsB_propf(J, "atan", Math_atan, 1);
150
jsB_propf(J, "atan2", Math_atan2, 2);
151
jsB_propf(J, "ceil", Math_ceil, 1);
152
jsB_propf(J, "cos", Math_cos, 1);
153
jsB_propf(J, "exp", Math_exp, 1);
154
jsB_propf(J, "floor", Math_floor, 1);
155
jsB_propf(J, "log", Math_log, 1);
156
jsB_propf(J, "max", Math_max, 0);
157
jsB_propf(J, "min", Math_min, 0);
158
jsB_propf(J, "pow", Math_pow, 2);
159
jsB_propf(J, "random", Math_random, 0);
160
jsB_propf(J, "round", Math_round, 1);
161
jsB_propf(J, "sin", Math_sin, 1);
162
jsB_propf(J, "sqrt", Math_sqrt, 1);
163
jsB_propf(J, "tan", Math_tan, 1);
164
}
165
js_defglobal(J, "Math", JS_DONTENUM);
166
}
167
168