]> Cypherpunks repositories - gostls13.git/commitdiff
handle Inf, NaN in float print
authorRuss Cox <rsc@golang.org>
Mon, 10 Nov 2008 22:54:10 +0000 (14:54 -0800)
committerRuss Cox <rsc@golang.org>
Mon, 10 Nov 2008 22:54:10 +0000 (14:54 -0800)
R=r
DELTA=48  (23 added, 14 deleted, 11 changed)
OCL=18707
CL=18922

src/runtime/print.c
src/runtime/runtime.c
src/runtime/runtime.h

index f50d308957c890fd220817b44557874997166d9d..de9cabfbb1414b0b25647930586ce4a77543c5f6 100644 (file)
@@ -52,6 +52,20 @@ sys·printfloat(float64 v)
        int32 e, s, i, n;
        float64 h;
 
+       if(isNaN(v)) {
+               sys·write(1, "NaN", 3);
+               return;
+       }
+       if(isInf(v, 0)) {
+               sys·write(1, "+Inf", 4);
+               return;
+       }
+       if(isInf(v, -1)) {
+               sys·write(1, "+Inf", 4);
+               return;
+       }
+
+
        n = 7;  // digits printed
        e = 0;  // exp
        s = 0;  // sign
@@ -103,27 +117,17 @@ sys·printfloat(float64 v)
                buf[n+3] = '-';
        }
 
-       buf[n+4] = (e/10) + '0';
-       buf[n+5] = (e%10) + '0';
-       sys·write(1, buf, n+6);
+       buf[n+4] = (e/100) + '0';
+       buf[n+5] = (e/10)%10 + '0';
+       buf[n+6] = (e%10) + '0';
+       sys·write(1, buf, n+7);
 }
 
 void
-sys·printint(int64 v)
+sys·printuint(uint64 v)
 {
        byte buf[100];
-       int32 i, s, big;
-
-       big = 0;
-       s = 0;
-       if(v < 0) {
-               v = -v;
-               s = 1;
-               if(v < 0) {
-                       big = 1;
-                       v--;
-               }
-       }
+       int32 i;
 
        for(i=nelem(buf)-1; i>0; i--) {
                buf[i] = v%10 + '0';
@@ -131,16 +135,19 @@ sys·printint(int64 v)
                        break;
                v = v/10;
        }
-       if(s){
-               i--;
-               buf[i] = '-';
-       }
-       if(big){
-               buf[nelem(buf)-1]++;
-       }
        sys·write(1, buf+i, nelem(buf)-i);
 }
 
+void
+sys·printint(int64 v)
+{
+       if(v < 0) {
+               sys·write(1, "-", 1);
+               v = -v;
+       }
+       sys·printuint(v);
+}
+
 void
 sys·printpointer(void *p)
 {
index a0d97dcda9049202310ce5bc1b5a080ba7cb9ad2..a8b23679506be900e415e4539c0d2594c8cf498f 100644 (file)
@@ -172,7 +172,7 @@ static      uint64  uvnan           = 0x7FF0000000000001ULL;
 static uint64  uvinf           = 0x7FF0000000000000ULL;
 static uint64  uvneginf        = 0xFFF0000000000000ULL;
 
-static int32
+bool
 isInf(float64 d, int32 sign)
 {
        uint64 x;
@@ -199,7 +199,7 @@ NaN(void)
        return *(float64*)&uvnan;
 }
 
-static int32
+bool
 isNaN(float64 d)
 {
        uint64 x;
index 30fa915b484717cc478939821944f5d743dd1615..74afa3aef1c4f579a0fd87879b34bc9eea4469eb 100644 (file)
@@ -288,6 +288,8 @@ void        sys·cmpstring(string, string, int32);
 void   sys·slicestring(string, int32, int32, string);
 void   sys·indexstring(string, int32, byte);
 void   sys·intstring(int64, string);
+bool   isInf(float64, int32);
+bool   isNaN(float64);
 
 /*
  * User go-called