Class: Complex
Overview
A complex number can be represented as a paired real number with imaginary unit; a+bi. Where a is real part, b is imaginary part and i is imaginary unit. Real a equals complex a+0i mathematically.
Complex object can be created as literal, and also by using Kernel#Complex, Complex::rect, Complex::polar or to_c method.
2+1i #=> (2+1i)
Complex(1) #=> (1+0i)
Complex(2, 3) #=> (2+3i)
Complex.polar(2, 3) #=> (-1.9799849932008908+0.2822400161197344i)
3.to_c #=> (3+0i)
You can also create complex object from floating-point numbers or strings.
Complex(0.3) #=> (0.3+0i)
Complex('0.3-0.5i') #=> (0.3-0.5i)
Complex('2/3+3/4i') #=> ((2/3)+(3/4)*i)
Complex('1@2') #=> (-0.4161468365471424+0.9092974268256817i)
0.3.to_c #=> (0.3+0i)
'0.3-0.5i'.to_c #=> (0.3-0.5i)
'2/3+3/4i'.to_c #=> ((2/3)+(3/4)*i)
'1@2'.to_c #=> (-0.4161468365471424+0.9092974268256817i)
A complex object is either an exact or an inexact number.
Complex(1, 1) / 2 #=> ((1/2)+(1/2)*i)
Complex(1, 1) / 2.0 #=> (0.5+0.5i)
Defined Under Namespace
Classes: compatible
Constant Summary collapse
- I =
The imaginary unit.
f_complex_new_bang2(rb_cComplex, ZERO, ONE)
Class Method Summary collapse
-
.polar(abs[, arg]) ⇒ Object
Returns a complex object which denotes the given polar form.
-
.rect(*args) ⇒ Object
Returns a complex object which denotes the given rectangular form.
-
.rectangular(*args) ⇒ Object
Returns a complex object which denotes the given rectangular form.
Instance Method Summary collapse
-
#*(numeric) ⇒ Object
Performs multiplication.
-
#**(numeric) ⇒ Object
Performs exponentiation.
-
#+(numeric) ⇒ Object
Performs addition.
-
#-(numeric) ⇒ Object
Performs subtraction.
-
#- ⇒ Object
Returns negation of the value.
-
#/(other) ⇒ Object
Performs division.
-
#<=>(object) ⇒ 0, ...
If
cmp
‘s imaginary part is zero, andobject
is also a real number (or a Complex number where the imaginary part is zero), compare the real part ofcmp
to object. -
#==(object) ⇒ Boolean
Returns true if cmp equals object numerically.
-
#abs ⇒ Object
Returns the absolute part of its polar form.
-
#abs2 ⇒ Object
Returns square of the absolute value.
-
#angle ⇒ Object
Returns the angle part of its polar form.
-
#arg ⇒ Object
Returns the angle part of its polar form.
-
#coerce(other) ⇒ Object
:nodoc:.
-
#conj ⇒ Object
Returns the complex conjugate.
-
#conjugate ⇒ Object
Returns the complex conjugate.
-
#denominator ⇒ Integer
Returns the denominator (lcm of both denominator - real and imag).
-
#eql?(other) ⇒ Boolean
:nodoc:.
-
#fdiv(numeric) ⇒ Object
Performs division as each part is a float, never returns a float.
-
#finite? ⇒ Boolean
Returns
true
ifcmp
‘s real and imaginary parts are both finite numbers, otherwise returnsfalse
. -
#hash ⇒ Object
:nodoc:.
-
#imag ⇒ Object
Returns the imaginary part.
-
#imaginary ⇒ Object
Returns the imaginary part.
-
#infinite? ⇒ nil, 1
Returns
1
ifcmp
‘s real or imaginary part is an infinite number, otherwise returnsnil
. -
#inspect ⇒ String
Returns the value as a string for inspection.
-
#magnitude ⇒ Object
Returns the absolute part of its polar form.
-
#marshal_dump ⇒ Object
private
:nodoc:.
-
#numerator ⇒ Numeric
Returns the numerator.
-
#phase ⇒ Object
Returns the angle part of its polar form.
-
#polar ⇒ Array
Returns an array; [cmp.abs, cmp.arg].
- #quo ⇒ Object
-
#rationalize([eps]) ⇒ Object
Returns the value as a rational if possible (the imaginary part should be exactly zero).
-
#real ⇒ Object
Returns the real part.
-
#real? ⇒ Object
Returns false, even if the complex number has no imaginary part.
-
#rect ⇒ Object
Returns an array; [cmp.real, cmp.imag].
-
#rectangular ⇒ Object
Returns an array; [cmp.real, cmp.imag].
-
#to_c ⇒ self
Returns self.
-
#to_f ⇒ Float
Returns the value as a float if possible (the imaginary part should be exactly zero).
-
#to_i ⇒ Integer
Returns the value as an integer if possible (the imaginary part should be exactly zero).
-
#to_r ⇒ Object
Returns the value as a rational if possible (the imaginary part should be exactly zero).
-
#to_s ⇒ String
Returns the value as a string.
Methods inherited from Numeric
#%, #+@, #ceil, #clone, #div, #divmod, #dup, #floor, #i, #integer?, #modulo, #negative?, #nonzero?, #positive?, #remainder, #round, #singleton_method_added, #step, #to_int, #truncate, #zero?
Methods included from Comparable
#<, #<=, #>, #>=, #between?, #clamp
Class Method Details
.polar(abs[, arg]) ⇒ Object
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 |
# File 'complex.c', line 698
static VALUE
nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
{
VALUE abs, arg;
switch (rb_scan_args(argc, argv, "11", &abs, &arg)) {
case 1:
nucomp_real_check(abs);
if (canonicalization) return abs;
return nucomp_s_new_internal(klass, abs, ZERO);
default:
nucomp_real_check(abs);
nucomp_real_check(arg);
break;
}
return f_complex_polar(klass, abs, arg);
}
|
.rect(real[, imag]) ⇒ Object .rectangular(real[, imag]) ⇒ Object
Returns a complex object which denotes the given rectangular form.
Complex.rectangular(1, 2) #=> (1+2i)
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'complex.c', line 489
static VALUE
nucomp_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE real, imag;
switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
case 1:
nucomp_real_check(real);
imag = ZERO;
break;
default:
nucomp_real_check(real);
nucomp_real_check(imag);
break;
}
return nucomp_s_canonicalize_internal(klass, real, imag);
}
|
.rect(real[, imag]) ⇒ Object .rectangular(real[, imag]) ⇒ Object
Returns a complex object which denotes the given rectangular form.
Complex.rectangular(1, 2) #=> (1+2i)
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'complex.c', line 489
static VALUE
nucomp_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE real, imag;
switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
case 1:
nucomp_real_check(real);
imag = ZERO;
break;
default:
nucomp_real_check(real);
nucomp_real_check(imag);
break;
}
return nucomp_s_canonicalize_internal(klass, real, imag);
}
|
Instance Method Details
#*(numeric) ⇒ Object
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 |
# File 'complex.c', line 871
VALUE
rb_complex_mul(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE real, imag;
get_dat2(self, other);
comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);
return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_mul(dat->real, other),
f_mul(dat->imag, other));
}
return rb_num_coerce_bin(self, other, '*');
}
|
#**(numeric) ⇒ Object
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'complex.c', line 984
VALUE
rb_complex_pow(VALUE self, VALUE other)
{
if (k_numeric_p(other) && k_exact_zero_p(other))
return f_complex_new_bang1(CLASS_OF(self), ONE);
if (RB_TYPE_P(other, T_RATIONAL) && RRATIONAL(other)->den == LONG2FIX(1))
other = RRATIONAL(other)->num; /* c14n */
if (RB_TYPE_P(other, T_COMPLEX)) {
get_dat1(other);
if (k_exact_zero_p(dat->imag))
other = dat->real; /* c14n */
}
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE r, theta, nr, ntheta;
get_dat1(other);
r = f_abs(self);
theta = f_arg(self);
nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
f_mul(dat->imag, theta)));
ntheta = f_add(f_mul(theta, dat->real),
f_mul(dat->imag, m_log_bang(r)));
return f_complex_polar(CLASS_OF(self), nr, ntheta);
}
if (FIXNUM_P(other)) {
long n = FIX2LONG(other);
if (n == 0) {
return nucomp_s_new_internal(CLASS_OF(self), ONE, ZERO);
}
if (n < 0) {
self = f_reciprocal(self);
other = rb_int_uminus(other);
n = -n;
}
{
get_dat1(self);
VALUE xr = dat->real, xi = dat->imag, zr = xr, zi = xi;
if (f_zero_p(xi)) {
zr = rb_num_pow(zr, other);
}
else if (f_zero_p(xr)) {
zi = rb_num_pow(zi, other);
if (n & 2) zi = f_negate(zi);
if (!(n & 1)) {
VALUE tmp = zr;
zr = zi;
zi = tmp;
}
}
else {
while (--n) {
long q, r;
for (; q = n / 2, r = n % 2, r == 0; n = q) {
VALUE tmp = f_sub(f_mul(xr, xr), f_mul(xi, xi));
xi = f_mul(f_mul(TWO, xr), xi);
xr = tmp;
}
comp_mul(zr, zi, xr, xi, &zr, &zi);
}
}
return nucomp_s_new_internal(CLASS_OF(self), zr, zi);
}
}
if (k_numeric_p(other) && f_real_p(other)) {
VALUE r, theta;
if (RB_TYPE_P(other, T_BIGNUM))
rb_warn("in a**b, b may be too big");
r = f_abs(self);
theta = f_arg(self);
return f_complex_polar(CLASS_OF(self), f_expt(r, other),
f_mul(theta, other));
}
return rb_num_coerce_bin(self, other, id_expt);
}
|
#+(numeric) ⇒ Object
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 |
# File 'complex.c', line 777
VALUE
rb_complex_plus(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE real, imag;
get_dat2(self, other);
real = f_add(adat->real, bdat->real);
imag = f_add(adat->imag, bdat->imag);
return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_add(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, '+');
}
|
#-(numeric) ⇒ Object
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'complex.c', line 811
VALUE
rb_complex_minus(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE real, imag;
get_dat2(self, other);
real = f_sub(adat->real, bdat->real);
imag = f_sub(adat->imag, bdat->imag);
return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_sub(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, '-');
}
|
#- ⇒ Object
Returns negation of the value.
-Complex(1, 2) #=> (-1-2i)
757 758 759 760 761 762 763 |
# File 'complex.c', line 757
VALUE
rb_complex_uminus(VALUE self)
{
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_negate(dat->real), f_negate(dat->imag));
}
|
#/(numeric) ⇒ Object #quo(numeric) ⇒ Object
947 948 949 950 951 |
# File 'complex.c', line 947
VALUE
rb_complex_div(VALUE self, VALUE other)
{
return f_divide(self, other, f_quo, id_quo);
}
|
#<=>(object) ⇒ 0, ...
If cmp
‘s imaginary part is zero, and object
is also a real number (or a Complex number where the imaginary part is zero), compare the real part of cmp
to object. Otherwise, return nil.
Complex(2, 3) <=> Complex(2, 3) #=> nil
Complex(2, 3) <=> 1 #=> nil
Complex(2) <=> 1 #=> 1
Complex(2) <=> 2 #=> 0
Complex(2) <=> 3 #=> -1
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 |
# File 'complex.c', line 1120
static VALUE
nucomp_cmp(VALUE self, VALUE other)
{
if (nucomp_real_p(self) && k_numeric_p(other)) {
if (RB_TYPE_P(other, T_COMPLEX) && nucomp_real_p(other)) {
get_dat2(self, other);
return rb_funcall(adat->real, idCmp, 1, bdat->real);
}
else if (f_real_p(other)) {
get_dat1(self);
return rb_funcall(dat->real, idCmp, 1, other);
}
}
return Qnil;
}
|
#==(object) ⇒ Boolean
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 |
# File 'complex.c', line 1082
static VALUE
nucomp_eqeq_p(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
get_dat2(self, other);
return f_boolcast(f_eqeq_p(adat->real, bdat->real) &&
f_eqeq_p(adat->imag, bdat->imag));
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_boolcast(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
}
return f_boolcast(f_eqeq_p(other, self));
}
|
#abs ⇒ Object #magnitude ⇒ Object
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 |
# File 'complex.c', line 1160
VALUE
rb_complex_abs(VALUE self)
{
get_dat1(self);
if (f_zero_p(dat->real)) {
VALUE a = f_abs(dat->imag);
if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag))
a = f_to_f(a);
return a;
}
if (f_zero_p(dat->imag)) {
VALUE a = f_abs(dat->real);
if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag))
a = f_to_f(a);
return a;
}
return rb_math_hypot(dat->real, dat->imag);
}
|
#abs2 ⇒ Object
1189 1190 1191 1192 1193 1194 1195 |
# File 'complex.c', line 1189
static VALUE
nucomp_abs2(VALUE self)
{
get_dat1(self);
return f_add(f_mul(dat->real, dat->real),
f_mul(dat->imag, dat->imag));
}
|
#arg ⇒ Float #angle ⇒ Float #phase ⇒ Float
1207 1208 1209 1210 1211 1212 |
# File 'complex.c', line 1207
VALUE
rb_complex_arg(VALUE self)
{
get_dat1(self);
return rb_math_atan2(dat->imag, dat->real);
}
|
#arg ⇒ Float #angle ⇒ Float #phase ⇒ Float
1207 1208 1209 1210 1211 1212 |
# File 'complex.c', line 1207
VALUE
rb_complex_arg(VALUE self)
{
get_dat1(self);
return rb_math_atan2(dat->imag, dat->real);
}
|
#coerce(other) ⇒ Object
:nodoc:
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 |
# File 'complex.c', line 1137
static VALUE
nucomp_coerce(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX))
return rb_assoc_new(other, self);
if (k_numeric_p(other) && f_real_p(other))
return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
rb_obj_class(other), rb_obj_class(self));
return Qnil;
}
|
#conj ⇒ Object #conjugate ⇒ Object
1253 1254 1255 1256 1257 1258 |
# File 'complex.c', line 1253
VALUE
rb_complex_conjugate(VALUE self)
{
get_dat1(self);
return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag));
}
|
#conj ⇒ Object #conjugate ⇒ Object
Returns the complex conjugate.
Complex(1, 2).conjugate #=> (1-2i)
1253 1254 1255 1256 1257 1258 |
# File 'complex.c', line 1253
VALUE
rb_complex_conjugate(VALUE self)
{
get_dat1(self);
return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag));
}
|
#denominator ⇒ Integer
Returns the denominator (lcm of both denominator - real and imag).
See numerator.
1281 1282 1283 1284 1285 1286 |
# File 'complex.c', line 1281
static VALUE
nucomp_denominator(VALUE self)
{
get_dat1(self);
return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag));
}
|
#eql?(other) ⇒ Boolean
:nodoc:
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 |
# File 'complex.c', line 1338
static VALUE
nucomp_eql_p(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
get_dat2(self, other);
return f_boolcast((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) &&
(CLASS_OF(adat->imag) == CLASS_OF(bdat->imag)) &&
f_eqeq_p(self, other));
}
return Qfalse;
}
|
#fdiv(numeric) ⇒ Object
Performs division as each part is a float, never returns a float.
Complex(11, 22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
963 964 965 966 967 |
# File 'complex.c', line 963
static VALUE
nucomp_fdiv(VALUE self, VALUE other)
{
return f_divide(self, other, f_fdiv, id_fdiv);
}
|
#finite? ⇒ Boolean
Returns true
if cmp
‘s real and imaginary parts are both finite numbers, otherwise returns false
.
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 |
# File 'complex.c', line 1440
static VALUE
rb_complex_finite_p(VALUE self)
{
get_dat1(self);
if (f_finite_p(dat->real) && f_finite_p(dat->imag)) {
return Qtrue;
}
return Qfalse;
}
|
#hash ⇒ Object
:nodoc:
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 |
# File 'complex.c', line 1322
static VALUE
nucomp_hash(VALUE self)
{
st_index_t v, h[2];
VALUE n;
get_dat1(self);
n = rb_hash(dat->real);
h[0] = NUM2LONG(n);
n = rb_hash(dat->imag);
h[1] = NUM2LONG(n);
v = rb_memhash(h, sizeof(h));
return ST2FIX(v);
}
|
#imag ⇒ Object #imaginary ⇒ Object
742 743 744 745 746 747 |
# File 'complex.c', line 742
VALUE
rb_complex_imag(VALUE self)
{
get_dat1(self);
return dat->imag;
}
|
#imag ⇒ Object #imaginary ⇒ Object
742 743 744 745 746 747 |
# File 'complex.c', line 742
VALUE
rb_complex_imag(VALUE self)
{
get_dat1(self);
return dat->imag;
}
|
#infinite? ⇒ nil, 1
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 |
# File 'complex.c', line 1463
static VALUE
rb_complex_infinite_p(VALUE self)
{
get_dat1(self);
if (NIL_P(f_infinite_p(dat->real)) && NIL_P(f_infinite_p(dat->imag))) {
return Qnil;
}
return ONE;
}
|
#inspect ⇒ String
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 |
# File 'complex.c', line 1419
static VALUE
nucomp_inspect(VALUE self)
{
VALUE s;
s = rb_usascii_str_new2("(");
rb_str_concat(s, f_format(self, rb_inspect));
rb_str_cat2(s, ")");
return s;
}
|
#abs ⇒ Object #magnitude ⇒ Object
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 |
# File 'complex.c', line 1160
VALUE
rb_complex_abs(VALUE self)
{
get_dat1(self);
if (f_zero_p(dat->real)) {
VALUE a = f_abs(dat->imag);
if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag))
a = f_to_f(a);
return a;
}
if (f_zero_p(dat->imag)) {
VALUE a = f_abs(dat->real);
if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag))
a = f_to_f(a);
return a;
}
return rb_math_hypot(dat->real, dat->imag);
}
|
#marshal_dump ⇒ Object (private)
:nodoc:
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 |
# File 'complex.c', line 1495
static VALUE
nucomp_marshal_dump(VALUE self)
{
VALUE a;
get_dat1(self);
a = rb_assoc_new(dat->real, dat->imag);
rb_copy_generic_ivar(a, self);
return a;
}
|
#numerator ⇒ Numeric
Returns the numerator.
1 2 3+4i <- numerator
- + -i -> ----
2 3 6 <- denominator
c = Complex('1/2+2/3i') #=> ((1/2)+(2/3)*i)
n = c.numerator #=> (3+4i)
d = c.denominator #=> 6
n / d #=> ((1/2)+(2/3)*i)
Complex(Rational(n.real, d), Rational(n.imag, d))
#=> ((1/2)+(2/3)*i)
See denominator.
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 |
# File 'complex.c', line 1306
static VALUE
nucomp_numerator(VALUE self)
{
VALUE cd;
get_dat1(self);
cd = nucomp_denominator(self);
return f_complex_new2(CLASS_OF(self),
f_mul(f_numerator(dat->real),
f_div(cd, f_denominator(dat->real))),
f_mul(f_numerator(dat->imag),
f_div(cd, f_denominator(dat->imag))));
}
|
#arg ⇒ Float #angle ⇒ Float #phase ⇒ Float
1207 1208 1209 1210 1211 1212 |
# File 'complex.c', line 1207
VALUE
rb_complex_arg(VALUE self)
{
get_dat1(self);
return rb_math_atan2(dat->imag, dat->real);
}
|
#polar ⇒ Array
1238 1239 1240 1241 1242 |
# File 'complex.c', line 1238
static VALUE
nucomp_polar(VALUE self)
{
return rb_assoc_new(f_abs(self), f_arg(self));
}
|
#quo ⇒ Object
#rationalize([eps]) ⇒ Object
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 |
# File 'complex.c', line 1650
static VALUE
nucomp_rationalize(int argc, VALUE *argv, VALUE self)
{
get_dat1(self);
rb_check_arity(argc, 0, 1);
if (!k_exact_zero_p(dat->imag)) {
rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
self);
}
return rb_funcallv(dat->real, id_rationalize, argc, argv);
}
|
#real ⇒ Object
725 726 727 728 729 730 |
# File 'complex.c', line 725
VALUE
rb_complex_real(VALUE self)
{
get_dat1(self);
return dat->real;
}
|
#Complex(1) ⇒ false #Complex(1, 2) ⇒ false
Returns false, even if the complex number has no imaginary part.
1267 1268 1269 1270 1271 |
# File 'complex.c', line 1267
static VALUE
nucomp_false(VALUE self)
{
return Qfalse;
}
|
#rect ⇒ Array #rectangular ⇒ Array
Returns an array; [cmp.real, cmp.imag].
Complex(1, 2).rectangular #=> [1, 2]
1223 1224 1225 1226 1227 1228 |
# File 'complex.c', line 1223
static VALUE
nucomp_rect(VALUE self)
{
get_dat1(self);
return rb_assoc_new(dat->real, dat->imag);
}
|
#rect ⇒ Array #rectangular ⇒ Array
Returns an array; [cmp.real, cmp.imag].
Complex(1, 2).rectangular #=> [1, 2]
1223 1224 1225 1226 1227 1228 |
# File 'complex.c', line 1223
static VALUE
nucomp_rect(VALUE self)
{
get_dat1(self);
return rb_assoc_new(dat->real, dat->imag);
}
|
#to_c ⇒ self
1673 1674 1675 1676 1677 |
# File 'complex.c', line 1673
static VALUE
nucomp_to_c(VALUE self)
{
return self;
}
|
#to_f ⇒ Float
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 |
# File 'complex.c', line 1600
static VALUE
nucomp_to_f(VALUE self)
{
get_dat1(self);
if (!k_exact_zero_p(dat->imag)) {
rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Float",
self);
}
return f_to_f(dat->real);
}
|
#to_i ⇒ Integer
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 |
# File 'complex.c', line 1577
static VALUE
nucomp_to_i(VALUE self)
{
get_dat1(self);
if (!k_exact_zero_p(dat->imag)) {
rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Integer",
self);
}
return f_to_i(dat->real);
}
|
#to_r ⇒ Object
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 |
# File 'complex.c', line 1625
static VALUE
nucomp_to_r(VALUE self)
{
get_dat1(self);
if (!k_exact_zero_p(dat->imag)) {
rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
self);
}
return f_to_r(dat->real);
}
|
#to_s ⇒ String
1401 1402 1403 1404 1405 |
# File 'complex.c', line 1401
static VALUE
nucomp_to_s(VALUE self)
{
return f_format(self, rb_String);
}
|