Skip to content

Implement BigDecimal#_decimal_shift for internal use #324

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
59 changes: 59 additions & 0 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,64 @@ BigDecimal_inspect(VALUE self)
return str;
}

/* Returns self * 10**v without changing the precision.
* This method is currently for internal use.
*
* BigDecimal("0.123e10")._decimal_shift(20) #=> "0.123e30"
* BigDecimal("0.123e10")._decimal_shift(-20) #=> "0.123e-10"
*/
static VALUE
BigDecimal_decimal_shift(VALUE self, VALUE v)
{
ENTER(5);
Real *c, *a;

v = rb_to_int(v);
if (!FIXNUM_P(v)) {
rb_raise(rb_eTypeError, "invalid shift width");
}
long shift = FIX2LONG(v);
if (shift == 0) return self;

GUARD_OBJ(a, GetVpValue(self, 1));
if (VpIsZero(a) || VpIsNaN(a) || VpIsInf(a)) return self;

long exponentShift = shift > 0 ? shift / BASE_FIG : (shift + 1) / BASE_FIG - 1;
shift -= exponentShift * BASE_FIG;
DECDIG ex = 1;
for (int i = 0; i < shift; i++) ex *= 10;
bool shiftDown = a->frac[0] * (DECDIG_DBL)ex >= BASE;
DECDIG iex = BASE / ex;

size_t prec = a->Prec + shiftDown;
size_t mx = prec * (VpBaseFig() + 1);
GUARD_OBJ(c, NewZeroWrapLimited(1, mx));
if (shift == 0) {
VpAsgn(c, a, 1);
} else if (shiftDown) {
exponentShift++;
DECDIG carry = 0;
for (size_t i = 0; i < a->Prec; i++) {
DECDIG v = a->frac[i];
c->frac[i] = carry * ex + v / iex;
carry = v % iex;
}
c->frac[a->Prec] = carry * ex;
} else {
DECDIG carry = 0;
for (ssize_t i = a->Prec - 1; i >= 0; i--) {
DECDIG v = a->frac[i];
c->frac[i] = v % iex * ex + carry;
carry = v / iex;
}
}
while (c->frac[prec - 1] == 0) prec--;
c->Prec = prec;
c->exponent = a->exponent + exponentShift;
c->sign = a->sign;
return VpCheckGetValue(c);
}

static VALUE BigMath_s_exp(VALUE, VALUE, VALUE);
static VALUE BigMath_s_log(VALUE, VALUE, VALUE);

Expand Down Expand Up @@ -4633,6 +4691,7 @@ Init_bigdecimal(void)
rb_define_method(rb_cBigDecimal, "infinite?", BigDecimal_IsInfinite, 0);
rb_define_method(rb_cBigDecimal, "finite?", BigDecimal_IsFinite, 0);
rb_define_method(rb_cBigDecimal, "truncate", BigDecimal_truncate, -1);
rb_define_method(rb_cBigDecimal, "_decimal_shift", BigDecimal_decimal_shift, 1);
rb_define_method(rb_cBigDecimal, "_dump", BigDecimal_dump, -1);

rb_mBigMath = rb_define_module("BigMath");
Expand Down
18 changes: 18 additions & 0 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,24 @@ def test_sqrt_5266
x.sqrt(109).to_s(109).split(' ')[0])
end

def test_internal_use_decimal_shift
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
assert_positive_infinite(BigDecimal("Infinity")._decimal_shift(10))
assert_negative_infinite(BigDecimal("-Infinity")._decimal_shift(10))
assert_nan(BigDecimal::NAN._decimal_shift(10))
assert_equal(BigDecimal(0), BigDecimal(0)._decimal_shift(10))
[
BigDecimal('-1234.56789'),
BigDecimal('123456789.012345678987654321'),
BigDecimal('1234567890123.45678987654321')
].each do |num|
(0..20).each do |shift|
assert_equal(num * 10**shift, num._decimal_shift(shift))
assert_equal(num / 10**shift, num._decimal_shift(-shift))
end
end
end

def test_fix
x = BigDecimal("1.1")
assert_equal(1, x.fix)
Expand Down
Loading