1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* bignum.c (power_cache_get_power): Appry bigtrunc to the result of

bigsq.
  (big2str_karatsuba): Fix number of leading zero characters.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42335 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-08-02 14:53:22 +00:00
parent 7c9a3d19fc
commit 54c3b1ba19
3 changed files with 28 additions and 9 deletions

View file

@ -1,3 +1,9 @@
Fri Aug 2 23:50:53 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (power_cache_get_power): Appry bigtrunc to the result of
bigsq.
(big2str_karatsuba): Fix number of leading zero characters.
Fri Aug 2 23:48:36 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (parser_yylex): calculate denominator directly as powers of

View file

@ -4156,7 +4156,7 @@ power_cache_get_power(int base, int power_level, size_t *numdigits_ret)
numdigits = numdigits0;
}
else {
power = bigsq(power_cache_get_power(base, power_level - 1, &numdigits));
power = bigtrunc(bigsq(power_cache_get_power(base, power_level - 1, &numdigits)));
numdigits *= 2;
}
rb_obj_hide(power);
@ -4358,21 +4358,26 @@ big2str_karatsuba(struct big2str_struct *b2s, VALUE x,
bds = BDIGITS(b);
}
if (lower_power_level != power_level-1 && b2s->ptr) {
len = (half_numdigits - lower_numdigits) * 2;
memset(b2s->ptr, '0', len);
b2s->ptr += len;
}
if (lower_power_level == 0 &&
(xn < bn ||
(xn == bn && bary_cmp(xds, xn, bds, bn) < 0))) {
if (b2s->ptr) {
len = half_numdigits * 2 - lower_numdigits;
memset(b2s->ptr, '0', len);
b2s->ptr += len;
}
big2str_orig(b2s, x, taillen);
}
else {
if (lower_power_level != power_level-1 && b2s->ptr) {
len = (half_numdigits - lower_numdigits) * 2;
memset(b2s->ptr, '0', len);
b2s->ptr += len;
}
bigdivmod(x, b, &q, &r);
bigtrunc(q);
bigtrunc(r);
assert(RBIGNUM_LEN(q) <= RBIGNUM_LEN(b));
rb_obj_hide(q);
rb_obj_hide(r);
big2str_karatsuba(b2s, q, lower_power_level, lower_numdigits+taillen);
@ -4454,8 +4459,14 @@ rb_big2str1(VALUE x, int base)
#ifndef NDEBUG
if (0 < power_level) {
VALUE power0 = power_cache_get_power(base, power_level-1, NULL);
assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power0), RBIGNUM_LEN(power0))) >= 0);
VALUE power1 = power_cache_get_power(base, power_level-1, NULL);
assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power1), RBIGNUM_LEN(power1))) >= 0);
/*
{
VALUE power0 = power_cache_get_power(base, power_level, NULL);
assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power0), RBIGNUM_LEN(power0))) < 0);
}
*/
}
#endif

View file

@ -106,6 +106,8 @@ class TestBignum < Test::Unit::TestCase
assert_equal("nd075ib45k86g" ,18446744073709551616.to_s(31), "[ruby-core:10686]")
assert_equal("1777777777777777777777" ,18446744073709551615.to_s(8))
assert_equal("-1777777777777777777777" ,-18446744073709551615.to_s(8))
assert_match(/\A10{99}1\z/, (10**100+1).to_s)
assert_match(/\A10{900}9{100}\z/, (10**1000+(10**100-1)).to_s)
end
b = 2**64