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

sprintf.c: exact number

* sprintf.c (rb_str_format): format exact number more exactly.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53533 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-01-14 07:12:42 +00:00
parent ed11f4b4b5
commit 1301d1f5bc
3 changed files with 18 additions and 3 deletions

View file

@ -1,3 +1,7 @@
Thu Jan 14 16:12:09 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* sprintf.c (rb_str_format): format exact number more exactly.
Thu Jan 14 15:08:43 2016 Tony Arcieri <bascule@gmail.com> Thu Jan 14 15:08:43 2016 Tony Arcieri <bascule@gmail.com>
* Remove 512-bit DH group. It's affected by LogJam Attack. * Remove 512-bit DH group. It's affected by LogJam Attack.

View file

@ -1036,13 +1036,19 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
int sign = (flags&FPLUS) ? 1 : 0, zero = 0; int sign = (flags&FPLUS) ? 1 : 0, zero = 0;
long len, done = 0; long len, done = 0;
int prefix = 0; int prefix = 0;
if (!RB_TYPE_P(val, T_RATIONAL)) { if (FIXNUM_P(val) || RB_TYPE_P(val, T_BIGNUM)) {
den = INT2FIX(1);
num = val;
}
else if (RB_TYPE_P(val, T_RATIONAL)) {
den = rb_rational_den(val);
num = rb_rational_num(val);
}
else {
nextvalue = val; nextvalue = val;
goto float_value; goto float_value;
} }
if (!(flags&FPREC)) prec = default_float_precision; if (!(flags&FPREC)) prec = default_float_precision;
den = rb_rational_den(val);
num = rb_rational_num(val);
if (FIXNUM_P(num)) { if (FIXNUM_P(num)) {
if ((SIGNED_VALUE)num < 0) { if ((SIGNED_VALUE)num < 0) {
long n = -FIX2LONG(num); long n = -FIX2LONG(num);

View file

@ -149,6 +149,11 @@ class TestSprintf < Test::Unit::TestCase
assert_equal(" Inf", sprintf("% e", inf), '[ruby-dev:34002]') assert_equal(" Inf", sprintf("% e", inf), '[ruby-dev:34002]')
end end
def test_bignum
assert_match(/\A10{120}\.0+\z/, sprintf("%f", 100**60))
assert_match(/\A10{180}\.0+\z/, sprintf("%f", 1000**60))
end
def test_rational def test_rational
assert_match(/\A0\.10+\z/, sprintf("%.60f", 0.1r)) assert_match(/\A0\.10+\z/, sprintf("%.60f", 0.1r))
assert_match(/\A0\.010+\z/, sprintf("%.60f", 0.01r)) assert_match(/\A0\.010+\z/, sprintf("%.60f", 0.01r))