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));
837 get_dat2(self, other);
839 real = f_add(adat->real, bdat->real);
840 imag = f_add(adat->imag, bdat->imag);
842 return f_complex_new2(
CLASS_OF(self), real, imag);
844 if (k_numeric_p(other) && f_real_p(other)) {
847 return f_complex_new2(
CLASS_OF(self),
848 f_add(dat->real, other), dat->imag);
872 get_dat2(self, other);
874 real = f_sub(adat->real, bdat->real);
875 imag = f_sub(adat->imag, bdat->imag);
877 return f_complex_new2(
CLASS_OF(self), real, imag);
879 if (k_numeric_p(other) && f_real_p(other)) {
882 return f_complex_new2(
CLASS_OF(self),
883 f_sub(dat->real, other), dat->imag);
904 bool arzero = f_zero_p(areal);
905 bool aizero = f_zero_p(aimag);
906 bool brzero = f_zero_p(breal);
907 bool bizero = f_zero_p(bimag);
908 *real = f_sub(safe_mul(areal, breal, arzero, brzero),
909 safe_mul(aimag, bimag, aizero, bizero));
910 *imag = f_add(safe_mul(areal, bimag, arzero, bizero),
911 safe_mul(aimag, breal, aizero, brzero));
932 get_dat2(self, other);
934 comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);
936 return f_complex_new2(
CLASS_OF(self), real, imag);
938 if (k_numeric_p(other) && f_real_p(other)) {
941 return f_complex_new2(
CLASS_OF(self),
942 f_mul(dat->real, other),
943 f_mul(dat->imag, other));
955 get_dat2(self, other);
960 if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
961 r = (*func)(bdat->imag, bdat->real);
962 n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
963 x = (*func)(f_add(adat->real, f_mul(adat->imag, r)), n);
964 y = (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n);
967 r = (*func)(bdat->real, bdat->imag);
968 n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
969 x = (*func)(f_add(f_mul(adat->real, r), adat->imag), n);
970 y = (*func)(f_sub(f_mul(adat->imag, r), adat->real), n);
973 x = rb_rational_canonicalize(x);
974 y = rb_rational_canonicalize(y);
976 return f_complex_new2(
CLASS_OF(self), x, y);
978 if (k_numeric_p(other) && f_real_p(other)) {
981 x = rb_rational_canonicalize((*func)(dat->real, other));
982 y = rb_rational_canonicalize((*func)(dat->imag, other));
983 return f_complex_new2(
CLASS_OF(self), x, y);
988#define rb_raise_zerodiv() rb_raise(rb_eZeroDivError, "divided by 0")
1006 return f_divide(self, other, f_quo, id_quo);
1009#define nucomp_quo rb_complex_div
1023 return f_divide(self, other, f_fdiv, id_fdiv);
1027f_reciprocal(
VALUE x)
1029 return f_quo(ONE, x);
1044complex_pow_for_special_angle(
VALUE self,
VALUE other)
1053 if (f_zero_p(dat->imag)) {
1057 else if (f_zero_p(dat->real)) {
1061 else if (f_eqeq_p(dat->real, dat->imag)) {
1065 else if (f_eqeq_p(dat->real, f_negate(dat->imag))) {
1072 if (UNDEF_P(x))
return x;
1074 if (f_negative_p(x)) {
1081 zx = rb_num_pow(x, other);
1086 rb_int_div(other, TWO)
1088 if (rb_int_odd_p(other)) {
1092 static const int dirs[][2] = {
1093 {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}
1098 switch (dirs[z_dir][0]) {
1099 case 0: zr = zero_for(zx);
break;
1100 case 1: zr = zx;
break;
1101 case -1: zr = f_negate(zx);
break;
1103 switch (dirs[z_dir][1]) {
1104 case 0: zi = zero_for(zx);
break;
1105 case 1: zi = zx;
break;
1106 case -1: zi = f_negate(zx);
break;
1108 return nucomp_s_new_internal(
CLASS_OF(self), zr, zi);
1125 if (k_numeric_p(other) && k_exact_zero_p(other))
1126 return f_complex_new_bang1(
CLASS_OF(self), ONE);
1129 other = RRATIONAL(other)->num;
1134 if (k_exact_zero_p(dat->imag))
1140 return nucomp_s_new_internal(
CLASS_OF(self), dat->real, dat->imag);
1143 VALUE result = complex_pow_for_special_angle(self, other);
1144 if (!UNDEF_P(result))
return result;
1147 VALUE r, theta, nr, ntheta;
1152 theta = f_arg(self);
1154 nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
1155 f_mul(dat->imag, theta)));
1156 ntheta = f_add(f_mul(theta, dat->real),
1157 f_mul(dat->imag, m_log_bang(r)));
1158 return f_complex_polar(
CLASS_OF(self), nr, ntheta);
1163 return nucomp_s_new_internal(
CLASS_OF(self), ONE, ZERO);
1166 self = f_reciprocal(self);
1167 other = rb_int_uminus(other);
1172 VALUE xr = dat->real, xi = dat->imag, zr = xr, zi = xi;
1175 zr = rb_num_pow(zr, other);
1177 else if (f_zero_p(xr)) {
1178 zi = rb_num_pow(zi, other);
1179 if (n & 2) zi = f_negate(zi);
1190 for (; q = n / 2, r = n % 2, r == 0; n = q) {
1191 VALUE tmp = f_sub(f_mul(xr, xr), f_mul(xi, xi));
1192 xi = f_mul(f_mul(TWO, xr), xi);
1195 comp_mul(zr, zi, xr, xi, &zr, &zi);
1198 return nucomp_s_new_internal(
CLASS_OF(self), zr, zi);
1201 if (k_numeric_p(other) && f_real_p(other)) {
1204 if (RB_BIGNUM_TYPE_P(other))
1205 rb_warn(
"in a**b, b may be too big");
1208 theta = f_arg(self);
1210 return f_complex_polar(
CLASS_OF(self), f_expt(r, other),
1211 f_mul(theta, other));
1230 get_dat2(self, other);
1232 return RBOOL(f_eqeq_p(adat->real, bdat->real) &&
1233 f_eqeq_p(adat->imag, bdat->imag));
1235 if (k_numeric_p(other) && f_real_p(other)) {
1238 return RBOOL(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
1240 return RBOOL(f_eqeq_p(other, self));
1244nucomp_real_p(
VALUE self)
1247 return f_zero_p(dat->imag);
1276 if (!k_numeric_p(other)) {
1279 if (!nucomp_real_p(self)) {
1283 if (nucomp_real_p(other)) {
1284 get_dat2(self, other);
1285 return rb_funcall(adat->real, idCmp, 1, bdat->real);
1290 if (f_real_p(other)) {
1291 return rb_funcall(dat->real, idCmp, 1, other);
1305 return rb_assoc_new(other, self);
1306 if (k_numeric_p(other) && f_real_p(other))
1307 return rb_assoc_new(f_complex_new_bang1(
CLASS_OF(self), other), self);
1309 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" can't be coerced into %"PRIsVALUE,
1335 if (f_zero_p(dat->real)) {
1336 VALUE a = f_abs(dat->imag);
1341 if (f_zero_p(dat->imag)) {
1342 VALUE a = f_abs(dat->real);
1347 return rb_math_hypot(dat->real, dat->imag);
1367nucomp_abs2(
VALUE self)
1370 return f_add(f_mul(dat->real, dat->real),
1371 f_mul(dat->imag, dat->imag));
1394 return rb_math_atan2(dat->imag, dat->real);
1417nucomp_rect(
VALUE self)
1420 return rb_assoc_new(dat->real, dat->imag);
1441nucomp_polar(
VALUE self)
1443 return rb_assoc_new(f_abs(self), f_arg(self));
1459 return f_complex_new2(
CLASS_OF(self), dat->real, f_negate(dat->imag));
1469nucomp_real_p_m(
VALUE self)
1489nucomp_denominator(
VALUE self)
1492 return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag));
1516nucomp_numerator(
VALUE self)
1522 cd = nucomp_denominator(self);
1523 return f_complex_new2(
CLASS_OF(self),
1524 f_mul(f_numerator(dat->real),
1525 f_div(cd, f_denominator(dat->real))),
1526 f_mul(f_numerator(dat->imag),
1527 f_div(cd, f_denominator(dat->imag))));
1532rb_complex_hash(
VALUE self)
1538 n = rb_hash(dat->real);
1540 n = rb_hash(dat->imag);
1559nucomp_hash(
VALUE self)
1561 return ST2FIX(rb_complex_hash(self));
1569 get_dat2(self, other);
1573 f_eqeq_p(self, other));
1584 return !isnan(f) && signbit(f);
1586 return f_negative_p(x);
1590f_tpositive_p(
VALUE x)
1592 return !f_signbit(x);
1602 impos = f_tpositive_p(dat->imag);
1608 if (!
rb_isdigit(RSTRING_PTR(s)[RSTRING_LEN(s) - 1]))
1629nucomp_to_s(
VALUE self)
1648nucomp_inspect(
VALUE self)
1659#define FINITE_TYPE_P(v) (RB_INTEGER_TYPE_P(v) || RB_TYPE_P(v, T_RATIONAL))
1674rb_complex_finite_p(
VALUE self)
1678 return RBOOL(f_finite_p(dat->real) && f_finite_p(dat->imag));
1694rb_complex_infinite_p(
VALUE self)
1698 if (!f_infinite_p(dat->real) && !f_infinite_p(dat->imag)) {
1706nucomp_dumper(
VALUE self)
1717 RCOMPLEX_SET_REAL(dat,
rb_ivar_get(a, id_i_real));
1718 RCOMPLEX_SET_IMAG(dat,
rb_ivar_get(a, id_i_imag));
1726nucomp_marshal_dump(
VALUE self)
1731 a = rb_assoc_new(dat->real, dat->imag);
1742 rb_raise(rb_eArgError,
"marshaled complex must have an array whose length is 2 but %ld",
RARRAY_LEN(a));
1757 return nucomp_s_canonicalize_internal(
rb_cComplex, x, y);
1769 return rb_complex_new_polar(x, y);
1782rb_dbl_complex_new(
double real,
double imag)
1800nucomp_to_i(
VALUE self)
1804 if (!k_exact_zero_p(dat->imag)) {
1805 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Integer",
1808 return f_to_i(dat->real);
1824nucomp_to_f(
VALUE self)
1828 if (!k_exact_zero_p(dat->imag)) {
1829 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Float",
1832 return f_to_f(dat->real);
1852nucomp_to_r(
VALUE self)
1859 else if (!k_exact_zero_p(dat->imag)) {
1860 VALUE imag = rb_check_convert_type_with_id(dat->imag,
T_RATIONAL,
"Rational", idTo_r);
1861 if (
NIL_P(imag) || !k_exact_zero_p(imag)) {
1862 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Rational",
1866 return f_to_r(dat->real);
1902nucomp_rationalize(
int argc,
VALUE *argv,
VALUE self)
1908 if (!k_exact_zero_p(dat->imag)) {
1909 rb_raise(
rb_eRangeError,
"can't convert %"PRIsVALUE
" into Rational",
1912 return rb_funcallv(dat->real, id_rationalize, argc, argv);
1922nucomp_to_c(
VALUE self)
1937nilclass_to_c(
VALUE self)
1949numeric_to_c(
VALUE self)
1957 return (c ==
'-' || c ==
'+');
1961read_sign(
const char **s,
1977 return isdigit((
unsigned char)c);
1981read_digits(
const char **s,
int strict,
1986 if (!isdecimal(**s))
1989 while (isdecimal(**s) || **s ==
'_') {
1992 if (strict)
return 0;
2007 }
while (**s ==
'_');
2014 return (c ==
'e' || c ==
'E');
2018read_num(
const char **s,
int strict,
2022 if (!read_digits(s, strict, b))
2030 if (!read_digits(s, strict, b)) {
2036 if (islettere(**s)) {
2041 if (!read_digits(s, strict, b)) {
2050read_den(
const char **s,
int strict,
2053 if (!read_digits(s, strict, b))
2059read_rat_nos(
const char **s,
int strict,
2062 if (!read_num(s, strict, b))
2068 if (!read_den(s, strict, b)) {
2077read_rat(
const char **s,
int strict,
2081 if (!read_rat_nos(s, strict, b))
2089 return (c ==
'i' || c ==
'I' ||
2090 c ==
'j' || c ==
'J');
2097 return rb_cstr_to_rat(s, 0);
2098 if (strpbrk(s,
".eE"))
2100 return rb_cstr_to_inum(s, 10, 0);
2104read_comp(
const char **s,
int strict,
2105 VALUE *ret,
char **b)
2113 sign = read_sign(s, b);
2115 if (isimagunit(**s)) {
2117 num =
INT2FIX((sign ==
'-') ? -1 : + 1);
2122 if (!read_rat_nos(s, strict, b)) {
2131 if (isimagunit(**s)) {
2142 st = read_rat(s, strict, b);
2144 if (strlen(bb) < 1 ||
2145 !isdecimal(*(bb + strlen(bb) - 1))) {
2150 *ret = rb_complex_new_polar(num, num2);
2159 sign = read_sign(s, b);
2160 if (isimagunit(**s))
2161 num2 =
INT2FIX((sign ==
'-') ? -1 : + 1);
2163 if (!read_rat_nos(s, strict, b)) {
2170 if (!isimagunit(**s)) {
2186skip_ws(
const char **s)
2188 while (isspace((
unsigned char)**s))
2193parse_comp(
const char *s,
int strict,
VALUE *num)
2199 buf =
ALLOCV_N(
char, tmp, strlen(s) + 1);
2203 if (!read_comp(&s, strict, num, &b)) {
2219string_to_c_strict(
VALUE self,
int raise)
2229 else if (!(s = rb_str_to_cstr(self))) {
2233 if (!parse_comp(s, TRUE, &num)) {
2234 if (!raise)
return Qnil;
2235 rb_raise(rb_eArgError,
"invalid value for convert(): %+"PRIsVALUE,
2270string_to_c(
VALUE self)
2276 (void)parse_comp(rb_str_fill_terminator(self, 1), FALSE, &num);
2282to_complex(
VALUE val)
2291 if (!raise)
return Qnil;
2296 a1 = string_to_c_strict(a1, raise);
2301 a2 = string_to_c_strict(a2, raise);
2309 if (k_exact_zero_p(dat->imag))
2318 if (k_exact_zero_p(dat->imag))
2324 if (UNDEF_P(a2) || (k_exact_zero_p(a2)))
2329 if (k_numeric_p(a1) && !f_real_p(a1))
2332 if (!k_numeric_p(a1)) {
2334 a1 = rb_protect(to_complex, a1, NULL);
2335 rb_set_errinfo(
Qnil);
2338 return to_complex(a1);
2342 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2343 (!f_real_p(a1) || !f_real_p(a2)))
2363 return nucomp_s_new(argc, argv2, klass);
2368nucomp_s_convert(
int argc,
VALUE *argv,
VALUE klass)
2376 return nucomp_convert(klass, a1, a2, TRUE);
2386numeric_abs2(
VALUE self)
2388 return f_mul(self, self);
2398numeric_arg(
VALUE self)
2400 if (f_positive_p(self))
2412numeric_rect(
VALUE self)
2414 return rb_assoc_new(self,
INT2FIX(0));
2424numeric_polar(
VALUE self)
2429 abs = rb_int_abs(self);
2430 arg = numeric_arg(self);
2433 abs = rb_float_abs(self);
2434 arg = float_arg(self);
2437 abs = rb_rational_abs(self);
2438 arg = numeric_arg(self);
2444 return rb_assoc_new(abs, arg);
2454float_arg(
VALUE self)
2458 if (f_tpositive_p(self))
2725 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.
VALUE rb_cNilClass
NilClass 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.
#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.