summaryrefslogtreecommitdiff
path: root/rational.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-25 06:28:00 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-25 06:28:00 +0000
commit631dde2572eb78cbf09d73d23a43852ccb199bd1 (patch)
treed85aad8b66468b69730c9a85362879b56f5aa41a /rational.c
parentb6d10b6c378edcb88d3238b5c862e585ad9c8b33 (diff)
round-down
* numeric.c (round_half_down, int_round_half_down): support round-down mode. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'rational.c')
-rw-r--r--rational.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/rational.c b/rational.c
index f7af5ca353..a78b347c08 100644
--- a/rational.c
+++ b/rational.c
@@ -1324,6 +1324,31 @@ nurat_round_half_up(VALUE self)
}
static VALUE
+nurat_round_half_down(VALUE self)
+{
+ VALUE num, den, neg;
+
+ get_dat1(self);
+
+ num = dat->num;
+ den = dat->den;
+ neg = INT_NEGATIVE_P(num);
+
+ if (neg)
+ num = rb_int_uminus(num);
+
+ num = rb_int_plus(rb_int_mul(num, TWO), den);
+ num = rb_int_minus(num, ONE);
+ den = rb_int_mul(den, TWO);
+ num = rb_int_idiv(num, den);
+
+ if (neg)
+ num = rb_int_uminus(num);
+
+ return num;
+}
+
+static VALUE
nurat_round_half_even(VALUE self)
{
VALUE num, den, neg, qr;