[前][次][番号順一覧][スレッド一覧]

ruby-changes:65198

From: S.H <ko1@a...>
Date: Tue, 9 Feb 2021 13:29:58 +0900 (JST)
Subject: [ruby-changes:65198] fad7908a5d (master): Improve performance Float#positive? and Float#negative? [Feature #17614] (#4160)

https://git.ruby-lang.org/ruby.git/commit/?id=fad7908a5d

From fad7908a5de4ab08367914d53780ff6518d5f552 Mon Sep 17 00:00:00 2001
From: "S.H" <gamelinks007@g...>
Date: Tue, 9 Feb 2021 13:29:42 +0900
Subject: Improve performance Float#positive? and Float#negative? [Feature
 #17614] (#4160)

---
 benchmark/float_neg_posi.yml |  8 ++++++++
 numeric.c                    | 30 ------------------------------
 numeric.rb                   | 22 ++++++++++++++++++++++
 3 files changed, 30 insertions(+), 30 deletions(-)
 create mode 100644 benchmark/float_neg_posi.yml

diff --git a/benchmark/float_neg_posi.yml b/benchmark/float_neg_posi.yml
new file mode 100644
index 0000000..172db1b
--- /dev/null
+++ b/benchmark/float_neg_posi.yml
@@ -0,0 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/float_neg_posi.yml#L1
+prelude: |
+  flo = 4.2
+benchmark:
+  negative?: |
+    flo.negative?
+  positive?: |
+    flo.positive?
+loop_count: 20000000
diff --git a/numeric.c b/numeric.c
index 97fc239..bcc0ab7 100644
--- a/numeric.c
+++ b/numeric.c
@@ -2386,34 +2386,6 @@ flo_truncate(int argc, VALUE *argv, VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2386
 
 /*
  *  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([ndigits])  ->  integer or float
  *
  *  Returns the largest number less than or equal to +num+ with
@@ -5654,8 +5626,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5626
     rb_define_method(rb_cFloat, "finite?",   rb_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);
 }
 
 #undef rb_float_value
diff --git a/numeric.rb b/numeric.rb
index fbddaa9..0746a83 100644
--- a/numeric.rb
+++ b/numeric.rb
@@ -204,4 +204,26 @@ class Float https://github.com/ruby/ruby/blob/trunk/numeric.rb#L204
     Primitive.attr! 'inline'
     Primitive.cexpr! 'FLOAT_ZERO_P(self) ? Qtrue : Qfalse'
   end
+
+  #
+  #  call-seq:
+  #     float.positive?  ->  true or false
+  #
+  #  Returns +true+ if +float+ is greater than 0.
+  #
+  def positive?
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'RFLOAT_VALUE(self) > 0.0 ? Qtrue : Qfalse'
+  end
+
+  #
+  #  call-seq:
+  #     float.negative?  ->  true or false
+  #
+  #  Returns +true+ if +float+ is less than 0.
+  #
+  def negative?
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'RFLOAT_VALUE(self) < 0.0 ? Qtrue : Qfalse'
+  end
 end
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]