ruby-changes:64774
From: Kenta <ko1@a...>
Date: Thu, 7 Jan 2021 10:21:33 +0900 (JST)
Subject: [ruby-changes:64774] 82f6085b3e (master): [ruby/bigdecimal] Fix trailing zero handling in rb_uint64_convert_to_BigDecimal
https://git.ruby-lang.org/ruby.git/commit/?id=82f6085b3e From 82f6085b3edaa41be4121b218b48100d4bf78670 Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@m...> Date: Wed, 6 Jan 2021 18:17:35 +0900 Subject: [ruby/bigdecimal] Fix trailing zero handling in rb_uint64_convert_to_BigDecimal https://github.com/ruby/bigdecimal/commit/2056604d56 diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 63de77c..72c7b34 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -2736,12 +2736,15 @@ rb_uint64_convert_to_BigDecimal(uint64_t uval, RB_UNUSED_VAR(size_t digs), int r https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2736 vp->exponent = len; VpSetSign(vp, 1); - size_t i; + size_t i, ntz = 0; for (i = 0; i < len; ++i) { DECDIG r = uval % BASE; vp->frac[len - i - 1] = r; + if (r == 0) ++ntz; uval /= BASE; } + + vp->Prec -= ntz; } return BigDecimal_wrap_struct(obj, vp); diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 74c991f..fbe0d7e 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1951,6 +1951,10 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1951 assert_equal(1, BigDecimal(-1).precision) assert_equal(2, BigDecimal(10).precision) assert_equal(2, BigDecimal(-10).precision) + assert_equal(9, BigDecimal(100_000_000).precision) + assert_equal(9, BigDecimal(-100_000_000).precision) + assert_equal(12, BigDecimal(100_000_000_000).precision) + assert_equal(12, BigDecimal(-100_000_000_000).precision) assert_equal(21, BigDecimal(100_000_000_000_000_000_000).precision) assert_equal(21, BigDecimal(-100_000_000_000_000_000_000).precision) assert_equal(103, BigDecimal("111e100").precision) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/