8#include "ruby/internal/config.h"
12# define _USE_MATH_DEFINES 1
20#include "internal/array.h"
21#include "internal/class.h"
22#include "internal/complex.h"
23#include "internal/math.h"
24#include "internal/numeric.h"
25#include "internal/object.h"
26#include "internal/rational.h"
27#include "internal/string.h"
28#include "ruby_assert.h"
30#define ZERO INT2FIX(0)
34#define RFLOAT_0 DBL2NUM(0)
41static ID id_abs, id_arg,
42 id_denominator, id_numerator,
43 id_real_p, id_i_real, id_i_imag,
44 id_finite_p, id_infinite_p, id_rationalize,
48#define id_negate idUMinus
58 return rb_funcall(x, id_##n, 0);\
63f_##n(VALUE x, VALUE y)\
65 return rb_funcall(x, id_##n, 1, y);\
68#define PRESERVE_SIGNEDZERO
74 LIKELY(rb_method_basic_definition_p(
rb_cInteger, idPLUS))) {
79 return rb_int_plus(x, y);
82 LIKELY(rb_method_basic_definition_p(
rb_cFloat, idPLUS))) {
85 return rb_float_plus(x, y);
88 LIKELY(rb_method_basic_definition_p(
rb_cRational, idPLUS))) {
91 return rb_rational_plus(x, y);
111 return RTEST(rb_int_gt(x, y));
114 return RTEST(rb_float_gt(x, y));
116 int const cmp = rb_cmpint(rb_rational_cmp(x, y), x, y);
126 LIKELY(rb_method_basic_definition_p(
rb_cInteger, idMULT))) {
127 if (FIXNUM_ZERO_P(y))
131 if (x == ONE)
return y;
132 if (y == ONE)
return x;
133 return rb_int_mul(x, y);
136 LIKELY(rb_method_basic_definition_p(
rb_cFloat, idMULT))) {
137 if (y == ONE)
return x;
138 return rb_float_mul(x, y);
141 LIKELY(rb_method_basic_definition_p(
rb_cRational, idMULT))) {
142 if (y == ONE)
return x;
143 return rb_rational_mul(x, y);
145 else if (LIKELY(rb_method_basic_definition_p(
CLASS_OF(x), idMULT))) {
146 if (y == ONE)
return x;
154 if (FIXNUM_ZERO_P(y) &&
155 LIKELY(rb_method_basic_definition_p(
CLASS_OF(x), idMINUS))) {
165 return rb_int_abs(x);
168 return rb_float_abs(x);
171 return rb_rational_abs(x);
174 return rb_complex_abs(x);
186 return numeric_arg(x);
192 return numeric_arg(x);
195 return rb_complex_arg(x);
204 return RRATIONAL(x)->num;
207 return rb_float_numerator(x);
213f_denominator(
VALUE x)
216 return RRATIONAL(x)->den;
219 return rb_float_denominator(x);
228 return rb_int_uminus(x);
231 return rb_float_uminus(x);
234 return rb_rational_uminus(x);
237 return rb_complex_uminus(x);
242static bool nucomp_real_p(
VALUE self);
257 return nucomp_real_p(x);
266 return rb_str_to_inum(x, 10, 0);
297 return rb_numeric_quo(x, y);
299 return rb_float_div(x, y);
301 return rb_numeric_quo(x, y);
303 return rb_funcallv(x, id_quo, 1, &y);
310 return INT_NEGATIVE_P(x);
314 return INT_NEGATIVE_P(RRATIONAL(x)->num);
315 return rb_num_negative_p(x);
318#define f_positive_p(x) (!f_negative_p(x))
324 return FLOAT_ZERO_P(x);
327 return FIXNUM_ZERO_P(x);
330 const VALUE num = RRATIONAL(x)->num;
331 return FIXNUM_ZERO_P(num);
336#define f_nonzero_p(x) (!f_zero_p(x))
339always_finite_type_p(
VALUE x)
349 if (always_finite_type_p(x)) {
355 return RTEST(rb_funcallv(x, id_finite_p, 0, 0));
361 if (always_finite_type_p(x)) {
367 return RTEST(rb_funcallv(x, id_infinite_p, 0, 0));
382#define k_exact_p(x) (!RB_FLOAT_TYPE_P(x))
384#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
387 struct RComplex *dat = RCOMPLEX(x)
389#define get_dat2(x,y) \
390 struct RComplex *adat = RCOMPLEX(x), *bdat = RCOMPLEX(y)
395 NEWOBJ_OF(obj,
struct RComplex, klass,
398 RCOMPLEX_SET_REAL(obj, real);
399 RCOMPLEX_SET_IMAG(obj, imag);
406nucomp_s_alloc(
VALUE klass)
408 return nucomp_s_new_internal(klass, ZERO, ZERO);
415 return nucomp_s_new_internal(klass, x, ZERO);
423 return nucomp_s_new_internal(klass, x, y);
426WARN_UNUSED_RESULT(
inline static VALUE nucomp_real_check(
VALUE num));
428nucomp_real_check(
VALUE num)
434 VALUE real = RCOMPLEX(num)->real;
438 if (!k_numeric_p(num) || !f_real_p(num))
447 int complex_r, complex_i;
450 if (!complex_r && !complex_i) {
451 return nucomp_s_new_internal(klass, real, imag);
453 else if (!complex_r) {
456 return nucomp_s_new_internal(klass,
457 f_sub(real, dat->imag),
458 f_add(ZERO, dat->real));
460 else if (!complex_i) {
463 return nucomp_s_new_internal(klass,
465 f_add(dat->imag, imag));
468 get_dat2(real, imag);
470 return nucomp_s_new_internal(klass,
471 f_sub(adat->real, bdat->imag),
472 f_add(adat->imag, bdat->real));
493nucomp_s_new(
int argc,
VALUE *argv,
VALUE klass)
499 real = nucomp_real_check(real);
503 real = nucomp_real_check(real);
504 imag = nucomp_real_check(imag);
508 return nucomp_s_new_internal(klass, real, imag);
517 y = f_add(dat->imag, y);
519 return nucomp_s_canonicalize_internal(klass, x, y);
577nucomp_f_complex(
int argc,
VALUE *argv,
VALUE klass)
582 if (
rb_scan_args(argc, argv,
"11:", &a1, &a2, &opts) == 1) {
586 raise = rb_opts_exception_p(opts, raise);
596m_##n##_bang(VALUE x)\
598 return rb_math_##n(x);\
608 return rb_math_log(1, &x);
618 return m_cos_bang(x);
622 f_mul(m_cos_bang(dat->real),
623 m_cosh_bang(dat->imag)),
624 f_mul(f_negate(m_sin_bang(dat->real)),
625 m_sinh_bang(dat->imag)));
633 return m_sin_bang(x);
637 f_mul(m_sin_bang(dat->real),
638 m_cosh_bang(dat->imag)),
639 f_mul(m_cos_bang(dat->real),
640 m_sinh_bang(dat->imag)));
647 if (f_zero_p(x) || f_zero_p(y)) {
648 return nucomp_s_new_internal(klass, x, RFLOAT_0);
656 else if (arg == M_PI_2) {
660 else if (arg == M_PI_2+M_PI) {
666 const double real = abs * cos(arg), imag = abs * sin(arg);
671 const double ax = sin(arg), ay = cos(arg);
675 return nucomp_s_new_internal(klass, x, y);
677 return nucomp_s_canonicalize_internal(klass,
685 x = nucomp_real_check(x);
686 y = nucomp_real_check(y);
687 return f_complex_polar_real(klass, x, y);
691# define cospi(x) __cospi(x)
693# define cospi(x) cos((x) * M_PI)
696# define sinpi(x) __sinpi(x)
698# define sinpi(x) sin((x) * M_PI)
702rb_dbl_complex_new_polar_pi(
double abs,
double ang)
705 const double fr = modf(ang, &fi);
706 int pos = fr == +0.5;
708 if (pos || fr == -0.5) {
709 if ((modf(fi / 2.0, &fi) != fr) ^ pos) abs = -abs;
710 return rb_complex_new(RFLOAT_0,
DBL2NUM(abs));
712 else if (fr == 0.0) {
713 if (modf(fi / 2.0, &fi) != 0.0) abs = -abs;
717 const double real = abs * cospi(ang), imag = abs * sinpi(ang);
739nucomp_s_polar(
int argc,
VALUE *argv,
VALUE klass)
744 abs = nucomp_real_check(abs);
746 arg = nucomp_real_check(arg);
751 return f_complex_polar_real(klass, abs, arg);
814 return f_complex_new2(
CLASS_OF(self),
815 f_negate(dat->real), f_negate(dat->imag));
846 get_dat2(self, other);
848 real = f_add(adat->real, bdat->real);
849 imag = f_add(adat->imag, bdat->imag);
851 return f_complex_new2(
CLASS_OF(self), real, imag);
853 if (k_numeric_p(other) && f_real_p(other)) {
856 return f_complex_new2(
CLASS_OF(self),
857 f_add(dat->real, other), dat->imag);
881 get_dat2(self, other);
883 real = f_sub(adat->real, bdat->real);
884 imag = f_sub(adat->imag, bdat->imag);
886 return f_complex_new2(
CLASS_OF(self), real, imag);
888 if (k_numeric_p(other) && f_real_p(other)) {
891 return f_complex_new2(
CLASS_OF(self),
892 f_sub(dat->real, other), dat->imag);
913 bool arzero = f_zero_p(areal);
914 bool aizero = f_zero_p(aimag);
915 bool brzero = f_zero_p(breal);
916 bool bizero = f_zero_p(bimag);
917 *real = f_sub(safe_mul(areal, breal, arzero, brzero),
918 safe_mul(aimag, bimag, aizero, bizero));
919 *imag = f_add(safe_mul(areal, bimag, arzero, bizero),
920 safe_mul(aimag, breal, aizero, brzero));
942 get_dat2(self, other);
944 comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);
946 return f_complex_new2(
CLASS_OF(self), real, imag);
948 if (k_numeric_p(other) && f_real_p(other)) {
951 return f_complex_new2(
CLASS_OF(self),
952 f_mul(dat->real, other),
953 f_mul(dat->imag, other));
965 get_dat2(self, other);
970 if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
971 r = (*func)(bdat->imag, bdat->real);
972 n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
973 x = (*func)(f_add(adat->real, f_mul(adat->imag, r)), n);
974 y = (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n);
977 r = (*func)(bdat->real, bdat->imag);
978 n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
979 x = (*func)(f_add(f_mul(adat->real, r), adat->imag), n);
980 y = (*func)(f_sub(f_mul(adat->imag, r), adat->real), n);
983 x = rb_rational_canonicalize(x);
984 y = rb_rational_canonicalize(y);
986 return f_complex_new2(
CLASS_OF(self), x, y);
988 if (k_numeric_p(other) && f_real_p(other)) {
991 x = rb_rational_canonicalize((*func)(dat->real, other));
992 y = rb_rational_canonicalize((*func)(dat->imag, other));
993 return f_complex_new2(
CLASS_OF(self), x, y);
998#define rb_raise_zerodiv() rb_raise(rb_eZeroDivError, "divided by 0")
1016 return f_divide(self, other, f_quo, id_quo);
1019#define nucomp_quo rb_complex_div
1033 return f_divide(self, other, f_fdiv, id_fdiv);
1037f_reciprocal(
VALUE x)
1039 return f_quo(ONE, x);
1054complex_pow_for_special_angle(
VALUE self,
VALUE other)
1063 if (f_zero_p(dat->imag)) {
1067 else if (f_zero_p(dat->real)) {
1071 else if (f_eqeq_p(dat->real, dat->imag)) {
1075 else if (f_eqeq_p(dat->real, f_negate(dat->imag))) {
1083 if (UNDEF_P(x))
return x;
1085 if (f_negative_p(x)) {
1092 zx = rb_num_pow(x, other);
1097 rb_int_div(other, TWO)
1099 if (rb_int_odd_p(other)) {
1103 static const int dirs[][2] = {
1104 {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}
1109 switch (dirs[z_dir][0]) {
1110 case 0: zr = zero_for(zx);
break;
1111 case 1: zr = zx;
break;
1112 case -1: zr = f_negate(zx);
break;
1114 switch (dirs[z_dir][1]) {
1115 case 0: zi = zero_for(zx);
break;
1116 case 1: zi = zx;
break;
1117 case -1: zi = f_negate(zx);
break;
1119 return nucomp_s_new_internal(
CLASS_OF(self), zr, zi);
1136 if (k_numeric_p(other) && k_exact_zero_p(other))
1137 return f_complex_new_bang1(
CLASS_OF(self), ONE);
1140 other = RRATIONAL(other)->num;
1145 if (k_exact_zero_p(dat->imag))
1151 return nucomp_s_new_internal(
CLASS_OF(self), dat->real, dat->imag);
1154 VALUE result = complex_pow_for_special_angle(self, other);
1155 if (!UNDEF_P(result))
return result;
1158 VALUE r, theta, nr, ntheta;
1163 theta = f_arg(self);
1165 nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
1166 f_mul(dat->imag, theta)));
1167 ntheta = f_add(f_mul(theta, dat->real),
1168 f_mul(dat->imag, m_log_bang(r)));
1169 return f_complex_polar(
CLASS_OF(self), nr, ntheta);
1174 return nucomp_s_new_internal(
CLASS_OF(self), ONE, ZERO);
1177 self = f_reciprocal(self);
1178 other = rb_int_uminus(other);
1183 VALUE xr = dat->real, xi = dat->imag, zr = xr, zi = xi;
1186 zr = rb_num_pow(zr, other);
1188 else if (f_zero_p(xr)) {
1189 zi = rb_num_pow(zi, other);
1190 if (n & 2) zi = f_negate(zi);
1201 for (; q = n / 2, r = n % 2, r == 0; n = q) {
1202 VALUE tmp = f_sub(f_mul(xr, xr), f_mul(xi, xi));
1203 xi = f_mul(f_mul(TWO, xr), xi);
1206 comp_mul(zr, zi, xr, xi, &zr, &zi);
1209 return nucomp_s_new_internal(
CLASS_OF(self), zr, zi);
1212 if (k_numeric_p(other) && f_real_p(other)) {
1215 if (RB_BIGNUM_TYPE_P(other))
1216 rb_warn(
"in a**b, b may be too big");
1219 theta = f_arg(self);
1221 return f_complex_polar(
CLASS_OF(self), f_expt(r, other),
1222 f_mul(theta, other));
1241 get_dat2(self, other);
1243 return RBOOL(f_eqeq_p(adat->real, bdat->real) &&
1244 f_eqeq_p(adat->imag, bdat->imag));
1246 if (k_numeric_p(other) && f_real_p(other)) {
1249 return RBOOL(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
1251 return RBOOL(f_eqeq_p(other, self));
1255nucomp_real_p(
VALUE self)
1258 return f_zero_p(dat->imag);
1287 if (!k_numeric_p(other)) {
1290 if (!nucomp_real_p(self)) {
1294 if (nucomp_real_p(other)) {
1295 get_dat2(self, other);
1296 return rb_funcall(adat->real, idCmp, 1, bdat->real);
1301 if (f_real_p(other)) {
1302 return rb_funcall(dat->real, idCmp, 1, other);
1317 if (k_numeric_p(other) && f_real_p(other))
1320 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" can't be coerced into %"PRIsVALUE,
1346 if (f_zero_p(dat->real)) {
1347 VALUE a = f_abs(dat->imag);
1352 if (f_zero_p(dat->imag)) {
1353 VALUE a = f_abs(dat->real);
1358 return rb_math_hypot(dat->real, dat->imag);
1378nucomp_abs2(
VALUE self)
1381 return f_add(f_mul(dat->real, dat->real),
1382 f_mul(dat->imag, dat->imag));
1405 return rb_math_atan2(dat->imag, dat->real);
1428nucomp_rect(
VALUE self)
1452nucomp_polar(
VALUE self)
1470 return f_complex_new2(
CLASS_OF(self), dat->real, f_negate(dat->imag));
1480nucomp_real_p_m(
VALUE self)
1500nucomp_denominator(
VALUE self)
1503 return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag));
1527nucomp_numerator(
VALUE self)
1533 cd = nucomp_denominator(self);
1534 return f_complex_new2(
CLASS_OF(self),
1535 f_mul(f_numerator(dat->real),
1536 f_div(cd, f_denominator(dat->real))),
1537 f_mul(f_numerator(dat->imag),
1538 f_div(cd, f_denominator(dat->imag))));
1543rb_complex_hash(
VALUE self)
1549 n = rb_hash(dat->real);
1551 n = rb_hash(dat->imag);
1570nucomp_hash(
VALUE self)
1572 return ST2FIX(rb_complex_hash(self));
1580 get_dat2(self, other);
1584 f_eqeq_p(self, other));
1595 return !isnan(f) && signbit(f);
1597 return f_negative_p(x);
1601f_tpositive_p(
VALUE x)
1603 return !f_signbit(x);
1613 impos = f_tpositive_p(dat->imag);
1619 if (!
rb_isdigit(RSTRING_PTR(s)[RSTRING_LEN(s) - 1]))
1640nucomp_to_s(
VALUE self)
1659nucomp_inspect(
VALUE self)
1670#define FINITE_TYPE_P(v) (RB_INTEGER_TYPE_P(v) || RB_TYPE_P(v, T_RATIONAL))
1685rb_complex_finite_p(
VALUE self)
1689 return RBOOL(f_finite_p(dat->real) && f_finite_p(dat->imag));
1705rb_complex_infinite_p(
VALUE self)
1709 if (!f_infinite_p(dat->real) && !f_infinite_p(dat->imag)) {
1717nucomp_dumper(
VALUE self)
1728 RCOMPLEX_SET_REAL(dat,
rb_ivar_get(a, id_i_real));
1729 RCOMPLEX_SET_IMAG(dat,
rb_ivar_get(a, id_i_imag));
1737nucomp_marshal_dump(
VALUE self)
1753 rb_raise(rb_eArgError,
"marshaled complex must have an array whose length is 2 but %ld",
RARRAY_LEN(a));
1768 return nucomp_s_canonicalize_internal(
rb_cComplex, x, y);
1780 return rb_complex_new_polar(x, y);
1793rb_dbl_complex_new(
double real,
double imag)
1811nucomp_to_i(
VALUE self)
1815 if (!k_exact_zero_p(dat->imag)) {
1816 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Integer",
1819 return f_to_i(dat->real);
1835nucomp_to_f(
VALUE self)
1839 if (!k_exact_zero_p(dat->imag)) {
1840 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Float",
1843 return f_to_f(dat->real);
1863nucomp_to_r(
VALUE self)
1870 else if (!k_exact_zero_p(dat->imag)) {
1871 VALUE imag = rb_check_convert_type_with_id(dat->imag,
T_RATIONAL,
"Rational", idTo_r);
1872 if (
NIL_P(imag) || !k_exact_zero_p(imag)) {
1873 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Rational",
1877 return f_to_r(dat->real);
1913nucomp_rationalize(
int argc,
VALUE *argv,
VALUE self)
1919 if (!k_exact_zero_p(dat->imag)) {
1920 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Rational",
1923 return rb_funcallv(dat->real, id_rationalize, argc, argv);
1933nucomp_to_c(
VALUE self)
1945numeric_to_c(
VALUE self)
1953 return (c ==
'-' || c ==
'+');
1957read_sign(
const char **s,
1973 return isdigit((
unsigned char)c);
1977read_digits(
const char **s,
int strict,
1982 if (!isdecimal(**s))
1985 while (isdecimal(**s) || **s ==
'_') {
1988 if (strict)
return 0;
2003 }
while (**s ==
'_');
2010 return (c ==
'e' || c ==
'E');
2014read_num(
const char **s,
int strict,
2018 if (!read_digits(s, strict, b))
2026 if (!read_digits(s, strict, b)) {
2032 if (islettere(**s)) {
2037 if (!read_digits(s, strict, b)) {
2046read_den(
const char **s,
int strict,
2049 if (!read_digits(s, strict, b))
2055read_rat_nos(
const char **s,
int strict,
2058 if (!read_num(s, strict, b))
2064 if (!read_den(s, strict, b)) {
2073read_rat(
const char **s,
int strict,
2077 if (!read_rat_nos(s, strict, b))
2085 return (c ==
'i' || c ==
'I' ||
2086 c ==
'j' || c ==
'J');
2093 return rb_cstr_to_rat(s, 0);
2094 if (strpbrk(s,
".eE"))
2096 return rb_cstr_to_inum(s, 10, 0);
2100read_comp(
const char **s,
int strict,
2101 VALUE *ret,
char **b)
2109 sign = read_sign(s, b);
2111 if (isimagunit(**s)) {
2113 num =
INT2FIX((sign ==
'-') ? -1 : + 1);
2118 if (!read_rat_nos(s, strict, b)) {
2127 if (isimagunit(**s)) {
2138 st = read_rat(s, strict, b);
2140 if (strlen(bb) < 1 ||
2141 !isdecimal(*(bb + strlen(bb) - 1))) {
2146 *ret = rb_complex_new_polar(num, num2);
2155 sign = read_sign(s, b);
2156 if (isimagunit(**s))
2157 num2 =
INT2FIX((sign ==
'-') ? -1 : + 1);
2159 if (!read_rat_nos(s, strict, b)) {
2166 if (!isimagunit(**s)) {
2182skip_ws(
const char **s)
2184 while (isspace((
unsigned char)**s))
2189parse_comp(
const char *s,
int strict,
VALUE *num)
2195 buf =
ALLOCV_N(
char, tmp, strlen(s) + 1);
2199 if (!read_comp(&s, strict, num, &b)) {
2215string_to_c_strict(
VALUE self,
int raise)
2225 else if (!(s = rb_str_to_cstr(self))) {
2229 if (!parse_comp(s, TRUE, &num)) {
2230 if (!raise)
return Qnil;
2231 rb_raise(rb_eArgError,
"invalid value for convert(): %+"PRIsVALUE,
2394string_to_c(
VALUE self)
2400 (void)parse_comp(rb_str_fill_terminator(self, 1), FALSE, &num);
2406to_complex(
VALUE val)
2415 if (!raise)
return Qnil;
2420 a1 = string_to_c_strict(a1, raise);
2425 a2 = string_to_c_strict(a2, raise);
2433 if (k_exact_zero_p(dat->imag))
2442 if (k_exact_zero_p(dat->imag))
2448 if (UNDEF_P(a2) || (k_exact_zero_p(a2)))
2453 if (k_numeric_p(a1) && !f_real_p(a1))
2456 if (!k_numeric_p(a1)) {
2458 a1 = rb_protect(to_complex, a1, NULL);
2459 rb_set_errinfo(
Qnil);
2462 return to_complex(a1);
2466 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2467 (!f_real_p(a1) || !f_real_p(a2)))
2487 return nucomp_s_new(argc, argv2, klass);
2492nucomp_s_convert(
int argc,
VALUE *argv,
VALUE klass)
2500 return nucomp_convert(klass, a1, a2, TRUE);
2510numeric_abs2(
VALUE self)
2512 return f_mul(self, self);
2522numeric_arg(
VALUE self)
2524 if (f_positive_p(self))
2536numeric_rect(
VALUE self)
2548numeric_polar(
VALUE self)
2553 abs = rb_int_abs(self);
2554 arg = numeric_arg(self);
2557 abs = rb_float_abs(self);
2558 arg = float_arg(self);
2561 abs = rb_rational_abs(self);
2562 arg = numeric_arg(self);
2578float_arg(
VALUE self)
2582 if (f_tpositive_p(self))
2848 rb_vm_register_global_object(RFLOAT_0 =
DBL2NUM(0.0));
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
static int rb_isdigit(int c)
Our own locale-insensitive version of isdigit(3).
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define NUM2DBL
Old name of rb_num2dbl.
VALUE rb_complex_polar(VALUE x, VALUE y)
Old name of rb_complex_new_polar.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define ST2FIX
Old name of RB_ST2FIX.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define DBL2NUM
Old name of rb_float_new.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_cRational
Rational class.
VALUE rb_convert_type(VALUE val, int type, const char *name, const char *mid)
Converts an object into another type.
VALUE rb_cComplex
Complex class.
VALUE rb_mMath
Math module.
VALUE rb_cInteger
Module class.
double rb_str_to_dbl(VALUE str, int mode)
Identical to rb_cstr_to_dbl(), except it accepts a Ruby's string instead of C's.
VALUE rb_cNumeric
Numeric class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
double rb_cstr_to_dbl(const char *str, int mode)
Converts a textual representation of a real number into a numeric, which is the nearest value that th...
VALUE rb_mComparable
Comparable module.
VALUE rb_cFloat
Float class.
VALUE rb_String(VALUE val)
This is the logic behind Kernel#String.
VALUE rb_cString
String class.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#define RGENGC_WB_PROTECTED_COMPLEX
This is a compile-time flag to enable/disable write barrier for struct RComplex.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#define rb_complex_new2(x, y)
Just another name of rb_complex_new.
#define rb_complex_new1(x)
Shorthand of x+0i.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_bin(), except for return values.
VALUE rb_num_coerce_bin(VALUE lhs, VALUE rhs, ID op)
Coerced binary operation.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RTEST
This is an old name of RB_TEST.
Internal header for Complex.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool rb_integer_type_p(VALUE obj)
Queries if the object is an instance of rb_cInteger.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.