2 #include <mruby/class.h>
3 #include <mruby/numeric.h>
6 #ifdef MRB_WITHOUT_FLOAT
7 # error Complex conflicts 'MRB_WITHOUT_FLOAT' configuration in your 'build_config.rb'
21 #if defined(MRB_64BIT) || defined(MRB_USE_FLOAT)
23 #define COMPLEX_USE_ISTRUCT
25 #include <mruby/istruct.h>
27 #define complex_ptr(mrb, v) (struct mrb_complex*)mrb_istruct_ptr(v)
30 complex_alloc(mrb_state *mrb, struct RClass *c, struct mrb_complex **p)
34 s = (struct RIStruct*)mrb_obj_alloc(mrb, MRB_TT_ISTRUCT, c);
35 *p = (struct mrb_complex*)s->inline_data;
37 return (struct RBasic*)s;
42 #include <mruby/data.h>
44 static const struct mrb_data_type mrb_complex_type = {"Complex", mrb_free};
47 complex_alloc(mrb_state *mrb, struct RClass *c, struct mrb_complex **p)
51 Data_Make_Struct(mrb, c, struct mrb_complex, &mrb_complex_type, *p, d);
53 return (struct RBasic*)d;
56 static struct mrb_complex*
57 complex_ptr(mrb_state *mrb, mrb_value v)
59 struct mrb_complex *p;
61 p = DATA_GET_PTR(mrb, v, &mrb_complex_type, struct mrb_complex);
63 mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized complex");
70 complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary)
72 struct RClass *c = mrb_class_get(mrb, "Complex");
73 struct mrb_complex *p;
74 struct RBasic *comp = complex_alloc(mrb, c, &p);
76 p->imaginary = imaginary;
77 MRB_SET_FROZEN_FLAG(comp);
79 return mrb_obj_value(comp);
83 complex_real(mrb_state *mrb, mrb_value self)
85 struct mrb_complex *p = complex_ptr(mrb, self);
86 return mrb_float_value(mrb, p->real);
90 complex_imaginary(mrb_state *mrb, mrb_value self)
92 struct mrb_complex *p = complex_ptr(mrb, self);
93 return mrb_float_value(mrb, p->imaginary);
97 complex_s_rect(mrb_state *mrb, mrb_value self)
99 mrb_float real, imaginary = 0.0;
101 mrb_get_args(mrb, "f|f", &real, &imaginary);
102 return complex_new(mrb, real, imaginary);
106 complex_to_f(mrb_state *mrb, mrb_value self)
108 struct mrb_complex *p = complex_ptr(mrb, self);
110 if (p->imaginary != 0) {
111 mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %v into Float", self);
114 return mrb_float_value(mrb, p->real);
118 complex_to_i(mrb_state *mrb, mrb_value self)
120 struct mrb_complex *p = complex_ptr(mrb, self);
122 if (p->imaginary != 0) {
123 mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %v into Float", self);
125 return mrb_int_value(mrb, p->real);
129 complex_to_c(mrb_state *mrb, mrb_value self)
134 /* Arithmetic on (significand, exponent) pairs avoids premature overflow in
142 add_pair(struct float_pair *s, struct float_pair const *a,
143 struct float_pair const *b)
147 } else if (a->s == 0.0F) {
149 } else if (a->x >= b->x) {
150 s->s = a->s + F(ldexp)(b->s, b->x - a->x);
153 s->s = F(ldexp)(a->s, a->x - b->x) + b->s;
159 mul_pair(struct float_pair *p, struct float_pair const *a,
160 struct float_pair const *b)
167 div_pair(struct float_pair *q, struct float_pair const *a,
168 struct float_pair const *b)
175 complex_div(mrb_state *mrb, mrb_value self)
177 mrb_value rhs = mrb_get_arg1(mrb);
178 struct mrb_complex *a, *b;
179 struct float_pair ar, ai, br, bi;
180 struct float_pair br2, bi2;
181 struct float_pair div;
182 struct float_pair ar_br, ai_bi;
183 struct float_pair ai_br, ar_bi;
184 struct float_pair zr, zi;
186 a = complex_ptr(mrb, self);
187 b = complex_ptr(mrb, rhs);
189 /* Split floating point components into significand and exponent */
190 ar.s = F(frexp)(a->real, &ar.x);
191 ai.s = F(frexp)(a->imaginary, &ai.x);
192 br.s = F(frexp)(b->real, &br.x);
193 bi.s = F(frexp)(b->imaginary, &bi.x);
195 /* Perform arithmetic on (significand, exponent) pairs to produce
199 mul_pair(&br2, &br, &br);
200 mul_pair(&bi2, &bi, &bi);
201 add_pair(&div, &br2, &bi2);
204 mul_pair(&ar_br, &ar, &br);
205 mul_pair(&ai_bi, &ai, &bi);
206 add_pair(&zr, &ar_br, &ai_bi);
207 div_pair(&zr, &zr, &div);
209 /* imaginary component */
210 mul_pair(&ai_br, &ai, &br);
211 mul_pair(&ar_bi, &ar, &bi);
213 add_pair(&zi, &ai_br, &ar_bi);
214 div_pair(&zi, &zi, &div);
216 /* assemble the result */
217 return complex_new(mrb, F(ldexp)(zr.s, zr.x), F(ldexp)(zi.s, zi.x));
220 void mrb_mruby_complex_gem_init(mrb_state *mrb)
224 #ifdef COMPLEX_USE_ISTRUCT
225 mrb_assert(sizeof(struct mrb_complex) < ISTRUCT_DATA_SIZE);
227 comp = mrb_define_class(mrb, "Complex", mrb_class_get(mrb, "Numeric"));
228 #ifdef COMPLEX_USE_ISTRUCT
229 MRB_SET_INSTANCE_TT(comp, MRB_TT_ISTRUCT);
231 MRB_SET_INSTANCE_TT(comp, MRB_TT_DATA);
233 mrb_undef_class_method(mrb, comp, "new");
234 mrb_define_class_method(mrb, comp, "rectangular", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
235 mrb_define_class_method(mrb, comp, "rect", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
236 mrb_define_method(mrb, mrb->kernel_module, "Complex", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
237 mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE());
238 mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE());
239 mrb_define_method(mrb, comp, "to_f", complex_to_f, MRB_ARGS_NONE());
240 mrb_define_method(mrb, comp, "to_i", complex_to_i, MRB_ARGS_NONE());
241 mrb_define_method(mrb, comp, "to_c", complex_to_c, MRB_ARGS_NONE());
242 mrb_define_method(mrb, comp, "__div__", complex_div, MRB_ARGS_REQ(1));
246 mrb_mruby_complex_gem_final(mrb_state* mrb)