-
Notifications
You must be signed in to change notification settings - Fork 1
/
math.c
executable file
·215 lines (184 loc) · 4.35 KB
/
math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <team@zephir-lang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. If you did not receive
* a copy of the license it is available through the world-wide-web at the
* following url: https://docs.zephir-lang.com/en/latest/license
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include <ext/standard/php_string.h>
#include <ext/standard/php_math.h>
#include <ext/standard/php_rand.h>
#include "php_ext.h"
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/string.h"
#include "kernel/operators.h"
#include "Zend/zend_operators.h"
double zephir_sqrt(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return sqrt(Z_LVAL_P(op1));
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return sqrt(zephir_get_numberval(op1));
}
double zephir_sin(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return sin(Z_LVAL_P(op1));
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return sin(zephir_get_numberval(op1));
}
double zephir_asin(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return asin(Z_LVAL_P(op1));
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return asin(zephir_get_numberval(op1));
}
double zephir_cos(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return cos(Z_LVAL_P(op1));
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return cos(zephir_get_numberval(op1));
}
double zephir_acos(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return acos(Z_LVAL_P(op1));
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return acos(zephir_get_numberval(op1));
}
double zephir_tan(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return tan(Z_LVAL_P(op1));
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return tan(zephir_get_numberval(op1));
}
double zephir_floor(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return (double) Z_LVAL_P(op1);
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return floor(zephir_get_numberval(op1));
}
double zephir_ceil(zval *op1)
{
switch (Z_TYPE_P(op1)) {
case IS_LONG:
return (double) Z_LVAL_P(op1);
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return ceil(zephir_get_numberval(op1));
}
extern double _php_math_round(double value, int places, int mode);
void zephir_round(zval *return_value, zval *op1, zval *op2, zval *op3)
{
int places = 0;
long mode = PHP_ROUND_HALF_UP;
double return_val;
convert_scalar_to_number_ex(op1);
if (op2) {
places = zephir_get_intval_ex(op2);
}
if (op3) {
mode = zephir_get_intval_ex(op3);
}
switch (Z_TYPE_P(op1)) {
case IS_LONG:
/* Simple case - long that doesn't need to be rounded. */
if (places >= 0) {
RETURN_DOUBLE((double) Z_LVAL_P(op1));
}
/* break omitted intentionally */
case IS_DOUBLE:
return_val = (Z_TYPE_P(op1) == IS_LONG) ? (double)Z_LVAL_P(op1) : Z_DVAL_P(op1);
return_val = _php_math_round(return_val, places, mode);
RETURN_DOUBLE(return_val);
break;
default:
RETURN_FALSE;
break;
}
}
zend_long zephir_mt_rand(zend_long min, zend_long max)
{
if (max < min) {
php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min);
return 0;
}
if (!BG(mt_rand_is_seeded)) {
php_mt_srand(GENERATE_SEED());
}
return php_mt_rand_range(min, max);
}
double zephir_ldexp(zval *value, zval *expval)
{
int exp = (int) zephir_get_numberval(expval);
switch (Z_TYPE_P(value)) {
case IS_LONG:
return (double) ldexp(Z_LVAL_P(value), exp);
case IS_DOUBLE:
return (double) ldexp(Z_DVAL_P(value), exp);
case IS_ARRAY:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_WARNING, "Unsupported operand types");
break;
}
return ldexp(zephir_get_numberval(value), exp);
}