| 1 |
/* |
| 2 |
** numeric.c - Numeric, Integer, Float class |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <mruby.h> |
| 8 |
#include <mruby/numeric.h> |
| 9 |
#include <mruby/internal.h> |
| 10 |
#include <mruby/presym.h> |
| 11 |
|
| 12 |
MRB_API mrb_value |
| 13 |
mrb_num_add(mrb_state *mrb, mrb_value x, mrb_value y) |
| 14 |
{ |
| 15 |
#ifdef MRB_USE_BIGINT |
| 16 |
if (mrb_bigint_p(x)) { |
| 17 |
return mrb_bint_add(mrb, x, y); |
| 18 |
} |
| 19 |
#endif |
| 20 |
if (mrb_integer_p(x)) { |
| 21 |
return mrb_int_add(mrb, x, y); |
| 22 |
} |
| 23 |
#ifndef MRB_NO_FLOAT |
| 24 |
if (mrb_float_p(x)) { |
| 25 |
return mrb_float_value(mrb, mrb_float(x) + mrb_as_float(mrb, y)); |
| 26 |
} |
| 27 |
#endif |
| 28 |
#if defined(MRB_USE_RATIONAL) || defined(MRB_USE_COMPLEX) |
| 29 |
switch (mrb_type(x)) { |
| 30 |
#if defined(MRB_USE_RATIONAL) |
| 31 |
case MRB_TT_RATIONAL: |
| 32 |
return mrb_rational_add(mrb, x, y); |
| 33 |
#endif |
| 34 |
#if defined(MRB_USE_COMPLEX) |
| 35 |
case MRB_TT_COMPLEX: |
| 36 |
return mrb_complex_add(mrb, x, y); |
| 37 |
#endif |
| 38 |
default: |
| 39 |
break; |
| 40 |
} |
| 41 |
#endif |
| 42 |
mrb_raise(mrb, E_TYPE_ERROR, "no number addition"); |
| 43 |
return mrb_nil_value(); /* not reached */ |
| 44 |
} |
| 45 |
|
| 46 |
MRB_API mrb_value |
| 47 |
mrb_num_sub(mrb_state *mrb, mrb_value x, mrb_value y) |
| 48 |
{ |
| 49 |
#ifdef MRB_USE_BIGINT |
| 50 |
if (mrb_bigint_p(x)) { |
| 51 |
return mrb_bint_sub(mrb, x, y); |
| 52 |
} |
| 53 |
#endif |
| 54 |
if (mrb_integer_p(x)) { |
| 55 |
return mrb_int_sub(mrb, x, y); |
| 56 |
} |
| 57 |
#ifndef MRB_NO_FLOAT |
| 58 |
if (mrb_float_p(x)) { |
| 59 |
return mrb_float_value(mrb, mrb_float(x) - mrb_as_float(mrb, y)); |
| 60 |
} |
| 61 |
#endif |
| 62 |
#if defined(MRB_USE_RATIONAL) || defined(MRB_USE_COMPLEX) |
| 63 |
switch (mrb_type(x)) { |
| 64 |
#if defined(MRB_USE_RATIONAL) |
| 65 |
case MRB_TT_RATIONAL: |
| 66 |
return mrb_rational_sub(mrb, x, y); |
| 67 |
#endif |
| 68 |
#if defined(MRB_USE_COMPLEX) |
| 69 |
case MRB_TT_COMPLEX: |
| 70 |
return mrb_complex_sub(mrb, x, y); |
| 71 |
#endif |
| 72 |
default: |
| 73 |
break; |
| 74 |
} |
| 75 |
#endif |
| 76 |
mrb_raise(mrb, E_TYPE_ERROR, "no number subtraction"); |
| 77 |
return mrb_nil_value(); /* not reached */ |
| 78 |
} |
| 79 |
|
| 80 |
MRB_API mrb_value |
| 81 |
mrb_num_mul(mrb_state *mrb, mrb_value x, mrb_value y) |
| 82 |
{ |
| 83 |
#ifdef MRB_USE_BIGINT |
| 84 |
if (mrb_bigint_p(x)) { |
| 85 |
return mrb_bint_mul(mrb, x, y); |
| 86 |
} |
| 87 |
#endif |
| 88 |
if (mrb_integer_p(x)) { |
| 89 |
return mrb_int_mul(mrb, x, y); |
| 90 |
} |
| 91 |
#ifndef MRB_NO_FLOAT |
| 92 |
if (mrb_float_p(x)) { |
| 93 |
return mrb_float_value(mrb, mrb_float(x) * mrb_as_float(mrb, y)); |
| 94 |
} |
| 95 |
#endif |
| 96 |
#if defined(MRB_USE_RATIONAL) || defined(MRB_USE_COMPLEX) |
| 97 |
switch (mrb_type(x)) { |
| 98 |
#if defined(MRB_USE_RATIONAL) |
| 99 |
case MRB_TT_RATIONAL: |
| 100 |
return mrb_rational_mul(mrb, x, y); |
| 101 |
#endif |
| 102 |
#if defined(MRB_USE_COMPLEX) |
| 103 |
case MRB_TT_COMPLEX: |
| 104 |
return mrb_complex_mul(mrb, x, y); |
| 105 |
#endif |
| 106 |
default: |
| 107 |
break; |
| 108 |
} |
| 109 |
#endif |
| 110 |
mrb_raise(mrb, E_TYPE_ERROR, "no number multiply"); |
| 111 |
return mrb_nil_value(); /* not reached */ |
| 112 |
} |