Printf "[%08f]\n", 0.0/0.0

e$B0J2<$N$h$&$K!"e(Bprintf e$B$Ge(B NaN, Inf e$B$Ge(B 0 padding
e$B$,5/$3$j$^$9!#e(B

% ./ruby -ve ‘printf “[%08f]\n”, 0.0/0.0’
ruby 1.9.0 (2008-03-06 revision 15699) [i686-linux]
[00000NaN]
% ./ruby -e ‘printf “[%08f]\n”, 1.0/0.0’
[00000Inf]
% ./ruby -e ‘printf “[%08f]\n”, -1.0/0.0’
[-0000Inf]

e$B$7$+$7!"e(BC e$B$N5,3J$G$Oe(B NaN e$B$He(B Inf e$B$K4X$7$F$Oe(B 0
padding e$B$O5/$3e(B
e$B$j$^$;$s!#<B:]!"e(Bglibc e$B$G$O$=$&$J$j$^$9!#e(B

% uname -srv
Linux 2.6.18-5-686 #1 SMP Mon Dec 24 16:41:07 UTC 2007
% cat tst.c
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
printf("[%08f]\n", 0.0/0.0);
printf("[%08f]\n", 1.0/0.0);
printf("[%08f]\n", -1.0/0.0);
return 0;
}
% gcc tst.c
% ./a.out
[ nan]
[ inf]
[ -inf]

e$B$J$+$@$G$9!#e(B

At Thu, 6 Mar 2008 01:57:27 +0900,
Tanaka A. wrote in [ruby-dev:33994]:

e$B0J2<$N$h$&$K!"e(Bprintf e$B$Ge(B NaN, Inf e$B$Ge(B 0 padding e$B$,5/$3$j$^$9!#e(B

% ./ruby -ve ‘printf “[%08f]\n”, 0.0/0.0’
ruby 1.9.0 (2008-03-06 revision 15699) [i686-linux]
[00000NaN]
% ./ruby -e ‘printf “[%08f]\n”, 1.0/0.0’
[00000Inf]
% ./ruby -e ‘printf “[%08f]\n”, -1.0/0.0’
[-0000Inf]

e$B%F%9%H$b4V0c$C$F$$$k$H$$$&$3$H$G$7$g$&$+!#e(B

Index: sprintf.c

— sprintf.c (revision 15701)
+++ sprintf.c (working copy)
@@ -767,4 +767,5 @@ rb_str_format(int argc, const VALUE *arg
const char *expr;

  •    flags &= ~FZERO;
       if  (isnan(fval)) {
     expr = "NaN";
    

Index: test/ruby/test_sprintf.rb

— test/ruby/test_sprintf.rb (revision 15701)
+++ test/ruby/test_sprintf.rb (working copy)
@@ -88,7 +88,7 @@ class TestSprintf < Test::Unit::TestCase
assert_equal(" +NaN", sprintf("%+8f", nan))

  • assert_equal(“00000NaN”, sprintf("%08f", nan))
  • assert_equal(" NaN", sprintf("%08f", nan))
    assert_equal(“NaN “, sprintf(”%-08f”, nan))
  • assert_equal("+0000NaN", sprintf("%+08f", nan))
  • assert_equal(" +NaN", sprintf("%+08f", nan))

    assert_equal(" NaN", sprintf("% 8f", nan))
    @@ -96,7 +96,7 @@ class TestSprintf < Test::Unit::TestCase
    assert_equal(" +NaN", sprintf("%+ 8f", nan))

  • assert_equal(" 0000NaN", sprintf("% 08f", nan))
  • assert_equal(" NaN", sprintf("% 08f", nan))
    assert_equal(" NaN “, sprintf(”%- 08f", nan))
  • assert_equal("+0000NaN", sprintf("%+ 08f", nan))
  • assert_equal(" +NaN", sprintf("%+ 08f", nan))
    end

@@ -111,7 +111,7 @@ class TestSprintf < Test::Unit::TestCase
assert_equal(" +Inf", sprintf("%+8f", inf))

  • assert_equal(“00000Inf”, sprintf("%08f", inf))
  • assert_equal(" Inf", sprintf("%08f", inf))
    assert_equal(“Inf “, sprintf(”%-08f”, inf))
  • assert_equal("+0000Inf", sprintf("%+08f", inf))
  • assert_equal(" +Inf", sprintf("%+08f", inf))

    assert_equal(" Inf", sprintf("% 8f", inf))
    @@ -119,7 +119,7 @@ class TestSprintf < Test::Unit::TestCase
    assert_equal(" +Inf", sprintf("%+ 8f", inf))

  • assert_equal(" 0000Inf", sprintf("% 08f", inf))
  • assert_equal(" Inf", sprintf("% 08f", inf))
    assert_equal(" Inf “, sprintf(”%- 08f", inf))
  • assert_equal("+0000Inf", sprintf("%+ 08f", inf))
  • assert_equal(" +Inf", sprintf("%+ 08f", inf))

    assert_equal("-Inf", sprintf("%f", -inf))
    @@ -131,7 +131,7 @@ class TestSprintf < Test::Unit::TestCase
    assert_equal(" -Inf", sprintf("%+8f", -inf))

  • assert_equal("-0000Inf", sprintf("%08f", -inf))
  • assert_equal(" -Inf", sprintf("%08f", -inf))
    assert_equal("-Inf “, sprintf(”%-08f", -inf))
  • assert_equal("-0000Inf", sprintf("%+08f", -inf))
  • assert_equal(" -Inf", sprintf("%+08f", -inf))

    assert_equal(" -Inf", sprintf("% 8f", -inf))
    @@ -139,7 +139,7 @@ class TestSprintf < Test::Unit::TestCase
    assert_equal(" -Inf", sprintf("%+ 8f", -inf))

  • assert_equal("-0000Inf", sprintf("% 08f", -inf))
  • assert_equal(" -Inf", sprintf("% 08f", -inf))
    assert_equal("-Inf “, sprintf(”%- 08f", -inf))
  • assert_equal("-0000Inf", sprintf("%+ 08f", -inf))
  • assert_equal(" -Inf", sprintf("%+ 08f", -inf))
    assert_equal(’…f00000000’,
    sprintf("%x", -2**32), ‘[ruby-dev:32351]’)

In article [email protected],
Nobuyoshi N. [email protected] writes:

e$B%F%9%H$b4V0c$C$F$$$k$H$$$&$3$H$G$7$g$&$+!#e(B

e$B$?$V$s!#e(B

Inf e$B$de(B NaN e$B$Ke(B 0 e$B$,$D$/$HJQ$G$9$h$M!#e(B

e$B$^$?!";d$,D4$Y$?$N$Oe(B C99 e$B$Ne(B draft e$B$He(B JIS X 3010
e$B$Ne(B fprintfe$B$Ne(B
e$B9`$Ge(B

   0     For  d,  i,  o,  u,  x,  X, a, A, e, E, f, F, g, and G
         conversions, leading zeros (following  any  indication
         of  sign  or  base) are used to pad to the field width |
         rather than  performing  space  padding,  except  when |
         converting  an  infinity or NaN.  If the 0 and - flags
         both appear, the 0 flag is ignored.  For d, i,  o,  u,
         x, and X conversions, if a precision is specified, the
         0  flag  is  ignored.   For  other  conversions,   the
         behavior is undefined.

e$B$H$+e(B

   220When  applied  to  infinite and NaN values, the -, +, and
      space flag characters have their usual meaning; the # and
      0 flag characters have no effect.

e$B$H!"e(B0 (e$B$He(B #) e$B$Oe(B infinity e$B$He(B NaN
e$B$K$O8z$+$J$$$HL@3N$K=q$$$F$"e(B
e$B$j$^$9!#e(B
(e$B$=$l$KBP$7!"e(B-, +, space e$B$O8z$/$h$&$G$9!#e(BNaN
e$B$NId9f$O$h$/$oe(B
e$B$+$j$^$;$s$,!#e(B)