summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--complex.c129
-rw-r--r--rational.c62
3 files changed, 103 insertions, 93 deletions
diff --git a/ChangeLog b/ChangeLog
index 6022d092ba..359a1e466f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sat Nov 3 23:38:15 2012 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * complex.c: modified doc.
+ * rational.c: ditto.
+
Sat Nov 3 22:38:55 2012 Tadayoshi Funaba <tadf@dotrb.org>
* ext/date/date_core.c: modified doc.
diff --git a/complex.c b/complex.c
index 94a28f0278..f2f524011c 100644
--- a/complex.c
+++ b/complex.c
@@ -440,8 +440,7 @@ nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag)
*
* Returns a complex object which denotes the given rectangular form.
*
- * For example:
- * Complex.rect(12, 2) # => (12+2i)
+ * Complex.rectangular(1, 2) #=> (1+2i)
*/
static VALUE
nucomp_s_new(int argc, VALUE *argv, VALUE klass)
@@ -481,6 +480,8 @@ f_complex_new2(VALUE klass, VALUE x, VALUE y)
* Complex(x[, y]) -> numeric
*
* Returns x+i*y;
+ *
+ * Complex(1, 2) #=> (1+2i)
*/
static VALUE
nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
@@ -590,10 +591,10 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y)
*
* Returns a complex object which denotes the given polar form.
*
- * Complex.polar(3, 0) #=> (3.0+0.0i)
- * Complex.polar(3, Math::PI/2) #=> (1.836909530733566e-16+3.0i)
- * Complex.polar(3, Math::PI) #=> (-3.0+3.673819061467132e-16i)
- * Complex.polar(3, -Math::PI/2) #=> (1.836909530733566e-16-3.0i)
+ * Complex.polar(3, 0) #=> (3.0+0.0i)
+ * Complex.polar(3, Math::PI/2) #=> (1.836909530733566e-16+3.0i)
+ * Complex.polar(3, Math::PI) #=> (-3.0+3.673819061467132e-16i)
+ * Complex.polar(3, -Math::PI/2) #=> (1.836909530733566e-16-3.0i)
*/
static VALUE
nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
@@ -618,6 +619,9 @@ nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
* cmp.real -> real
*
* Returns the real part.
+ *
+ * Complex(7).real #=> 7
+ * Complex(9, -4).real #=> 9
*/
static VALUE
nucomp_real(VALUE self)
@@ -632,6 +636,9 @@ nucomp_real(VALUE self)
* cmp.imaginary -> real
*
* Returns the imaginary part.
+ *
+ * Complex(7).imaginary #=> 0
+ * Complex(9, -4).imaginary #=> -4
*/
static VALUE
nucomp_imag(VALUE self)
@@ -645,6 +652,8 @@ nucomp_imag(VALUE self)
* -cmp -> complex
*
* Returns negation of the value.
+ *
+ * -Complex(1, 2) #=> (-1-2i)
*/
static VALUE
nucomp_negate(VALUE self)
@@ -683,10 +692,11 @@ f_addsub(VALUE self, VALUE other,
*
* Performs addition.
*
- * Complex(5, 2) + 3 # => (8+2i)
- * Complex(5, 2) + 3.i # => (5+5i)
- * Complex(5, 2) + Complex(3, 4) # => (8+6i)
- *
+ * Complex(2, 3) + Complex(2, 3) #=> (4+6i)
+ * Complex(900) + Complex(1) #=> (901+0i)
+ * Complex(-2, 9) + Complex(-9, 2) #=> (-11+11i)
+ * Complex(9, 8) + 4 #=> (13+8i)
+ * Complex(20, 9) + 9.8 #=> (29.8+9i)
*/
static VALUE
nucomp_add(VALUE self, VALUE other)
@@ -700,7 +710,11 @@ nucomp_add(VALUE self, VALUE other)
*
* Performs subtraction.
*
- * Complex(33, 12) - 10 # => (23+12i)
+ * Complex(2, 3) - Complex(2, 3) #=> (0+0i)
+ * Complex(900) - Complex(1) #=> (899+0i)
+ * Complex(-2, 9) - Complex(-9, 2) #=> (7+7i)
+ * Complex(9, 8) - 4 #=> (5+8i)
+ * Complex(20, 9) - 9.8 #=> (10.2+9i)
*/
static VALUE
nucomp_sub(VALUE self, VALUE other)
@@ -714,7 +728,11 @@ nucomp_sub(VALUE self, VALUE other)
*
* Performs multiplication.
*
- * Complex(78, 58) * 10 # => (780+580i)
+ * Complex(2, 3) * Complex(2, 3) #=> (-5+12i)
+ * Complex(900) * Complex(1) #=> (900+0i)
+ * Complex(-2, 9) * Complex(-9, 2) #=> (0-85i)
+ * Complex(9, 8) * 4 #=> (36+32i)
+ * Complex(20, 9) * 9.8 #=> (196.0+88.2i)
*/
static VALUE
nucomp_mul(VALUE self, VALUE other)
@@ -802,10 +820,11 @@ f_divide(VALUE self, VALUE other,
*
* Performs division.
*
- * For example:
- *
- * Complex(10.0) / 3 #=> (3.3333333333333335+(0/1)*i)
- * Complex(10) / 3 #=> ((10/3)+(0/1)*i) # not (3+0i)
+ * Complex(2, 3) / Complex(2, 3) #=> ((1/1)+(0/1)*i)
+ * Complex(900) / Complex(1) #=> ((900/1)+(0/1)*i)
+ * Complex(-2, 9) / Complex(-9, 2) #=> ((36/85)-(77/85)*i)
+ * Complex(9, 8) / 4 #=> ((9/4)+(2/1)*i)
+ * Complex(20, 9) / 9.8 #=> (2.0408163265306123+0.9183673469387754i)
*/
static VALUE
nucomp_div(VALUE self, VALUE other)
@@ -821,9 +840,7 @@ nucomp_div(VALUE self, VALUE other)
*
* Performs division as each part is a float, never returns a float.
*
- * For example:
- *
- * Complex(11,22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
+ * Complex(11, 22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
*/
static VALUE
nucomp_fdiv(VALUE self, VALUE other)
@@ -843,10 +860,8 @@ f_reciprocal(VALUE x)
*
* Performs exponentiation.
*
- * For example:
- *
- * Complex('i') ** 2 #=> (-1+0i)
- * Complex(-8) ** Rational(1,3) #=> (1.0000000000000002+1.7320508075688772i)
+ * Complex('i') ** 2 #=> (-1+0i)
+ * Complex(-8) ** Rational(1, 3) #=> (1.0000000000000002+1.7320508075688772i)
*/
static VALUE
nucomp_expt(VALUE self, VALUE other)
@@ -932,6 +947,12 @@ nucomp_expt(VALUE self, VALUE other)
* cmp == object -> true or false
*
* Returns true if cmp equals object numerically.
+ *
+ * Complex(2, 3) == Complex(2, 3) #=> true
+ * Complex(5) == 5 #=> true
+ * Complex(0) == 0.0 #=> true
+ * Complex('1/3') == 0.33 #=> false
+ * Complex('1/2') == '1/2' #=> false
*/
static VALUE
nucomp_eqeq_p(VALUE self, VALUE other)
@@ -970,6 +991,9 @@ nucomp_coerce(VALUE self, VALUE other)
* cmp.magnitude -> real
*
* Returns the absolute part of its polar form.
+ *
+ * Complex(-1).abs #=> 1
+ * Complex(3.0, -4.0).abs #=> 5.0
*/
static VALUE
nucomp_abs(VALUE self)
@@ -996,6 +1020,9 @@ nucomp_abs(VALUE self)
* cmp.abs2 -> real
*
* Returns square of the absolute value.
+ *
+ * Complex(-1).abs2 #=> 1
+ * Complex(3.0, -4.0).abs2 #=> 25.0
*/
static VALUE
nucomp_abs2(VALUE self)
@@ -1013,8 +1040,7 @@ nucomp_abs2(VALUE self)
*
* Returns the angle part of its polar form.
*
- * Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966
- *
+ * Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966
*/
static VALUE
nucomp_arg(VALUE self)
@@ -1029,6 +1055,8 @@ nucomp_arg(VALUE self)
* cmp.rectangular -> array
*
* Returns an array; [cmp.real, cmp.imag].
+ *
+ * Complex(1, 2).rectangular #=> [1, 2]
*/
static VALUE
nucomp_rect(VALUE self)
@@ -1042,6 +1070,8 @@ nucomp_rect(VALUE self)
* cmp.polar -> array
*
* Returns an array; [cmp.abs, cmp.arg].
+ *
+ * Complex(1, 2).polar #=> [2.23606797749979, 1.1071487177940904]
*/
static VALUE
nucomp_polar(VALUE self)
@@ -1055,6 +1085,8 @@ nucomp_polar(VALUE self)
* cmp.conjugate -> complex
*
* Returns the complex conjugate.
+ *
+ * Complex(1, 2).conjugate #=> (1-2i)
*/
static VALUE
nucomp_conj(VALUE self)
@@ -1122,8 +1154,6 @@ nucomp_denominator(VALUE self)
*
* Returns the numerator.
*
- * For example:
- *
* 1 2 3+4i <- numerator
* - + -i -> ----
* 2 3 6 <- denominator
@@ -1229,6 +1259,12 @@ f_format(VALUE self, VALUE (*func)(VALUE))
* cmp.to_s -> string
*
* Returns the value as a string.
+ *
+ * Complex(2).to_s #=> "2+0i"
+ * Complex('-8/6').to_s #=> "-4/3+0i"
+ * Complex('1/2i').to_s #=> "0+1/2i"
+ * Complex(0, Float::INFINITY).to_s #=> "0+Infinity*i"
+ * Complex(Float::NAN, Float::NAN).to_s #=> "NaN+NaN*i"
*/
static VALUE
nucomp_to_s(VALUE self)
@@ -1241,6 +1277,12 @@ nucomp_to_s(VALUE self)
* cmp.inspect -> string
*
* Returns the value as a string for inspection.
+ *
+ * Complex(2).inspect #=> "(2+0i)"
+ * Complex('-8/6').inspect #=> "((-4/3)+0i)"
+ * Complex('1/2i').inspect #=> "(0+(1/2)*i)"
+ * Complex(0, Float::INFINITY).inspect #=> "(0+Infinity*i)"
+ * Complex(Float::NAN, Float::NAN).inspect #=> "(NaN+NaN*i)"
*/
static VALUE
nucomp_inspect(VALUE self)
@@ -1331,7 +1373,12 @@ rb_Complex(VALUE x, VALUE y)
* call-seq:
* cmp.to_i -> integer
*
- * Returns the value as an integer if possible.
+ * Returns the value as an integer if possible (the imaginary part
+ * should be exactly zero).
+ *
+ * Complex(1, 0).to_i #=> 1
+ * Complex(1, 0.0).to_i # RangeError
+ * Complex(1, 2).to_i # RangeError
*/
static VALUE
nucomp_to_i(VALUE self)
@@ -1350,7 +1397,12 @@ nucomp_to_i(VALUE self)
* call-seq:
* cmp.to_f -> float
*
- * Returns the value as a float if possible.
+ * Returns the value as a float if possible (the imaginary part should
+ * be exactly zero).
+ *
+ * Complex(1, 0).to_f #=> 1.0
+ * Complex(1, 0.0).to_f # RangeError
+ * Complex(1, 2).to_f # RangeError
*/
static VALUE
nucomp_to_f(VALUE self)
@@ -1369,8 +1421,12 @@ nucomp_to_f(VALUE self)
* call-seq:
* cmp.to_r -> rational
*
- * If the imaginary part is exactly 0, returns the real part as a Rational,
- * otherwise a RangeError is raised.
+ * Returns the value as a rational if possible (the imaginary part
+ * should be exactly zero).
+ *
+ * Complex(1, 0).to_r #=> (1/1)
+ * Complex(1, 0.0).to_r # RangeError
+ * Complex(1, 2).to_r # RangeError
*/
static VALUE
nucomp_to_r(VALUE self)
@@ -1389,8 +1445,13 @@ nucomp_to_r(VALUE self)
* call-seq:
* cmp.rationalize([eps]) -> rational
*
- * If the imaginary part is exactly 0, returns the real part as a Rational,
- * otherwise a RangeError is raised.
+ * Returns the value as a rational if possible (the imaginary part
+ * should be exactly zero). The optional argument eps is always
+ * ignored.
+ *
+ * Complex(1.0/3, 0).rationalize #=> (1/3)
+ * Complex(1, 0.0).rationalize # RangeError
+ * Complex(1, 2).rationalize # RangeError
*/
static VALUE
nucomp_rationalize(int argc, VALUE *argv, VALUE self)
@@ -1594,8 +1655,6 @@ string_to_c_strict(VALUE self)
* sequences can be separated by an underscore. Returns zero for null
* or garbage string.
*
- * For example:
- *
* '9'.to_c #=> (9+0i)
* '2.5'.to_c #=> (2.5+0i)
* '2.5/1'.to_c #=> ((5/2)+0i)
diff --git a/rational.c b/rational.c
index 99bb06aad9..c06794f31d 100644
--- a/rational.c
+++ b/rational.c
@@ -548,6 +548,8 @@ f_rational_new_no_reduce2(VALUE klass, VALUE x, VALUE y)
* Rational(x[, y]) -> numeric
*
* Returns x/y;
+ *
+ * Rational(1, 2) #=> (1/2)
*/
static VALUE
nurat_f_rational(int argc, VALUE *argv, VALUE klass)
@@ -561,8 +563,6 @@ nurat_f_rational(int argc, VALUE *argv, VALUE klass)
*
* Returns the numerator.
*
- * For example:
- *
* Rational(7).numerator #=> 7
* Rational(7, 1).numerator #=> 7
* Rational(9, -4).numerator #=> -9
@@ -581,8 +581,6 @@ nurat_numerator(VALUE self)
*
* Returns the denominator (always positive).
*
- * For example:
- *
* Rational(7).denominator #=> 1
* Rational(7, 1).denominator #=> 1
* Rational(9, -4).denominator #=> 4
@@ -687,8 +685,6 @@ f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
*
* Performs addition.
*
- * For example:
- *
* Rational(2, 3) + Rational(2, 3) #=> (4/3)
* Rational(900) + Rational(1) #=> (900/1)
* Rational(-2, 9) + Rational(-9, 2) #=> (-85/18)
@@ -729,8 +725,6 @@ nurat_add(VALUE self, VALUE other)
*
* Performs subtraction.
*
- * For example:
- *
* Rational(2, 3) - Rational(2, 3) #=> (0/1)
* Rational(900) - Rational(1) #=> (899/1)
* Rational(-2, 9) - Rational(-9, 2) #=> (77/18)
@@ -810,8 +804,6 @@ f_muldiv(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
*
* Performs multiplication.
*
- * For example:
- *
* Rational(2, 3) * Rational(2, 3) #=> (4/9)
* Rational(900) * Rational(1) #=> (900/1)
* Rational(-2, 9) * Rational(-9, 2) #=> (1/1)
@@ -853,8 +845,6 @@ nurat_mul(VALUE self, VALUE other)
*
* Performs division.
*
- * For example:
- *
* Rational(2, 3) / Rational(2, 3) #=> (1/1)
* Rational(900) / Rational(1) #=> (900/1)
* Rational(-2, 9) / Rational(-9, 2) #=> (4/81)
@@ -913,8 +903,6 @@ nurat_div(VALUE self, VALUE other)
*
* Performs division and returns the value as a float.
*
- * For example:
- *
* Rational(2, 3).fdiv(1) #=> 0.6666666666666666
* Rational(2, 3).fdiv(0.5) #=> 1.3333333333333333
* Rational(2).fdiv(3) #=> 0.6666666666666666
@@ -933,8 +921,6 @@ nurat_fdiv(VALUE self, VALUE other)
*
* Performs exponentiation.
*
- * For example:
- *
* Rational(2) ** Rational(3) #=> (8/1)
* Rational(10) ** -2 #=> (1/100)
* Rational(10) ** -2.0 #=> 0.01
@@ -995,8 +981,6 @@ nurat_expt(VALUE self, VALUE other)
*
* Performs comparison and returns -1, 0, or +1.
*
- * For example:
- *
* Rational(2, 3) <=> Rational(2, 3) #=> 0
* Rational(5) <=> 5 #=> 0
* Rational(2,3) <=> Rational(1,3) #=> 1
@@ -1046,8 +1030,6 @@ nurat_cmp(VALUE self, VALUE other)
*
* Returns true if rat equals object numerically.
*
- * For example:
- *
* Rational(2, 3) == Rational(2, 3) #=> true
* Rational(5) == 5 #=> true
* Rational(0) == 0.0 #=> true
@@ -1172,8 +1154,6 @@ nurat_ceil(VALUE self)
* Equivalent to
* rat.truncate.
*
- * For example:
- *
* Rational(2, 3).to_i #=> 0
* Rational(3).to_i #=> 3
* Rational(300.6).to_i #=> 300
@@ -1246,8 +1226,6 @@ f_round_common(int argc, VALUE *argv, VALUE self, VALUE (*func)(VALUE))
*
* Returns the truncated value (toward negative infinity).
*
- * For example:
- *
* Rational(3).floor #=> 3
* Rational(2, 3).floor #=> 0
* Rational(-3, 2).floor #=> -1
@@ -1272,8 +1250,6 @@ nurat_floor_n(int argc, VALUE *argv, VALUE self)
*
* Returns the truncated value (toward positive infinity).
*
- * For example:
- *
* Rational(3).ceil #=> 3
* Rational(2, 3).ceil #=> 1
* Rational(-3, 2).ceil #=> -1
@@ -1298,8 +1274,6 @@ nurat_ceil_n(int argc, VALUE *argv, VALUE self)
*
* Returns the truncated value (toward zero).
*
- * For example:
- *
* Rational(3).truncate #=> 3
* Rational(2, 3).truncate #=> 0
* Rational(-3, 2).truncate #=> -1
@@ -1325,8 +1299,6 @@ nurat_truncate_n(int argc, VALUE *argv, VALUE self)
* Returns the truncated value (toward the nearest integer;
* 0.5 => 1; -0.5 => -1).
*
- * For example:
- *
* Rational(3).round #=> 3
* Rational(2, 3).round #=> 1
* Rational(-3, 2).round #=> -2
@@ -1350,8 +1322,6 @@ nurat_round_n(int argc, VALUE *argv, VALUE self)
*
* Return the value as a float.
*
- * For example:
- *
* Rational(2).to_f #=> 2.0
* Rational(9, 4).to_f #=> 2.25
* Rational(-3, 4).to_f #=> -0.75
@@ -1370,8 +1340,6 @@ nurat_to_f(VALUE self)
*
* Returns self.
*
- * For example:
- *
* Rational(2).to_r #=> (2/1)
* Rational(-8, 6).to_r #=> (-4/3)
*/
@@ -1486,8 +1454,6 @@ nurat_rationalize_internal(VALUE a, VALUE b, VALUE *p, VALUE *q)
* argument eps is given (rat-|eps| <= result <= rat+|eps|), self
* otherwise.
*
- * For example:
- *
* r = Rational(5033165, 16777216)
* r.rationalize #=> (5033165/16777216)
* r.rationalize(Rational('0.01')) #=> (3/10)
@@ -1551,11 +1517,9 @@ f_format(VALUE self, VALUE (*func)(VALUE))
*
* Returns the value as a string.
*
- * For example:
- *
* Rational(2).to_s #=> "2/1"
* Rational(-8, 6).to_s #=> "-4/3"
- * Rational('0.5').to_s #=> "1/2"
+ * Rational('1/2').to_s #=> "1/2"
*/
static VALUE
nurat_to_s(VALUE self)
@@ -1569,11 +1533,9 @@ nurat_to_s(VALUE self)
*
* Returns the value as a string for inspection.
*
- * For example:
- *
* Rational(2).inspect #=> "(2/1)"
* Rational(-8, 6).inspect #=> "(-4/3)"
- * Rational('0.5').inspect #=> "(1/2)"
+ * Rational('1/2').inspect #=> "(1/2)"
*/
static VALUE
nurat_inspect(VALUE self)
@@ -1652,8 +1614,6 @@ rb_rational_reciprocal(VALUE x)
* Returns the greatest common divisor (always positive). 0.gcd(x)
* and x.gcd(0) return abs(x).
*
- * For example:
- *
* 2.gcd(2) #=> 2
* 3.gcd(-7) #=> 1
* ((1<<31)-1).gcd((1<<61)-1) #=> 1
@@ -1672,8 +1632,6 @@ rb_gcd(VALUE self, VALUE other)
* Returns the least common multiple (always positive). 0.lcm(x) and
* x.lcm(0) return zero.
*
- * For example:
- *
* 2.lcm(2) #=> 2
* 3.lcm(-7) #=> 21
* ((1<<31)-1).lcm((1<<61)-1) #=> 4951760154835678088235319297
@@ -1691,8 +1649,6 @@ rb_lcm(VALUE self, VALUE other)
*
* Returns an array; [int.gcd(int2), int.lcm(int2)].
*
- * For example:
- *
* 2.gcdlcm(2) #=> [2, 2]
* 3.gcdlcm(-7) #=> [1, 21]
* ((1<<31)-1).gcdlcm((1<<61)-1) #=> [1, 4951760154835678088235319297]
@@ -1790,8 +1746,6 @@ integer_denominator(VALUE self)
*
* Returns the numerator. The result is machine dependent.
*
- * For example:
- *
* n = 0.3.numerator #=> 5404319552844595
* d = 0.3.denominator #=> 18014398509481984
* n.fdiv(d) #=> 0.3
@@ -1855,8 +1809,6 @@ nilclass_rationalize(int argc, VALUE *argv, VALUE self)
*
* Returns the value as a rational.
*
- * For example:
- *
* 1.to_r #=> (1/1)
* (1<<64).to_r #=> (18446744073709551616/1)
*/
@@ -1916,8 +1868,6 @@ float_decode(VALUE self)
* NOTE: 0.3.to_r isn't the same as '0.3'.to_r. The latter is
* equivalent to '3/10'.to_r, but the former isn't so.
*
- * For example:
- *
* 2.0.to_r #=> (2/1)
* 2.5.to_r #=> (5/2)
* -0.75.to_r #=> (-3/4)
@@ -1953,8 +1903,6 @@ float_to_r(VALUE self)
* <= flt+|eps|). if eps is not given, it will be chosen
* automatically.
*
- * For example:
- *
* 0.3.rationalize #=> (3/10)
* 1.333.rationalize #=> (1333/1000)
* 1.333.rationalize(0.01) #=> (4/3)
@@ -2155,8 +2103,6 @@ string_to_r_strict(VALUE self)
* NOTE: '0.3'.to_r isn't the same as 0.3.to_r. The former is
* equivalent to '3/10'.to_r, but the latter isn't so.
*
- * For example:
- *
* ' 2 '.to_r #=> (2/1)
* '300/2'.to_r #=> (150/1)
* '-9.2'.to_r #=> (-46/5)