Skip to content

Commit

Permalink
* numeric.c: provides predicate real? instead of scalar?.
Browse files Browse the repository at this point in the history
	* complex.c: follows the above change.

	* lib/cmath.c: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19393 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
tadf committed Sep 16, 2008
1 parent d9d3781 commit acde736
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 38 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Wed Sep 17 06:58:31 2008 Tadayoshi Funaba <tadf@dotrb.org>

* numeric.c: provides predicate real? instead of scalar?.

* complex.c: follows the above change.

* lib/cmath.c: ditto.

Wed Sep 17 01:56:27 2008 Tanaka Akira <akr@fsij.org>

* test/ruby/test_io_m17n.rb: use __FILE__ instead of /dev/null.
Expand Down
11 changes: 4 additions & 7 deletions complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ VALUE rb_cComplex;
static ID id_Unify, id_abs, id_abs2, id_arg, id_cmp, id_conjugate,
id_convert, id_denominator, id_divmod, id_equal_p, id_exact_p, id_expt,
id_floor, id_hash, id_idiv, id_inspect, id_negate, id_new, id_new_bang,
id_numerator, id_polar, id_quo, id_scalar_p, id_to_f, id_to_i, id_to_r,
id_numerator, id_polar, id_quo, id_real_p, id_to_f, id_to_i, id_to_r,
id_to_s, id_truncate;

#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
Expand Down Expand Up @@ -168,9 +168,7 @@ fun1(inspect)
fun1(negate)
fun1(numerator)
fun1(polar)
fun1(scalar_p)

#define f_real_p f_scalar_p
fun1(real_p)

fun1(to_f)
fun1(to_i)
Expand Down Expand Up @@ -1383,7 +1381,7 @@ Init_Complex(void)
id_numerator = rb_intern("numerator");
id_polar = rb_intern("polar");
id_quo = rb_intern("quo");
id_scalar_p = rb_intern("scalar?");
id_real_p = rb_intern("real?");
id_to_f = rb_intern("to_f");
id_to_i = rb_intern("to_i");
id_to_r = rb_intern("to_r");
Expand Down Expand Up @@ -1459,13 +1457,12 @@ Init_Complex(void)
rb_define_method(rb_cComplex, "~", nucomp_conjugate, 0); /* gcc */
#endif

#if 0
rb_define_method(rb_cComplex, "real?", nucomp_false, 0);
#if 0
rb_define_method(rb_cComplex, "complex?", nucomp_true, 0);
rb_define_method(rb_cComplex, "exact?", nucomp_exact_p, 0);
rb_define_method(rb_cComplex, "inexact?", nucomp_inexact_p, 0);
#endif
rb_define_method(rb_cComplex, "scalar?", nucomp_false, 0);

rb_define_method(rb_cComplex, "numerator", nucomp_numerator, 0);
rb_define_method(rb_cComplex, "denominator", nucomp_denominator, 0);
Expand Down
34 changes: 17 additions & 17 deletions lib/cmath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module CMath
alias atanh! atanh

def exp(z)
if z.scalar?
if z.real?
exp!(z)
else
Complex(exp!(z.real) * cos!(z.image),
Expand All @@ -35,7 +35,7 @@ def exp(z)

def log(*args)
z, b = args
if z.scalar? and z >= 0 and (b.nil? or b >= 0)
if z.real? and z >= 0 and (b.nil? or b >= 0)
log!(*args)
else
r, theta = z.polar
Expand All @@ -48,15 +48,15 @@ def log(*args)
end

def log10(z)
if z.scalar?
if z.real?
log10!(z)
else
log(z) / log!(10)
end
end

def sqrt(z)
if z.scalar?
if z.real?
if z >= 0
sqrt!(z)
else
Expand All @@ -74,7 +74,7 @@ def sqrt(z)
end

def sin(z)
if z.scalar?
if z.real?
sin!(z)
else
Complex(sin!(z.real) * cosh!(z.image),
Expand All @@ -83,7 +83,7 @@ def sin(z)
end

def cos(z)
if z.scalar?
if z.real?
cos!(z)
else
Complex(cos!(z.real) * cosh!(z.image),
Expand All @@ -92,15 +92,15 @@ def cos(z)
end

def tan(z)
if z.scalar?
if z.real?
tan!(z)
else
sin(z)/cos(z)
end
end

def sinh(z)
if z.scalar?
if z.real?
sinh!(z)
else
Complex(sinh!(z.real) * cos!(z.image),
Expand All @@ -109,7 +109,7 @@ def sinh(z)
end

def cosh(z)
if z.scalar?
if z.real?
cosh!(z)
else
Complex(cosh!(z.real) * cos!(z.image),
Expand All @@ -118,63 +118,63 @@ def cosh(z)
end

def tanh(z)
if z.scalar?
if z.real?
tanh!(z)
else
sinh(z) / cosh(z)
end
end

def asin(z)
if z.scalar? and z >= -1 and z <= 1
if z.real? and z >= -1 and z <= 1
asin!(z)
else
-1.0.im * log(1.0.im * z + sqrt(1.0 - z * z))
end
end

def acos(z)
if z.scalar? and z >= -1 and z <= 1
if z.real? and z >= -1 and z <= 1
acos!(z)
else
-1.0.im * log(z + 1.0.im * sqrt(1.0 - z * z))
end
end

def atan(z)
if z.scalar?
if z.real?
atan!(z)
else
1.0.im * log((1.0.im + z) / (1.0.im - z)) / 2.0
end
end

def atan2(y,x)
if y.scalar? and x.scalar?
if y.real? and x.real?
atan2!(y,x)
else
-1.0.im * log((x + 1.0.im * y) / sqrt(x * x + y * y))
end
end

def acosh(z)
if z.scalar? and z >= 1
if z.real? and z >= 1
acosh!(z)
else
log(z + sqrt(z * z - 1.0))
end
end

def asinh(z)
if z.scalar?
if z.real?
asinh!(z)
else
log(z + sqrt(1.0 + z * z))
end
end

def atanh(z)
if z.scalar? and z >= -1 and z <= 1
if z.real? and z >= -1 and z <= 1
atanh!(z)
else
log((1.0 + z) / (1.0 - z)) / 2.0
Expand Down
8 changes: 4 additions & 4 deletions numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,14 @@ num_remainder(VALUE x, VALUE y)

/*
* call-seq:
* num.scalar? -> true or false
* num.real? -> true or false
*
* Returns <code>true</code> if <i>num</i> is an <code>Scalar</code>
* Returns <code>true</code> if <i>num</i> is a <code>Real</code>
* (i.e. non <code>Complex</code>).
*/

static VALUE
num_scalar_p(VALUE num)
num_real_p(VALUE num)
{
return Qtrue;
}
Expand Down Expand Up @@ -3145,7 +3145,7 @@ Init_Numeric(void)
rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);

rb_define_method(rb_cNumeric, "scalar?", num_scalar_p, 0);
rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
Expand Down
10 changes: 6 additions & 4 deletions test/ruby/test_complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,29 +243,31 @@ def test_attr2
c = Complex(1)

if defined?(Complex::Unify)
assert_equal(true, c.scalar?)
=begin
assert_equal(true, c.finite?)
assert_equal(false, c.infinite?)
assert_equal(false, c.nan?)
assert_equal(true, c.integer?)
assert_equal(false, c.float?)
assert_equal(true, c.rational?)
=end
assert_equal(true, c.real?)
=begin
assert_equal(false, c.complex?)
assert_equal(true, c.exact?)
assert_equal(false, c.inexact?)
=end
else
assert_equal(false, c.scalar?)
=begin
assert_equal(true, c.finite?)
assert_equal(false, c.infinite?)
assert_equal(false, c.nan?)
assert_equal(false, c.integer?)
assert_equal(false, c.float?)
assert_equal(false, c.rational?)
=end
assert_equal(false, c.real?)
=begin
assert_equal(true, c.complex?)
assert_equal(true, c.exact?)
assert_equal(false, c.inexact?)
Expand Down Expand Up @@ -882,8 +884,8 @@ def test_prec
end

def test_supp
assert_equal(true, 1.scalar?)
assert_equal(true, 1.1.scalar?)
assert_equal(true, 1.real?)
assert_equal(true, 1.1.real?)

assert_equal(1, 1.real)
assert_equal(0, 1.image)
Expand Down
4 changes: 2 additions & 2 deletions test/ruby/test_numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def %(x); :mod; end
end
end

def test_scalar_p
assert(Numeric.new.scalar?)
def test_real_p
assert(Numeric.new.real?)
end

def test_integer_p
Expand Down
10 changes: 6 additions & 4 deletions test/ruby/test_rational.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,31 @@ def test_attr2
c = Rational(1)

if defined?(Rational::Unify)
assert_equal(true, c.scalar?)
=begin
assert_equal(true, c.finite?)
assert_equal(false, c.infinite?)
assert_equal(false, c.nan?)
assert_equal(true, c.integer?)
assert_equal(false, c.float?)
assert_equal(true, c.rational?)
=end
assert_equal(true, c.real?)
=begin
assert_equal(false, c.complex?)
assert_equal(true, c.exact?)
assert_equal(false, c.inexact?)
=end
else
assert_equal(true, c.scalar?)
=begin
assert_equal(true, c.finite?)
assert_equal(false, c.infinite?)
assert_equal(false, c.nan?)
assert_equal(false, c.integer?)
assert_equal(false, c.float?)
assert_equal(true, c.rational?)
=end
assert_equal(true, c.real?)
=begin
assert_equal(false, c.complex?)
assert_equal(true, c.exact?)
assert_equal(false, c.inexact?)
Expand Down Expand Up @@ -1054,8 +1056,8 @@ def test_gcdlcm
end

def test_supp
assert_equal(true, 1.scalar?)
assert_equal(true, 1.1.scalar?)
assert_equal(true, 1.real?)
assert_equal(true, 1.1.real?)

assert_equal(1, 1.numerator)
assert_equal(9, 9.numerator)
Expand Down

0 comments on commit acde736

Please sign in to comment.