diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-05-17 06:01:47 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-05-17 06:01:47 +0000 |
commit | fb6ca7beef5f6ffecfc23e728bbbd5ec3618f660 (patch) | |
tree | c1ba22f01a708157a7ceef0725942ebce1f61ef3 /numeric.c | |
parent | 863b127faab768d796e2f1afe897d4229deb8f06 (diff) | |
download | ruby-fb6ca7beef5f6ffecfc23e728bbbd5ec3618f660.tar.gz |
numeric.c: Numeric#positive? and Numeric#negative?
* numeric.c (num_positive_p, num_negative_p): add methods
Numeric#positive? and Numeric#negative?.
[ruby-core:69173] [Feature #11151]
* numeric.c (flo_positive_p, flo_negative_p): specialiazed
functions for Float.
* complex.c (Init_Complex): Complex do not have positive? and
negative? methods
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50522 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -648,6 +648,42 @@ num_to_int(VALUE num) return rb_funcallv(num, id_to_i, 0, 0); } +/* + * call-seq: + * num.positive? -> true or false + * + * Returns +true+ if +num+ is greater than 0. + */ + +static VALUE +num_positive_p(VALUE num) +{ + const ID mid = '>'; + + if (FIXNUM_P(num)) { + if (method_basic_p(rb_cFixnum)) + return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0); + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + if (method_basic_p(rb_cBignum)) + return BIGNUM_POSITIVE_P(num); + } + return RTEST(compare_with_zero(num, mid)); +} + +/* + * call-seq: + * num.positive? -> true or false + * + * Returns +true+ if +num+ is less than 0. + */ + +static VALUE +num_negative_p(VALUE num) +{ + return negative_int_p(num) ? Qtrue : Qfalse; +} + /******************************************************************** * @@ -1830,6 +1866,34 @@ flo_truncate(VALUE num) /* * call-seq: + * float.positive? -> true or false + * + * Returns +true+ if +float+ is greater than 0. + */ + +static VALUE +flo_positive_p(VALUE num) +{ + double f = RFLOAT_VALUE(num); + return f > 0.0 ? Qtrue : Qfalse; +} + +/* + * call-seq: + * float.negative? -> true or false + * + * Returns +true+ if +float+ is less than 0. + */ + +static VALUE +flo_negative_p(VALUE num) +{ + double f = RFLOAT_VALUE(num); + return f < 0.0 ? Qtrue : Qfalse; +} + +/* + * call-seq: * num.floor -> integer * * Returns the largest integer less than or equal to +num+. @@ -4062,6 +4126,8 @@ Init_Numeric(void) rb_define_method(rb_cNumeric, "round", num_round, -1); rb_define_method(rb_cNumeric, "truncate", num_truncate, 0); rb_define_method(rb_cNumeric, "step", num_step, -1); + rb_define_method(rb_cNumeric, "positive?", num_positive_p, 0); + rb_define_method(rb_cNumeric, "negative?", num_negative_p, 0); rb_cInteger = rb_define_class("Integer", rb_cNumeric); rb_undef_alloc_func(rb_cInteger); @@ -4265,6 +4331,8 @@ Init_Numeric(void) rb_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0); rb_define_method(rb_cFloat, "next_float", flo_next_float, 0); rb_define_method(rb_cFloat, "prev_float", flo_prev_float, 0); + rb_define_method(rb_cFloat, "positive?", flo_positive_p, 0); + rb_define_method(rb_cFloat, "negative?", flo_negative_p, 0); id_to = rb_intern("to"); id_by = rb_intern("by"); |