mruby/c VM Source Code release 3.4
Loading...
Searching...
No Matches
c_math.c
Go to the documentation of this file.
1
13
14/***** Feature test switches ************************************************/
15/***** System headers *******************************************************/
16//@cond
17#include "vm_config.h"
18#include <math.h>
19//@endcond
20
21/***** Local headers ********************************************************/
22#include "mrubyc.h"
23
24/***** Constat values *******************************************************/
25/***** Macros ***************************************************************/
26/***** Typedefs *************************************************************/
27/***** Function prototypes **************************************************/
28/***** Local variables ******************************************************/
29/***** Global variables *****************************************************/
30/***** Signal catching functions ********************************************/
31/***** Local functions ******************************************************/
32#if MRBC_USE_FLOAT && MRBC_USE_MATH
33//================================================================
36static double to_double( struct VM *vm, const mrbc_value *v )
37{
38 switch( mrbc_type(*v) ) {
39 case MRBC_TT_INTEGER: return (double)v->i;
40 case MRBC_TT_FLOAT: return (double)v->d;
41 default: break;
42 }
43
44 mrbc_raise(vm, MRBC_CLASS(TypeError), 0);
45 return 0.0;
46}
47
48//================================================================
51static void c_math_acos(struct VM *vm, mrbc_value v[], int argc)
52{
53 v[0] = mrbc_float_value(vm, acos( to_double(vm, &v[1]) ));
54}
55
56//================================================================
59static void c_math_acosh(struct VM *vm, mrbc_value v[], int argc)
60{
61 v[0] = mrbc_float_value(vm, acosh( to_double(vm, &v[1]) ));
62}
63
64//================================================================
67static void c_math_asin(struct VM *vm, mrbc_value v[], int argc)
68{
69 v[0] = mrbc_float_value(vm, asin( to_double(vm, &v[1]) ));
70}
71
72//================================================================
75static void c_math_asinh(struct VM *vm, mrbc_value v[], int argc)
76{
77 v[0] = mrbc_float_value(vm, asinh( to_double(vm, &v[1]) ));
78}
79
80//================================================================
83static void c_math_atan(struct VM *vm, mrbc_value v[], int argc)
84{
85 v[0] = mrbc_float_value(vm, atan( to_double(vm, &v[1]) ));
86}
87
88//================================================================
91static void c_math_atan2(struct VM *vm, mrbc_value v[], int argc)
92{
93 v[0] = mrbc_float_value(vm, atan2( to_double(vm, &v[1]), to_double(vm, &v[2]) ));
94}
95
96//================================================================
99static void c_math_atanh(struct VM *vm, mrbc_value v[], int argc)
100{
101 v[0] = mrbc_float_value(vm, atanh( to_double(vm, &v[1]) ));
102}
103
104//================================================================
107static void c_math_cbrt(struct VM *vm, mrbc_value v[], int argc)
108{
109 v[0] = mrbc_float_value(vm, cbrt( to_double(vm, &v[1]) ));
110}
111
112//================================================================
115static void c_math_cos(struct VM *vm, mrbc_value v[], int argc)
116{
117 v[0] = mrbc_float_value(vm, cos( to_double(vm, &v[1]) ));
118}
119
120//================================================================
123static void c_math_cosh(struct VM *vm, mrbc_value v[], int argc)
124{
125 v[0] = mrbc_float_value(vm, cosh( to_double(vm, &v[1]) ));
126}
127
128//================================================================
131static void c_math_erf(struct VM *vm, mrbc_value v[], int argc)
132{
133 v[0] = mrbc_float_value(vm, erf( to_double(vm, &v[1]) ));
134}
135
136//================================================================
139static void c_math_erfc(struct VM *vm, mrbc_value v[], int argc)
140{
141 v[0] = mrbc_float_value(vm, erfc( to_double(vm, &v[1]) ));
142}
143
144//================================================================
147static void c_math_exp(struct VM *vm, mrbc_value v[], int argc)
148{
149 v[0] = mrbc_float_value(vm, exp( to_double(vm, &v[1]) ));
150}
151
152//================================================================
155static void c_math_hypot(struct VM *vm, mrbc_value v[], int argc)
156{
157 v[0] = mrbc_float_value(vm, hypot( to_double(vm, &v[1]), to_double(vm, &v[2]) ));
158}
159
160//================================================================
163static void c_math_ldexp(struct VM *vm, mrbc_value v[], int argc)
164{
165 int exp;
166 switch( mrbc_type(v[2]) ) {
167 case MRBC_TT_INTEGER: exp = v[2].i; break;
168 case MRBC_TT_FLOAT: exp = (int)v[2].d; break;
169 default:
170 mrbc_raise(vm, MRBC_CLASS(TypeError), 0);
171 return;
172 }
173
174 v[0] = mrbc_float_value(vm, ldexp( to_double(vm, &v[1]), exp ));
175}
176
177//================================================================
180static void c_math_log(struct VM *vm, mrbc_value v[], int argc)
181{
182 v[0] = mrbc_float_value(vm, log( to_double(vm, &v[1]) ));
183}
184
185//================================================================
188static void c_math_log10(struct VM *vm, mrbc_value v[], int argc)
189{
190 v[0] = mrbc_float_value(vm, log10( to_double(vm, &v[1]) ));
191}
192
193//================================================================
196static void c_math_log2(struct VM *vm, mrbc_value v[], int argc)
197{
198 v[0] = mrbc_float_value(vm, log2( to_double(vm, &v[1]) ));
199}
200
201//================================================================
204static void c_math_sin(struct VM *vm, mrbc_value v[], int argc)
205{
206 v[0] = mrbc_float_value(vm, sin( to_double(vm, &v[1]) ));
207}
208
209//================================================================
212static void c_math_sinh(struct VM *vm, mrbc_value v[], int argc)
213{
214 v[0] = mrbc_float_value(vm, sinh( to_double(vm, &v[1]) ));
215}
216
217//================================================================
220static void c_math_sqrt(struct VM *vm, mrbc_value v[], int argc)
221{
222 v[0] = mrbc_float_value(vm, sqrt( to_double(vm, &v[1]) ));
223}
224
225//================================================================
228static void c_math_tan(struct VM *vm, mrbc_value v[], int argc)
229{
230 v[0] = mrbc_float_value(vm, tan( to_double(vm, &v[1]) ));
231}
232
233//================================================================
236static void c_math_tanh(struct VM *vm, mrbc_value v[], int argc)
237{
238 v[0] = mrbc_float_value(vm, tanh( to_double(vm, &v[1]) ));
239}
240
241/***** Global functions *****************************************************/
242//================================================================
246{
247 static mrbc_value e = mrbc_float_value(0, M_E);
249
250 static mrbc_value pi = mrbc_float_value(0, M_PI);
251 mrbc_set_class_const( MRBC_CLASS(Math), MRBC_SYM(PI), &pi );
252}
253
254/* MRBC_AUTOGEN_METHOD_TABLE
255
256 MODULE("Math")
257 FILE("_autogen_module_math.h")
258
259 METHOD( "acos", c_math_acos )
260 METHOD( "acosh", c_math_acosh )
261 METHOD( "asin", c_math_asin )
262 METHOD( "asinh", c_math_asinh )
263 METHOD( "atan", c_math_atan )
264 METHOD( "atan2", c_math_atan2 )
265 METHOD( "atanh", c_math_atanh )
266 METHOD( "cbrt", c_math_cbrt )
267 METHOD( "cos", c_math_cos )
268 METHOD( "cosh", c_math_cosh )
269 METHOD( "erf", c_math_erf )
270 METHOD( "erfc", c_math_erfc )
271 METHOD( "exp", c_math_exp )
272 METHOD( "hypot", c_math_hypot )
273 METHOD( "ldexp", c_math_ldexp )
274 METHOD( "log", c_math_log )
275 METHOD( "log10", c_math_log10 )
276 METHOD( "log2", c_math_log2 )
277 METHOD( "sin", c_math_sin )
278 METHOD( "sinh", c_math_sinh )
279 METHOD( "sqrt", c_math_sqrt )
280 METHOD( "tan", c_math_tan )
281 METHOD( "tanh", c_math_tanh )
282*/
283#include "_autogen_module_math.h"
284
285#endif // MRBC_USE_FLOAT && MRBC_USE_MATH
void mrbc_init_module_math(void)
Definition c_math.c:245
static double to_double(struct VM *vm, const mrbc_value *v)
Definition c_math.c:36
#define MRBC_CLASS(cls)
Definition class.h:51
void mrbc_raise(struct VM *vm, struct RClass *exc_cls, const char *msg)
Definition error.c:150
int mrbc_set_class_const(const struct RClass *cls, mrbc_sym sym_id, mrbc_value *v)
Definition global.c:71
Include at once the necessary header files.
mrbc_float_t d
Definition value.h:156
mrbc_int_t i
Definition value.h:154
Virtual Machine.
Definition vm.h:140
#define MRBC_SYM(sym)
Definition symbol.h:30
#define mrbc_type(o)
Definition value.h:193
#define mrbc_float_value(vm, n)
Definition value.h:209
@ MRBC_TT_FLOAT
Float.
Definition value.h:85
@ MRBC_TT_INTEGER
Integer.
Definition value.h:83
struct RObject mrbc_value
Definition value.h:174
Global configuration of mruby/c VM's.