]> Cypherpunks repositories - gostls13.git/commitdiff
add primitive ftoa, dtoa to strings library.
authorRob Pike <r@golang.org>
Mon, 20 Oct 2008 20:53:20 +0000 (13:53 -0700)
committerRob Pike <r@golang.org>
Mon, 20 Oct 2008 20:53:20 +0000 (13:53 -0700)
R=rsc
DELTA=72  (69 added, 0 deleted, 3 changed)
OCL=17478
CL=17480

src/lib/strings.go
test/stringslib.go

index 2f3b630dfab324f76b77a25abbbe8a3706cf3030..98650b5475fc546c3324c6678382b208d755f70e 100644 (file)
@@ -168,7 +168,7 @@ export func atoi(s string) (i int, ok bool) {
        return i, okok
 }
 
-export func itol(i int64) string {
+export func ltoa(i int64) string {
        if i == 0 {
                return "0"
        }
@@ -197,5 +197,70 @@ export func itol(i int64) string {
 }
 
 export func itoa(i int) string {
-       return itol(int64(i));
+       return ltoa(int64(i));
+}
+
+// Convert float64 to string.  No control over format.
+// Result not great; only useful for simple debugging.
+export func dtoa(v float64) string {
+       var buf [20]byte;
+
+       const n = 7;    // digits printed
+       e := 0; // exp
+       var sign byte = '+';
+       if(v != 0) {
+               // sign
+               if(v < 0) {
+                       v = -v;
+                       sign = '-';
+               }
+
+               // normalize
+               for v >= 10 {
+                       e++;
+                       v /= 10;
+               }
+               for v < 1 {
+                       e--;
+                       v *= 10;
+               }
+
+               // round
+               var h float64 = 5;
+               for i := 0; i < n; i++ {
+                       h /= 10;
+               }
+               v += h;
+               if v >= 10 {
+                       e++;
+                       v /= 10;
+               }
+       }
+
+       // format +d.dddd+edd
+       buf[0] = sign;
+       for i := 0; i < n; i++ {
+               s := int64(v);
+               buf[i+2] = byte(s)+'0';
+               v -= float64(s);
+               v *= 10;
+       }
+       buf[1] = buf[2];
+       buf[2] = '.';
+
+       buf[n+2] = 'e';
+       buf[n+3] = '+';
+       if e < 0 {
+               e = -e;
+               buf[n+3] = '-';
+       }
+
+       // TODO: exponents > 99?
+       buf[n+4] = byte((e/10) + '0');
+       buf[n+5] = byte((e%10) + '0');
+       return string(buf)[0:n+6];      // TODO: should be able to slice buf
+}
+
+export func ftoa(v float) string {
+       return dtoa(float64(v));
 }
index a6263709834f46c880cc7e872cf7e405e03c9f8f..cb288b813a88e6d803a5ab30dbaba465343550f9 100644 (file)
@@ -98,11 +98,15 @@ func main() {
                n, ok = strings.atoi("20ba"); if n != 0 || ok { panic("atoi 20ba") }
                n, ok = strings.atoi("hello"); if n != 0 || ok { panic("hello") }
        }
+
+       if strings.ftoa(1e6) != "+1.000000e+06" { panic("ftoa 1e6") }
+       if strings.ftoa(-1e-6) != "-1.000000e-06" { panic("ftoa -1e-6") }
+       if strings.ftoa(-1.234567e-6) != "-1.234567e-06" { panic("ftoa -1.234567e-6") }
        
        if itoa(0) != "0" { panic("itoa 0") }
        if itoa(12345) != "12345" { panic("itoa 12345") }
        if itoa(-1<<31) != "-2147483648" { panic("itoa 1<<31") }
-       
+
        // should work if int == int64: is there some way to know?
        // if itoa(-1<<63) != "-9223372036854775808" { panic("itoa 1<<63") }
 }