www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11400] New: Floating point numbers with fractional part printing alignment problem

http://d.puremagic.com/issues/show_bug.cgi?id=11400

           Summary: Floating point numbers with fractional part printing
                    alignment problem
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-10-31 05:54:29 PDT ---
void main() {
    import std.stdio;
    writefln("%2d", 5);
    writefln("%2d", -5);
    writefln("%3d", 5);
    writefln("%3d", 12);
    writefln("%3d", -12);
    writeln;
    writefln("%2.0f", 5.0);
    writefln("%2.0f", -5.0);
    writefln("%3.0f", 5.0);
    writefln("%3.0f", 12.0);
    writefln("%3.0f", -12.0);
    writeln;
    writefln("%2.1f", 5.0);
    writefln("%2.1f", -5.0);
    writefln("%3.1f", 5.0);
    writefln("%3.1f", 12.0);
    writefln("%3.1f", -12.0);
}


dmd 2.064beta3 gives:


 5
-5
  5
 12
-12

 5
-5
  5
 12
-12

5.0
-5.0
5.0
12.0
-12.0


But I expect:

 5
-5
  5
 12
-12

 5
-5
  5
 12
-12

 5.0  <== note here
-5.0
  5.0 <== note here
 12.0 <== note here
-12.0


Note that the problem is not present if you use ".0" when you print floating
point numbers.

This is useful to correct align a 2D matrix, to print it in a more readable
way. You can see in this program:

void main() {
    import std.stdio;
    double[][] m = [[4.243, 0.000, 0.000, 0.000],
                    [5.185, 6.566, 0.000, 0.000],
                    [12.728, 3.046, 1.650, 0.000],
                    [9.899, 1.625, 1.850, 1.393]];
    writefln("%(%(%2.3f %)\n%)", m);
}


That prints a table with misaligned columns:

4.243 0.000 0.000 0.000
5.185 6.566 0.000 0.000
12.728 3.046 1.650 0.000
9.899 1.625 1.850 1.393

that is a little harder to read than:

 4.243  0.000  0.000  0.000
 5.185  6.566  0.000  0.000
12.728  3.046  1.650  0.000
 9.899  1.625  1.850  1.393

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 31 2013