mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* sprintf.c (rb_f_sprintf): support FZERO and FSPACE with NaN/Inf.
* test/ruby/test_sprintf.rb (test_nan, test_inf): add tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6495 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
29904b93f4
commit
a28006f959
3 changed files with 95 additions and 3 deletions
|
@ -1,3 +1,9 @@
|
|||
Tue Jun 22 23:35:43 2004 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||
|
||||
* sprintf.c (rb_f_sprintf): support FZERO and FSPACE with NaN/Inf.
|
||||
|
||||
* test/ruby/test_sprintf.rb (test_nan, test_inf): add tests.
|
||||
|
||||
Tue Jun 22 21:11:36 2004 Masaki Suketa <masaki.suketa@nifty.ne.jp>
|
||||
|
||||
* ext/win32ole/win32ole.c (OLE_FREE): should not call CoFreeUnuse-
|
||||
|
|
26
sprintf.c
26
sprintf.c
|
@ -698,7 +698,7 @@ rb_f_sprintf(argc, argv)
|
|||
expr = "Inf";
|
||||
}
|
||||
need = strlen(expr);
|
||||
if (fval < 0.0 || (flags & FPLUS))
|
||||
if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS))
|
||||
need++;
|
||||
if ((flags & FWIDTH) && need < width)
|
||||
need = width;
|
||||
|
@ -706,14 +706,34 @@ rb_f_sprintf(argc, argv)
|
|||
CHECK(need);
|
||||
sprintf(&buf[blen], "%*s", need, "");
|
||||
if (flags & FMINUS) {
|
||||
if (fval < 0.0)
|
||||
if (!isnan(fval) && fval < 0.0)
|
||||
buf[blen++] = '-';
|
||||
else if (flags & FPLUS)
|
||||
buf[blen++] = '+';
|
||||
else if (flags & FSPACE)
|
||||
blen++;
|
||||
strncpy(&buf[blen], expr, strlen(expr));
|
||||
}
|
||||
else if (flags & FZERO) {
|
||||
if (!isnan(fval) && fval < 0.0) {
|
||||
buf[blen++] = '-';
|
||||
need--;
|
||||
}
|
||||
else if (flags & FPLUS) {
|
||||
buf[blen++] = '+';
|
||||
need--;
|
||||
}
|
||||
else if (flags & FSPACE) {
|
||||
blen++;
|
||||
need--;
|
||||
}
|
||||
while (need-- - strlen(expr) > 0) {
|
||||
buf[blen++] = '0';
|
||||
}
|
||||
strncpy(&buf[blen], expr, strlen(expr));
|
||||
}
|
||||
else {
|
||||
if (fval < 0.0)
|
||||
if (!isnan(fval) && fval < 0.0)
|
||||
buf[blen + need - strlen(expr) - 1] = '-';
|
||||
else if (flags & FPLUS)
|
||||
buf[blen + need - strlen(expr) - 1] = '+';
|
||||
|
|
|
@ -72,4 +72,70 @@ class TestSprintf < Test::Unit::TestCase
|
|||
assert_equal(" +0010", sprintf("%+6.4b", 2))
|
||||
assert_equal(" -0001", sprintf("%+6.4b", -1))
|
||||
end
|
||||
|
||||
def test_nan
|
||||
nan = 0.0 / 0.0
|
||||
assert_equal("NaN", sprintf("%f", nan))
|
||||
assert_equal("NaN", sprintf("%-f", nan))
|
||||
assert_equal("+NaN", sprintf("%+f", nan))
|
||||
|
||||
assert_equal(" NaN", sprintf("%8f", nan))
|
||||
assert_equal("NaN ", sprintf("%-8f", nan))
|
||||
assert_equal(" +NaN", sprintf("%+8f", nan))
|
||||
|
||||
assert_equal("00000NaN", sprintf("%08f", nan))
|
||||
assert_equal("NaN ", sprintf("%-08f", nan))
|
||||
assert_equal("+0000NaN", sprintf("%+08f", nan))
|
||||
|
||||
assert_equal(" NaN", sprintf("% 8f", nan))
|
||||
assert_equal(" NaN ", sprintf("%- 8f", nan))
|
||||
assert_equal(" +NaN", sprintf("%+ 8f", nan))
|
||||
|
||||
assert_equal(" 0000NaN", sprintf("% 08f", nan))
|
||||
assert_equal(" NaN ", sprintf("%- 08f", nan))
|
||||
assert_equal("+0000NaN", sprintf("%+ 08f", nan))
|
||||
end
|
||||
|
||||
def test_inf
|
||||
inf = 1.0 / 0.0
|
||||
assert_equal("Inf", sprintf("%f", inf))
|
||||
assert_equal("Inf", sprintf("%-f", inf))
|
||||
assert_equal("+Inf", sprintf("%+f", inf))
|
||||
|
||||
assert_equal(" Inf", sprintf("%8f", inf))
|
||||
assert_equal("Inf ", sprintf("%-8f", inf))
|
||||
assert_equal(" +Inf", sprintf("%+8f", inf))
|
||||
|
||||
assert_equal("00000Inf", sprintf("%08f", inf))
|
||||
assert_equal("Inf ", sprintf("%-08f", inf))
|
||||
assert_equal("+0000Inf", sprintf("%+08f", inf))
|
||||
|
||||
assert_equal(" Inf", sprintf("% 8f", inf))
|
||||
assert_equal(" Inf ", sprintf("%- 8f", inf))
|
||||
assert_equal(" +Inf", sprintf("%+ 8f", inf))
|
||||
|
||||
assert_equal(" 0000Inf", sprintf("% 08f", inf))
|
||||
assert_equal(" Inf ", sprintf("%- 08f", inf))
|
||||
assert_equal("+0000Inf", sprintf("%+ 08f", inf))
|
||||
|
||||
assert_equal("-Inf", sprintf("%f", -inf))
|
||||
assert_equal("-Inf", sprintf("%-f", -inf))
|
||||
assert_equal("-Inf", sprintf("%+f", -inf))
|
||||
|
||||
assert_equal(" -Inf", sprintf("%8f", -inf))
|
||||
assert_equal("-Inf ", sprintf("%-8f", -inf))
|
||||
assert_equal(" -Inf", sprintf("%+8f", -inf))
|
||||
|
||||
assert_equal("-0000Inf", sprintf("%08f", -inf))
|
||||
assert_equal("-Inf ", sprintf("%-08f", -inf))
|
||||
assert_equal("-0000Inf", sprintf("%+08f", -inf))
|
||||
|
||||
assert_equal(" -Inf", sprintf("% 8f", -inf))
|
||||
assert_equal("-Inf ", sprintf("%- 8f", -inf))
|
||||
assert_equal(" -Inf", sprintf("%+ 8f", -inf))
|
||||
|
||||
assert_equal("-0000Inf", sprintf("% 08f", -inf))
|
||||
assert_equal("-Inf ", sprintf("%- 08f", -inf))
|
||||
assert_equal("-0000Inf", sprintf("%+ 08f", -inf))
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue