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/error.h"
24#include "internal/math.h"
25#include "internal/numeric.h"
26#include "internal/object.h"
27#include "internal/rational.h"
28#include "internal/string.h"
29#include "ruby_assert.h"
31#define ZERO INT2FIX(0)
35#define RFLOAT_0 DBL2NUM(0)
42static ID id_abs, id_arg,
43 id_denominator, id_numerator,
44 id_real_p, id_i_real, id_i_imag,
45 id_finite_p, id_infinite_p, id_rationalize,
49#define id_negate idUMinus
55#define PRESERVE_SIGNEDZERO
61 LIKELY(rb_method_basic_definition_p(
rb_cInteger, idPLUS))) {
66 return rb_int_plus(x, y);
69 LIKELY(rb_method_basic_definition_p(
rb_cFloat, idPLUS))) {
72 return rb_float_plus(x, y);
75 LIKELY(rb_method_basic_definition_p(
rb_cRational, idPLUS))) {
78 return rb_rational_plus(x, y);
98 return RTEST(rb_int_gt(x, y));
101 return RTEST(rb_float_gt(x, y));
103 int const cmp = rb_cmpint(rb_rational_cmp(x, y), x, y);
113 LIKELY(rb_method_basic_definition_p(
rb_cInteger, idMULT))) {
114 if (FIXNUM_ZERO_P(y))
118 if (x == ONE)
return y;
119 if (y == ONE)
return x;
120 return rb_int_mul(x, y);
123 LIKELY(rb_method_basic_definition_p(
rb_cFloat, idMULT))) {
124 if (y == ONE)
return x;
125 return rb_float_mul(x, y);
128 LIKELY(rb_method_basic_definition_p(
rb_cRational, idMULT))) {
129 if (y == ONE)
return x;
130 return rb_rational_mul(x, y);
132 else if (LIKELY(rb_method_basic_definition_p(
CLASS_OF(x), idMULT))) {
133 if (y == ONE)
return x;
141 if (FIXNUM_ZERO_P(y) &&
142 LIKELY(rb_method_basic_definition_p(
CLASS_OF(x), idMINUS))) {
152 return rb_int_abs(x);
155 return rb_float_abs(x);
158 return rb_rational_abs(x);
161 return rb_complex_abs(x);
173 return numeric_arg(x);
179 return numeric_arg(x);
182 return rb_complex_arg(x);
191 return RRATIONAL(x)->num;
194 return rb_float_numerator(x);
200f_denominator(
VALUE x)
203 return RRATIONAL(x)->den;
206 return rb_float_denominator(x);
215 return rb_int_uminus(x);
218 return rb_float_uminus(x);
221 return rb_rational_uminus(x);
224 return rb_complex_uminus(x);
229static bool nucomp_real_p(
VALUE self);
244 return nucomp_real_p(x);
253 return rb_str_to_inum(x, 10, 0);
279 return rb_int_fdiv(x, y);
281 return rb_float_div(x, y);
283 return rb_rational_fdiv(x, y);
285 return rb_funcallv(x, id_fdiv, 1, &y);
292 return rb_numeric_quo(x, y);
294 return rb_float_div(x, y);
296 return rb_numeric_quo(x, y);
298 return rb_funcallv(x, id_quo, 1, &y);
305 return INT_NEGATIVE_P(x);
309 return INT_NEGATIVE_P(RRATIONAL(x)->num);
310 return rb_num_negative_p(x);
313#define f_positive_p(x) (!f_negative_p(x))
316always_finite_type_p(
VALUE x)
326 if (always_finite_type_p(x)) {
332 return RTEST(rb_funcallv(x, id_finite_p, 0, 0));
338 if (always_finite_type_p(x)) {
344 return RTEST(rb_funcallv(x, id_infinite_p, 0, 0));
359#define k_exact_p(x) (!RB_FLOAT_TYPE_P(x))
361#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
364 struct RComplex *dat = RCOMPLEX(x)
366#define get_dat2(x,y) \
367 struct RComplex *adat = RCOMPLEX(x), *bdat = RCOMPLEX(y)
372 NEWOBJ_OF(obj,
struct RComplex, klass,
375 RCOMPLEX_SET_REAL(obj, real);
376 RCOMPLEX_SET_IMAG(obj, imag);
383nucomp_s_alloc(
VALUE klass)
385 return nucomp_s_new_internal(klass, ZERO, ZERO);
392 return nucomp_s_new_internal(klass, x, ZERO);
400 return nucomp_s_new_internal(klass, x, y);
403WARN_UNUSED_RESULT(
inline static VALUE nucomp_real_check(
VALUE num));
405nucomp_real_check(
VALUE num)
411 VALUE real = RCOMPLEX(num)->real;
415 if (!k_numeric_p(num) || !f_real_p(num))
424 int complex_r, complex_i;
427 if (!complex_r && !complex_i) {
428 return nucomp_s_new_internal(klass, real, imag);
430 else if (!complex_r) {
433 return nucomp_s_new_internal(klass,
434 f_sub(real, dat->imag),
435 f_add(ZERO, dat->real));
437 else if (!complex_i) {
440 return nucomp_s_new_internal(klass,
442 f_add(dat->imag, imag));
445 get_dat2(real, imag);
447 return nucomp_s_new_internal(klass,
448 f_sub(adat->real, bdat->imag),
449 f_add(adat->imag, bdat->real));
470nucomp_s_new(
int argc,
VALUE *argv,
VALUE klass)
476 real = nucomp_real_check(real);
480 real = nucomp_real_check(real);
481 imag = nucomp_real_check(imag);
485 return nucomp_s_new_internal(klass, real, imag);
494 y = f_add(dat->imag, y);
496 return nucomp_s_canonicalize_internal(klass, x, y);
554nucomp_f_complex(
int argc,
VALUE *argv,
VALUE klass)
559 if (
rb_scan_args(argc, argv,
"11:", &a1, &a2, &opts) == 1) {
563 raise = rb_opts_exception_p(opts, raise);
573m_##n##_bang(VALUE x)\
575 return rb_math_##n(x);\
585 return rb_math_log(1, &x);
595 return m_cos_bang(x);
599 f_mul(m_cos_bang(dat->real),
600 m_cosh_bang(dat->imag)),
601 f_mul(f_negate(m_sin_bang(dat->real)),
602 m_sinh_bang(dat->imag)));
610 return m_sin_bang(x);
614 f_mul(m_sin_bang(dat->real),
615 m_cosh_bang(dat->imag)),
616 f_mul(m_cos_bang(dat->real),
617 m_sinh_bang(dat->imag)));
624 if (f_zero_p(x) || f_zero_p(y)) {
625 return nucomp_s_new_internal(klass, x, RFLOAT_0);
633 else if (arg == M_PI_2) {
637 else if (arg == M_PI_2+M_PI) {
643 const double real = abs * cos(arg), imag = abs * sin(arg);
648 const double ax = sin(arg), ay = cos(arg);
652 return nucomp_s_new_internal(klass, x, y);
654 return nucomp_s_canonicalize_internal(klass,
662 x = nucomp_real_check(x);
663 y = nucomp_real_check(y);
664 return f_complex_polar_real(klass, x, y);
668# define cospi(x) __cospi(x)
670# define cospi(x) cos((x) * M_PI)
673# define sinpi(x) __sinpi(x)
675# define sinpi(x) sin((x) * M_PI)
679rb_dbl_complex_new_polar_pi(
double abs,
double ang)
682 const double fr = modf(ang, &fi);
683 int pos = fr == +0.5;
685 if (pos || fr == -0.5) {
686 if ((modf(fi / 2.0, &fi) != fr) ^ pos) abs = -abs;
687 return rb_complex_new(RFLOAT_0,
DBL2NUM(abs));
689 else if (fr == 0.0) {
690 if (modf(fi / 2.0, &fi) != 0.0) abs = -abs;
694 const double real = abs * cospi(ang), imag = abs * sinpi(ang);
716nucomp_s_polar(
int argc,
VALUE *argv,
VALUE klass)
721 abs = nucomp_real_check(abs);
723 arg = nucomp_real_check(arg);
728 return f_complex_polar_real(klass, abs, arg);
791 return f_complex_new2(
CLASS_OF(self),
792 f_negate(dat->real), f_negate(dat->imag));
823 get_dat2(self, other);
825 real = f_add(adat->real, bdat->real);
826 imag = f_add(adat->imag, bdat->imag);
828 return f_complex_new2(
CLASS_OF(self), real, imag);
830 if (k_numeric_p(other) && f_real_p(other)) {
833 return f_complex_new2(
CLASS_OF(self),
834 f_add(dat->real, other), dat->imag);
858 get_dat2(self, other);
860 real = f_sub(adat->real, bdat->real);
861 imag = f_sub(adat->imag, bdat->imag);
863 return f_complex_new2(
CLASS_OF(self), real, imag);
865 if (k_numeric_p(other) && f_real_p(other)) {
868 return f_complex_new2(
CLASS_OF(self),
869 f_sub(dat->real, other), dat->imag);
890 bool arzero = f_zero_p(areal);
891 bool aizero = f_zero_p(aimag);
892 bool brzero = f_zero_p(breal);
893 bool bizero = f_zero_p(bimag);
894 *real = f_sub(safe_mul(areal, breal, arzero, brzero),
895 safe_mul(aimag, bimag, aizero, bizero));
896 *imag = f_add(safe_mul(areal, bimag, arzero, bizero),
897 safe_mul(aimag, breal, aizero, brzero));
919 get_dat2(self, other);
921 comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);
923 return f_complex_new2(
CLASS_OF(self), real, imag);
925 if (k_numeric_p(other) && f_real_p(other)) {
928 return f_complex_new2(
CLASS_OF(self),
929 f_mul(dat->real, other),
930 f_mul(dat->imag, other));
942 get_dat2(self, other);
947 if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
948 r = (*func)(bdat->imag, bdat->real);
949 n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
950 x = (*func)(f_add(adat->real, f_mul(adat->imag, r)), n);
951 y = (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n);
954 r = (*func)(bdat->real, bdat->imag);
955 n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
956 x = (*func)(f_add(f_mul(adat->real, r), adat->imag), n);
957 y = (*func)(f_sub(f_mul(adat->imag, r), adat->real), n);
960 x = rb_rational_canonicalize(x);
961 y = rb_rational_canonicalize(y);
963 return f_complex_new2(
CLASS_OF(self), x, y);
965 if (k_numeric_p(other) && f_real_p(other)) {
968 x = rb_rational_canonicalize((*func)(dat->real, other));
969 y = rb_rational_canonicalize((*func)(dat->imag, other));
970 return f_complex_new2(
CLASS_OF(self), x, y);
975#define rb_raise_zerodiv() rb_raise(rb_eZeroDivError, "divided by 0")
993 return f_divide(self, other, f_quo, id_quo);
996#define nucomp_quo rb_complex_div
1010 return f_divide(self, other, f_fdiv, id_fdiv);
1014f_reciprocal(
VALUE x)
1016 return f_quo(ONE, x);
1031complex_pow_for_special_angle(
VALUE self,
VALUE other)
1040 if (f_zero_p(dat->imag)) {
1044 else if (f_zero_p(dat->real)) {
1048 else if (f_eqeq_p(dat->real, dat->imag)) {
1052 else if (f_eqeq_p(dat->real, f_negate(dat->imag))) {
1060 if (UNDEF_P(x))
return x;
1062 if (f_negative_p(x)) {
1069 zx = rb_num_pow(x, other);
1074 rb_int_div(other, TWO)
1076 if (rb_int_odd_p(other)) {
1080 static const int dirs[][2] = {
1081 {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}
1086 switch (dirs[z_dir][0]) {
1087 case 0: zr = zero_for(zx);
break;
1088 case 1: zr = zx;
break;
1089 case -1: zr = f_negate(zx);
break;
1091 switch (dirs[z_dir][1]) {
1092 case 0: zi = zero_for(zx);
break;
1093 case 1: zi = zx;
break;
1094 case -1: zi = f_negate(zx);
break;
1096 return nucomp_s_new_internal(
CLASS_OF(self), zr, zi);
1113 if (k_numeric_p(other) && k_exact_zero_p(other))
1114 return f_complex_new_bang1(
CLASS_OF(self), ONE);
1117 other = RRATIONAL(other)->num;
1122 if (k_exact_zero_p(dat->imag))
1128 return nucomp_s_new_internal(
CLASS_OF(self), dat->real, dat->imag);
1131 VALUE result = complex_pow_for_special_angle(self, other);
1132 if (!UNDEF_P(result))
return result;
1135 VALUE r, theta, nr, ntheta;
1140 theta = f_arg(self);
1142 nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
1143 f_mul(dat->imag, theta)));
1144 ntheta = f_add(f_mul(theta, dat->real),
1145 f_mul(dat->imag, m_log_bang(r)));
1146 return f_complex_polar(
CLASS_OF(self), nr, ntheta);
1151 return nucomp_s_new_internal(
CLASS_OF(self), ONE, ZERO);
1154 self = f_reciprocal(self);
1155 other = rb_int_uminus(other);
1160 VALUE xr = dat->real, xi = dat->imag, zr = xr, zi = xi;
1163 zr = rb_num_pow(zr, other);
1165 else if (f_zero_p(xr)) {
1166 zi = rb_num_pow(zi, other);
1167 if (n & 2) zi = f_negate(zi);
1178 for (; q = n / 2, r = n % 2, r == 0; n = q) {
1179 VALUE tmp = f_sub(f_mul(xr, xr), f_mul(xi, xi));
1180 xi = f_mul(f_mul(TWO, xr), xi);
1183 comp_mul(zr, zi, xr, xi, &zr, &zi);
1186 return nucomp_s_new_internal(
CLASS_OF(self), zr, zi);
1189 if (k_numeric_p(other) && f_real_p(other)) {
1192 if (RB_BIGNUM_TYPE_P(other))
1193 rb_warn(
"in a**b, b may be too big");
1195 r = rb_num_pow(f_abs(self), other);
1196 theta = f_mul(f_arg(self), other);
1198 return f_complex_polar(
CLASS_OF(self), r, theta);
1217 get_dat2(self, other);
1219 return RBOOL(f_eqeq_p(adat->real, bdat->real) &&
1220 f_eqeq_p(adat->imag, bdat->imag));
1222 if (k_numeric_p(other) && f_real_p(other)) {
1225 return RBOOL(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
1227 return RBOOL(f_eqeq_p(other, self));
1231nucomp_real_p(
VALUE self)
1234 return f_zero_p(dat->imag);
1267 if (!k_numeric_p(other)) {
1270 if (!nucomp_real_p(self)) {
1274 if (nucomp_real_p(other)) {
1275 get_dat2(self, other);
1276 return rb_funcall(adat->real, idCmp, 1, bdat->real);
1281 if (f_real_p(other)) {
1282 return rb_funcall(dat->real, idCmp, 1, other);
1297 if (k_numeric_p(other) && f_real_p(other))
1300 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" can't be coerced into %"PRIsVALUE,
1326 if (f_zero_p(dat->real)) {
1327 VALUE a = f_abs(dat->imag);
1332 if (f_zero_p(dat->imag)) {
1333 VALUE a = f_abs(dat->real);
1338 return rb_math_hypot(dat->real, dat->imag);
1358nucomp_abs2(
VALUE self)
1361 return f_add(f_mul(dat->real, dat->real),
1362 f_mul(dat->imag, dat->imag));
1385 return rb_math_atan2(dat->imag, dat->real);
1408nucomp_rect(
VALUE self)
1432nucomp_polar(
VALUE self)
1450 return f_complex_new2(
CLASS_OF(self), dat->real, f_negate(dat->imag));
1460nucomp_real_p_m(
VALUE self)
1480nucomp_denominator(
VALUE self)
1483 return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag));
1507nucomp_numerator(
VALUE self)
1513 cd = nucomp_denominator(self);
1514 return f_complex_new2(
CLASS_OF(self),
1515 f_mul(f_numerator(dat->real),
1516 f_div(cd, f_denominator(dat->real))),
1517 f_mul(f_numerator(dat->imag),
1518 f_div(cd, f_denominator(dat->imag))));
1523rb_complex_hash(
VALUE self)
1529 n = rb_hash(dat->real);
1531 n = rb_hash(dat->imag);
1550nucomp_hash(
VALUE self)
1552 return ST2FIX(rb_complex_hash(self));
1560 get_dat2(self, other);
1564 f_eqeq_p(self, other));
1575 return !isnan(f) && signbit(f);
1577 return f_negative_p(x);
1581f_tpositive_p(
VALUE x)
1583 return !f_signbit(x);
1593 impos = f_tpositive_p(dat->imag);
1599 if (!
rb_isdigit(RSTRING_PTR(s)[RSTRING_LEN(s) - 1]))
1620nucomp_to_s(
VALUE self)
1639nucomp_inspect(
VALUE self)
1650#define FINITE_TYPE_P(v) (RB_INTEGER_TYPE_P(v) || RB_TYPE_P(v, T_RATIONAL))
1665rb_complex_finite_p(
VALUE self)
1669 return RBOOL(f_finite_p(dat->real) && f_finite_p(dat->imag));
1685rb_complex_infinite_p(
VALUE self)
1689 if (!f_infinite_p(dat->real) && !f_infinite_p(dat->imag)) {
1697nucomp_dumper(
VALUE self)
1708 RCOMPLEX_SET_REAL(dat,
rb_ivar_get(a, id_i_real));
1709 RCOMPLEX_SET_IMAG(dat,
rb_ivar_get(a, id_i_imag));
1717nucomp_marshal_dump(
VALUE self)
1733 rb_raise(rb_eArgError,
"marshaled complex must have an array whose length is 2 but %ld",
RARRAY_LEN(a));
1748 return nucomp_s_canonicalize_internal(
rb_cComplex, x, y);
1767rb_dbl_complex_new(
double real,
double imag)
1785nucomp_to_i(
VALUE self)
1789 if (!k_exact_zero_p(dat->imag)) {
1790 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Integer",
1793 return f_to_i(dat->real);
1809nucomp_to_f(
VALUE self)
1813 if (!k_exact_zero_p(dat->imag)) {
1814 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Float",
1817 return f_to_f(dat->real);
1837nucomp_to_r(
VALUE self)
1844 else if (!k_exact_zero_p(dat->imag)) {
1845 VALUE imag = rb_check_convert_type_with_id(dat->imag,
T_RATIONAL,
"Rational", idTo_r);
1846 if (
NIL_P(imag) || !k_exact_zero_p(imag)) {
1847 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Rational",
1851 return rb_funcallv(dat->real, id_to_r, 0, 0);
1887nucomp_rationalize(
int argc,
VALUE *argv,
VALUE self)
1893 if (!k_exact_zero_p(dat->imag)) {
1894 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Rational",
1897 return rb_funcallv(dat->real, id_rationalize, argc, argv);
1907nucomp_to_c(
VALUE self)
1919numeric_to_c(
VALUE self)
1927 return (c ==
'-' || c ==
'+');
1931read_sign(
const char **s,
1947 return isdigit((
unsigned char)c);
1951read_digits(
const char **s,
int strict,
1956 if (!isdecimal(**s))
1959 while (isdecimal(**s) || **s ==
'_') {
1962 if (strict)
return 0;
1977 }
while (**s ==
'_');
1984 return (c ==
'e' || c ==
'E');
1988read_num(
const char **s,
int strict,
1992 if (!read_digits(s, strict, b))
2000 if (!read_digits(s, strict, b)) {
2006 if (islettere(**s)) {
2011 if (!read_digits(s, strict, b)) {
2020read_den(
const char **s,
int strict,
2023 if (!read_digits(s, strict, b))
2029read_rat_nos(
const char **s,
int strict,
2032 if (!read_num(s, strict, b))
2038 if (!read_den(s, strict, b)) {
2047read_rat(
const char **s,
int strict,
2051 if (!read_rat_nos(s, strict, b))
2059 return (c ==
'i' || c ==
'I' ||
2060 c ==
'j' || c ==
'J');
2067 return rb_cstr_to_rat(s, 0);
2068 if (strpbrk(s,
".eE"))
2070 return rb_cstr_to_inum(s, 10, 0);
2074read_comp(
const char **s,
int strict,
2075 VALUE *ret,
char **b)
2083 sign = read_sign(s, b);
2085 if (isimagunit(**s)) {
2087 num =
INT2FIX((sign ==
'-') ? -1 : + 1);
2092 if (!read_rat_nos(s, strict, b)) {
2101 if (isimagunit(**s)) {
2112 st = read_rat(s, strict, b);
2114 if (strlen(bb) < 1 ||
2115 !isdecimal(*(bb + strlen(bb) - 1))) {
2120 *ret = rb_complex_new_polar(num, num2);
2129 sign = read_sign(s, b);
2130 if (isimagunit(**s))
2131 num2 =
INT2FIX((sign ==
'-') ? -1 : + 1);
2133 if (!read_rat_nos(s, strict, b)) {
2140 if (!isimagunit(**s)) {
2156skip_ws(
const char **s)
2158 while (isspace((
unsigned char)**s))
2163parse_comp(
const char *s,
int strict,
VALUE *num)
2169 buf =
ALLOCV_N(
char, tmp, strlen(s) + 1);
2173 if (!read_comp(&s, strict, num, &b)) {
2189string_to_c_strict(
VALUE self,
int raise)
2199 else if (!(s = rb_str_to_cstr(self))) {
2203 if (!parse_comp(s, TRUE, &num)) {
2204 if (!raise)
return Qnil;
2205 rb_raise(rb_eArgError,
"invalid value for convert(): %+"PRIsVALUE,
2368string_to_c(
VALUE self)
2374 (void)parse_comp(rb_str_fill_terminator(self, 1), FALSE, &num);
2380to_complex(
VALUE val)
2389 if (!raise)
return Qnil;
2390 rb_cant_convert(
Qnil,
"Complex");
2394 a1 = string_to_c_strict(a1, raise);
2399 a2 = string_to_c_strict(a2, raise);
2407 if (k_exact_zero_p(dat->imag))
2416 if (k_exact_zero_p(dat->imag))
2422 if (UNDEF_P(a2) || (k_exact_zero_p(a2)))
2427 if (k_numeric_p(a1) && !f_real_p(a1))
2430 if (!k_numeric_p(a1)) {
2432 a1 = rb_protect(to_complex, a1, NULL);
2433 rb_set_errinfo(
Qnil);
2436 return to_complex(a1);
2440 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2441 (!f_real_p(a1) || !f_real_p(a2)))
2461 return nucomp_s_new(argc, argv2, klass);
2466nucomp_s_convert(
int argc,
VALUE *argv,
VALUE klass)
2474 return nucomp_convert(klass, a1, a2, TRUE);
2484numeric_abs2(
VALUE self)
2486 return f_mul(self, self);
2496numeric_arg(
VALUE self)
2498 if (f_positive_p(self))
2510numeric_rect(
VALUE self)
2522numeric_polar(
VALUE self)
2527 abs = rb_int_abs(self);
2528 arg = numeric_arg(self);
2531 abs = rb_float_abs(self);
2532 arg = float_arg(self);
2535 abs = rb_rational_abs(self);
2536 arg = numeric_arg(self);
2552float_arg(
VALUE self)
2556 if (f_tpositive_p(self))
2822 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.
#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_cObject
Object 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.