diff options
author | Sigrid Solveig Haflínudóttir <sigrid@ftrv.se> | 2023-03-09 11:29:12 +0000 |
---|---|---|
committer | Sigrid Solveig Haflínudóttir <sigrid@ftrv.se> | 2023-03-09 11:29:12 +0000 |
commit | cc23b9bf4998d93b8de040f7a5a95a1f3b525b3b (patch) | |
tree | dc84370b5e7f890740a2d6674cd0916d88b209f4 /sys/src/libc/fmt | |
parent | 8738adf91dfbd656908a578fd567a811536ce557 (diff) |
print, strtod: fix -0 and -NaN, respect verb flags when formatting
Diffstat (limited to 'sys/src/libc/fmt')
-rw-r--r-- | sys/src/libc/fmt/fltfmt.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/sys/src/libc/fmt/fltfmt.c b/sys/src/libc/fmt/fltfmt.c index b0fcc3938..77638282c 100644 --- a/sys/src/libc/fmt/fltfmt.c +++ b/sys/src/libc/fmt/fltfmt.c @@ -11,6 +11,8 @@ enum NEXP10 = 308, }; +#define SIGN (1<<31) + static int xadd(char *a, int n, int v) { @@ -55,34 +57,26 @@ static void xdtoa(Fmt *fmt, char *s2, double f) { char s1[NSIGNIF+10]; + FPdbleword a; double g, h; int e, d, i, n; - int c1, c2, c3, c4, ucase, sign, chr, prec; + int c1, c2, c3, c4, ucase, sign, chr, prec, isnan, isinf; prec = FDEFLT; if(fmt->flags & FmtPrec) prec = fmt->prec; if(prec > FDIGIT) prec = FDIGIT; - if(isNaN(f)) { - strcpy(s2, "NaN"); - return; - } - if(isInf(f, 1)) { - strcpy(s2, "+Inf"); - return; - } - if(isInf(f, -1)) { - strcpy(s2, "-Inf"); - return; - } - sign = 0; - if(f < 0) { - f = -f; - sign++; - } + a.x = f; + sign = a.hi & SIGN; ucase = 0; chr = fmt->r; + isnan = isNaN(f); + isinf = isInf(f, 0); + if(isnan || isinf) + goto found; + if(sign) + f = -f; if(isupper(chr)) { ucase = 1; chr = tolower(chr); @@ -180,6 +174,15 @@ found: else if(fmt->flags & FmtSpace) s2[d++] = ' '; + if(isnan){ + strcpy(s2+d, "NaN"); + return; + } + if(isinf){ + strcpy(s2+d, "Inf"); + return; + } + /* * copy into final place * c1 digits of leading '0' |