Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trailing zeros handling in rb_uint64_convert_to_BigDecimal #203

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2862,21 +2862,29 @@ rb_uint64_convert_to_BigDecimal(uint64_t uval, RB_UNUSED_VAR(size_t digs), int r
}
else {
DECDIG buf[BIGDECIMAL_INT64_MAX_LENGTH] = {0,};
size_t exp = 0, ntz = 0;
for (; uval > 0; ++exp) {
DECDIG r = uval % BASE;
if (r == 0) ++ntz;
buf[BIGDECIMAL_INT64_MAX_LENGTH - exp - 1] = r;
DECDIG r = uval % BASE;
size_t len = 0, ntz = 0;
if (r == 0) {
// Count and skip trailing zeros
for (; r == 0 && uval > 0; ++ntz) {
uval /= BASE;
r = uval % BASE;
}
}
for (; uval > 0; ++len) {
// Store digits
buf[BIGDECIMAL_INT64_MAX_LENGTH - len - 1] = r;
uval /= BASE;
r = uval % BASE;
}

const size_t len = exp - ntz;
const size_t exp = len + ntz;
vp = VpAllocReal(len);
vp->MaxPrec = len;
vp->Prec = len;
vp->exponent = exp;
VpSetSign(vp, 1);
MEMCPY(vp->frac, buf + BIGDECIMAL_INT64_MAX_LENGTH - exp, DECDIG, len);
MEMCPY(vp->frac, buf + BIGDECIMAL_INT64_MAX_LENGTH - len, DECDIG, len);
}

return BigDecimal_wrap_struct(obj, vp);
Expand Down
11 changes: 11 additions & 0 deletions test/bigdecimal/helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# frozen_string_literal: false
require "test/unit"
require "bigdecimal"
require 'rbconfig/sizeof'

module TestBigDecimalBase
if RbConfig::SIZEOF.key?("int64_t")
SIZEOF_DECDIG = RbConfig::SIZEOF["int32_t"]
BASE = 1_000_000_000
BASE_FIG = 9
else
SIZEOF_DECDIG = RbConfig::SIZEOF["int16_t"]
BASE = 1000
BASE_FIG = 4
end

def setup
@mode = BigDecimal.mode(BigDecimal::EXCEPTION_ALL)
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, true)
Expand Down
14 changes: 13 additions & 1 deletion test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: false
require_relative "helper"
require 'bigdecimal/math'
require 'rbconfig/sizeof'

class TestBigDecimal < Test::Unit::TestCase
include TestBigDecimalBase
Expand Down Expand Up @@ -101,6 +100,19 @@ def test_BigDecimal_bug7522
assert_not_same(bd, BigDecimal(bd, 1, exception: false))
end

def test_BigDecimal_issue_192
# https://github.com/ruby/bigdecimal/issues/192
# https://github.com/rails/rails/pull/42125
if BASE_FIG == 9
int = 1_000_000_000_12345_0000
big = BigDecimal("0.100000000012345e19")
else # BASE_FIG == 4
int = 1_0000_12_00
big = BigDecimal("0.1000012e9")
end
assert_equal(BigDecimal(int), big, "[ruby/bigdecimal#192]")
end

def test_BigDecimal_with_invalid_string
[
'', '.', 'e1', 'd1', '.e', '.d', '1.e', '1.d', '.1e', '.1d',
Expand Down
15 changes: 15 additions & 0 deletions test/bigdecimal/test_bigdecimal_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def test_Float_to_d_without_precision
assert_equal(9.05, 9.05.to_d.to_f)
assert_equal("9.05", 9.05.to_d.to_s('F'))

assert_equal("65.6", 65.6.to_d.to_s("F"))

assert_equal(Math::PI, Math::PI.to_d.to_f)

bug9214 = '[ruby-core:58858]'
Expand Down Expand Up @@ -60,6 +62,19 @@ def test_Float_to_d_bug13331
"[ruby-core:80234] [Bug #13331]")
end

def test_Float_to_d_issue_192
# https://github.com/ruby/bigdecimal/issues/192
# https://github.com/rails/rails/pull/42125
if BASE_FIG == 9
flo = 1_000_000_000.12345
big = BigDecimal("0.100000000012345e10")
else # BASE_FIG == 4
flo = 1_0000.12
big = BigDecimal("0.1000012e5")
end
assert_equal(flo.to_d, big, "[ruby/bigdecimal#192]")
end

def test_Rational_to_d
digits = 100
delta = 1.0/10**(digits)
Expand Down