summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorS-H-GAMELINKS <gamelinks007@gmail.com>2020-11-16 13:22:47 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-11-20 11:47:47 +0900
commitd79cdcb113ed63705f857ac2a7c323db398c4256 (patch)
treef4f177bd0c78b050cd795f74570256782b38029a /numeric.c
parent1800f3fa5c29515113ecdcc5695e8a96f462b74c (diff)
add flo_prev_or_next func
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3769
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/numeric.c b/numeric.c
index 7495b11095..466df469dc 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1820,6 +1820,25 @@ rb_flo_is_finite_p(VALUE num)
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)
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)
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);
}
/*