Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 2602

Show
Ignore:
Timestamp:
10/03/07 00:02:16 (1 year ago)
Author:
kris
Message:

correctly supports -nan and -inf

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/text/convert/Float.d

    r2526 r2602  
    5959******************************************************************************/ 
    6060 
    61 char[] toUtf8 (NumType d, uint decimals=6, bool scientific=false) 
     61char[] toUtf8 (NumType d, uint decimals=2, bool scientific=false) 
    6262{ 
    6363        char[64] tmp = void; 
     
    7575******************************************************************************/ 
    7676 
    77 wchar[] toUtf16 (NumType d, uint decimals=6, bool scientific=false) 
     77wchar[] toUtf16 (NumType d, uint decimals=2, bool scientific=false) 
    7878{ 
    7979        wchar[64] tmp = void; 
     
    9191******************************************************************************/ 
    9292 
    93 dchar[] toUtf32 (NumType d, uint decimals=6, bool scientific=false) 
     93dchar[] toUtf32 (NumType d, uint decimals=2, bool scientific=false) 
    9494{ 
    9595        dchar[64] tmp = void; 
     
    115115T[] format(T) (T[] dst, NumType x, uint decimals = 2, bool scientific = false) 
    116116{ 
    117         assert (dst.length >= 32); 
    118  
    119         // function to strip digits from the 
    120         // left of a normalized base-10 number 
     117        static T[] inf = "-inf"; 
     118        static T[] nan = "-nan"; 
     119 
     120        // extract the sign bit 
     121        static bool signed (NumType x) 
     122        { 
     123                static if (NumType.sizeof is 4)  
     124                           return ((*cast(uint *)&x) & 0x8000_0000) != 0; 
     125                static if (NumType.sizeof is 8)  
     126                           return ((*cast(ulong *)&x) & 0x8000_0000_0000_0000) != 0; 
     127                static if (NumType.sizeof is 10)  
     128                          { 
     129                          auto pe = cast(ubyte *)&x; 
     130                          return (pe[9] & 0x80) != 0; 
     131                          } 
     132        } 
     133 
     134        // strip digits from the left of a normalized base-10 number 
    121135        static int toDigit (inout NumType v, inout int count) 
    122136        { 
     
    136150        } 
    137151 
     152        // sanity check 
     153        assert (dst.length >= 32); 
     154 
     155        // extract the sign 
     156        bool sign = signed (x); 
     157        if (sign) 
     158            x = -x; 
     159 
    138160        if (x !<>= x) 
    139             return "nan"
     161            return sign ? nan : nan[1..$]
    140162 
    141163        if (x is x.infinity) 
    142             return "inf"; 
    143  
    144         int exp; 
    145         bool sign; 
    146  
    147         // extract the sign 
    148         if (x < 0.0) 
    149            { 
    150            x = -x; 
    151            sign = true; 
    152            } 
     164            return sign ? inf : inf[1..$]; 
     165 
     166        // assume no exponent 
     167        int exp = 0; 
    153168 
    154169        // don't scale if zero 
     
    247262{ 
    248263        T               c; 
    249         T*              p, 
    250                         end; 
     264        T*              p; 
    251265        int             exp; 
    252266        bool            sign; 
     
    264278           } 
    265279 
    266         // set end check 
    267         end = src.ptr + src.length; 
     280        // set begin and end checks 
     281        auto begin = p; 
     282        auto end = src.ptr + src.length; 
    268283 
    269284        // read leading digits; note that leading 
     
    313328        else 
    314329           // was it was nan instead? 
    315            if (p is src.ptr
     330           if (p is begin
    316331               if (p[0..3] == "inf") 
    317                    p += 3, value = NumType.infinity; 
     332                   p += 3, value = value.infinity; 
    318333               else 
    319334                  if (p[0..3] == "nan") 
    320                       p += 3, value = NumType.nan; 
     335                      p += 3, value = value.nan; 
    321336 
    322337        // set parse length, and return value 
    323338        if (ate) 
    324339            *ate = p - src.ptr; 
    325         return sign ? -value : value;  
     340 
     341        if (sign) 
     342            value = -value; 
     343        return value; 
    326344} 
    327345 
     
    376394                char[64] tmp; 
    377395 
     396                auto f = parse ("nan"); 
     397                assert (format(tmp, f) == "nan"); 
     398                f = parse ("inf"); 
     399                assert (format(tmp, f) == "inf"); 
     400                f = parse ("-nan"); 
     401                assert (format(tmp, f) == "-nan"); 
     402                f = parse (" -inf"); 
     403                assert (format(tmp, f) == "-inf"); 
     404 
    378405                assert (format (tmp, 3.14159, 6) == "3.141590"); 
    379406                assert (format (tmp, 3.14159, 4) == "3.1416");