diff options
author | S-H-GAMELINKS <gamelinks007@gmail.com> | 2020-11-16 13:22:47 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-11-20 11:47:47 +0900 |
commit | d79cdcb113ed63705f857ac2a7c323db398c4256 (patch) | |
tree | f4f177bd0c78b050cd795f74570256782b38029a /numeric.c | |
parent | 1800f3fa5c29515113ecdcc5695e8a96f462b74c (diff) | |
download | ruby-d79cdcb113ed63705f857ac2a7c323db398c4256.tar.gz |
add flo_prev_or_next func
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 29 |
1 files changed, 21 insertions, 8 deletions
@@ -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); } /* |