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

ruby-changes:63657

From: S-H-GAMELINKS <ko1@a...>
Date: Fri, 20 Nov 2020 11:48:03 +0900 (JST)
Subject: [ruby-changes:63657] d79cdcb113 (master): add flo_prev_or_next func

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

From d79cdcb113ed63705f857ac2a7c323db398c4256 Mon Sep 17 00:00:00 2001
From: S-H-GAMELINKS <gamelinks007@g...>
Date: Mon, 16 Nov 2020 13:22:47 +0900
Subject: add flo_prev_or_next func


diff --git a/numeric.c b/numeric.c
index 7495b11..466df46 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1820,6 +1820,25 @@ rb_flo_is_finite_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1820
     return Qtrue;
 }
 
+enum flo_prevnext_flags
+{
+    FLOAT_PREV = 0,
+    FLOAT_NEXT = 1
+};
+
+static VALUE
+flo_prev_or_next(VALUE flo, int flag)
+{
+    double x, y;
+    x = NUM2DBL(flo);
+    if (flag == 1)
+      y = nextafter(x, HUGE_VAL);
+    else
+      y = nextafter(x, -HUGE_VAL);
+
+    return DBL2NUM(y);
+}
+
 /*
  *  call-seq:
  *     float.next_float  ->  float
@@ -1875,10 +1894,7 @@ rb_flo_is_finite_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1894
 static VALUE
 flo_next_float(VALUE vx)
 {
-    double x, y;
-    x = NUM2DBL(vx);
-    y = nextafter(x, HUGE_VAL);
-    return DBL2NUM(y);
+    return flo_prev_or_next(vx, FLOAT_NEXT);
 }
 
 /*
@@ -1926,10 +1942,7 @@ flo_next_float(VALUE vx) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1942
 static VALUE
 flo_prev_float(VALUE vx)
 {
-    double x, y;
-    x = NUM2DBL(vx);
-    y = nextafter(x, -HUGE_VAL);
-    return DBL2NUM(y);
+    return flo_prev_or_next(vx, FLOAT_PREV);
 }
 
 /*
-- 
cgit v0.10.2


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

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