]> Cypherpunks repositories - gostls13.git/commitdiff
fix two (!) bugs printing floating point zero
authorRob Pike <r@golang.org>
Sat, 28 Jun 2008 19:54:24 +0000 (12:54 -0700)
committerRob Pike <r@golang.org>
Sat, 28 Jun 2008 19:54:24 +0000 (12:54 -0700)
SVN=125260

src/lib/fmt.go

index c12c39b267506ca522d0d82f7933980096b26085..632fb95210c8543d192b8f95482d17672d2d10c6 100644 (file)
@@ -347,6 +347,9 @@ func pow10(n int) double {
 }
 
 func unpack(a double) (negative bool, exp int, num double) {
+       if a == 0 {
+               return false, 0, 0.0
+       }
        neg := a < 0;
        if neg {
                a = -a;
@@ -380,9 +383,20 @@ func (f *Fmt) E(a double) *Fmt {
                prec = f.prec;
        }
        prec++;  // one digit left of decimal
+       var s string;
        // multiply by 10^prec to get decimal places; put decimal after first digit
-       g *= pow10(prec);
-       s := f.integer(int64(g + .5), 10, true, &ldigits);  // get the digits into a string
+       if g == 0 {
+               // doesn't work for zero - fake it
+               s = "000000000000000000000000000000000000000000000000000000000000";
+               if prec < len(s) {
+                       s = s[0:prec];
+               } else {
+                       prec = len(s);
+               }
+       } else {
+               g *= pow10(prec);
+               s = f.integer(int64(g + .5), 10, true, &ldigits);  // get the digits into a string
+       }
        s = s[0:1] + "." + s[1:prec];  // insert a decimal point
        // print exponent with leading 0 if appropriate.
        es := New().p(2).integer(int64(exp), 10, true, &ldigits);