rational.c, complex.c: expose arithmetic operation functions.

* mrb_{rational,complex}_{add,sub,mul,div}
This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-03-12 08:35:07 +09:00
parent 55476efb62
commit 10001ff67f
2 changed files with 57 additions and 15 deletions
+32 -8
View File
@@ -168,10 +168,9 @@ complex_eq(mrb_state *mrb, mrb_value x)
return mrb_bool_value(mrb_complex_eq(mrb, x, y));
}
static mrb_value
complex_add(mrb_state *mrb, mrb_value x)
mrb_value
mrb_complex_add(mrb_state *mrb, mrb_value x, mrb_value y)
{
mrb_value y = mrb_get_arg1(mrb);
struct mrb_complex *p1 = complex_ptr(mrb, x);
switch (mrb_type(y)) {
@@ -190,9 +189,15 @@ complex_add(mrb_state *mrb, mrb_value x)
}
static mrb_value
complex_sub(mrb_state *mrb, mrb_value x)
complex_add(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
return mrb_complex_add(mrb, x, y);
}
mrb_value
mrb_complex_sub(mrb_state *mrb, mrb_value x, mrb_value y)
{
struct mrb_complex *p1 = complex_ptr(mrb, x);
switch (mrb_type(y)) {
@@ -211,9 +216,15 @@ complex_sub(mrb_state *mrb, mrb_value x)
}
static mrb_value
complex_mul(mrb_state *mrb, mrb_value x)
complex_sub(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
return mrb_complex_sub(mrb, x, y);
}
mrb_value
mrb_complex_mul(mrb_state *mrb, mrb_value x, mrb_value y)
{
struct mrb_complex *p1 = complex_ptr(mrb, x);
switch (mrb_type(y)) {
@@ -232,6 +243,13 @@ complex_mul(mrb_state *mrb, mrb_value x)
}
}
static mrb_value
complex_mul(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
return mrb_complex_mul(mrb, x, y);
}
/* Arithmetic on (significand, exponent) pairs avoids premature overflow in
complex division */
struct float_pair {
@@ -272,11 +290,10 @@ div_pair(struct float_pair *q, struct float_pair const *a,
q->x = a->x - b->x;
}
static mrb_value
complex_div(mrb_state *mrb, mrb_value self)
mrb_value
mrb_complex_div(mrb_state *mrb, mrb_value self, mrb_value rhs)
{
struct mrb_complex *a, *b;
mrb_value rhs = mrb_get_arg1(mrb);
a = complex_ptr(mrb, self);
if (mrb_type(rhs) != MRB_TT_COMPLEX) {
@@ -324,6 +341,13 @@ complex_div(mrb_state *mrb, mrb_value self)
return complex_new(mrb, F(ldexp)(zr.s, zr.x), F(ldexp)(zi.s, zi.x));
}
static mrb_value
complex_div(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
return mrb_complex_div(mrb, x, y);
}
mrb_int mrb_div_int(mrb_state *mrb, mrb_int x, mrb_int y);
mrb_value mrb_rational_new(mrb_state *mrb, mrb_int n, mrb_int d);
mrb_value mrb_rational_div(mrb_state *mrb, mrb_value x, mrb_value y);
+25 -7
View File
@@ -479,11 +479,10 @@ rational_minus(mrb_state *mrb, mrb_value x)
return rational_new(mrb, -n, p->denominator);
}
static mrb_value
rational_add(mrb_state *mrb, mrb_value x)
mrb_value
mrb_rational_add(mrb_state *mrb, mrb_value x, mrb_value y)
{
struct mrb_rational *p1 = rational_ptr(mrb, x);
mrb_value y = mrb_get_arg1(mrb);
switch (mrb_type(y)) {
case MRB_TT_INTEGER:
@@ -519,10 +518,16 @@ rational_add(mrb_state *mrb, mrb_value x)
}
static mrb_value
rational_sub(mrb_state *mrb, mrb_value x)
rational_add(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
return mrb_rational_add(mrb, x, y);
}
mrb_value
mrb_rational_sub(mrb_state *mrb, mrb_value x, mrb_value y)
{
struct mrb_rational *p1 = rational_ptr(mrb, x);
mrb_value y = mrb_get_arg1(mrb);
switch (mrb_type(y)) {
case MRB_TT_INTEGER:
@@ -565,10 +570,16 @@ rational_sub(mrb_state *mrb, mrb_value x)
}
static mrb_value
rational_mul(mrb_state *mrb, mrb_value x)
rational_sub(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
return mrb_rational_sub(mrb, x, y);
}
mrb_value
mrb_rational_mul(mrb_state *mrb, mrb_value x, mrb_value y)
{
struct mrb_rational *p1 = rational_ptr(mrb, x);
mrb_value y = mrb_get_arg1(mrb);
switch (mrb_type(y)) {
case MRB_TT_INTEGER:
@@ -600,6 +611,13 @@ rational_mul(mrb_state *mrb, mrb_value x)
}
}
static mrb_value
rational_mul(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
return mrb_rational_mul(mrb, x, y);
}
mrb_value
mrb_rational_div(mrb_state *mrb, mrb_value x, mrb_value y)
{