 |
Changeset 2602
- 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
| r2526 |
r2602 |
|
| 59 | 59 | ******************************************************************************/ |
|---|
| 60 | 60 | |
|---|
| 61 | | char[] toUtf8 (NumType d, uint decimals=6, bool scientific=false) |
|---|
| | 61 | char[] toUtf8 (NumType d, uint decimals=2, bool scientific=false) |
|---|
| 62 | 62 | { |
|---|
| 63 | 63 | char[64] tmp = void; |
|---|
| … | … | |
| 75 | 75 | ******************************************************************************/ |
|---|
| 76 | 76 | |
|---|
| 77 | | wchar[] toUtf16 (NumType d, uint decimals=6, bool scientific=false) |
|---|
| | 77 | wchar[] toUtf16 (NumType d, uint decimals=2, bool scientific=false) |
|---|
| 78 | 78 | { |
|---|
| 79 | 79 | wchar[64] tmp = void; |
|---|
| … | … | |
| 91 | 91 | ******************************************************************************/ |
|---|
| 92 | 92 | |
|---|
| 93 | | dchar[] toUtf32 (NumType d, uint decimals=6, bool scientific=false) |
|---|
| | 93 | dchar[] toUtf32 (NumType d, uint decimals=2, bool scientific=false) |
|---|
| 94 | 94 | { |
|---|
| 95 | 95 | dchar[64] tmp = void; |
|---|
| … | … | |
| 115 | 115 | T[] format(T) (T[] dst, NumType x, uint decimals = 2, bool scientific = false) |
|---|
| 116 | 116 | { |
|---|
| 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 |
|---|
| 121 | 135 | static int toDigit (inout NumType v, inout int count) |
|---|
| 122 | 136 | { |
|---|
| … | … | |
| 136 | 150 | } |
|---|
| 137 | 151 | |
|---|
| | 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 | |
|---|
| 138 | 160 | if (x !<>= x) |
|---|
| 139 | | return "nan"; |
|---|
| | 161 | return sign ? nan : nan[1..$]; |
|---|
| 140 | 162 | |
|---|
| 141 | 163 | 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; |
|---|
| 153 | 168 | |
|---|
| 154 | 169 | // don't scale if zero |
|---|
| … | … | |
| 247 | 262 | { |
|---|
| 248 | 263 | T c; |
|---|
| 249 | | T* p, |
|---|
| 250 | | end; |
|---|
| | 264 | T* p; |
|---|
| 251 | 265 | int exp; |
|---|
| 252 | 266 | bool sign; |
|---|
| … | … | |
| 264 | 278 | } |
|---|
| 265 | 279 | |
|---|
| 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; |
|---|
| 268 | 283 | |
|---|
| 269 | 284 | // read leading digits; note that leading |
|---|
| … | … | |
| 313 | 328 | else |
|---|
| 314 | 329 | // was it was nan instead? |
|---|
| 315 | | if (p is src.ptr) |
|---|
| | 330 | if (p is begin) |
|---|
| 316 | 331 | if (p[0..3] == "inf") |
|---|
| 317 | | p += 3, value = NumType.infinity; |
|---|
| | 332 | p += 3, value = value.infinity; |
|---|
| 318 | 333 | else |
|---|
| 319 | 334 | if (p[0..3] == "nan") |
|---|
| 320 | | p += 3, value = NumType.nan; |
|---|
| | 335 | p += 3, value = value.nan; |
|---|
| 321 | 336 | |
|---|
| 322 | 337 | // set parse length, and return value |
|---|
| 323 | 338 | if (ate) |
|---|
| 324 | 339 | *ate = p - src.ptr; |
|---|
| 325 | | return sign ? -value : value; |
|---|
| | 340 | |
|---|
| | 341 | if (sign) |
|---|
| | 342 | value = -value; |
|---|
| | 343 | return value; |
|---|
| 326 | 344 | } |
|---|
| 327 | 345 | |
|---|
| … | … | |
| 376 | 394 | char[64] tmp; |
|---|
| 377 | 395 | |
|---|
| | 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 | |
|---|
| 378 | 405 | assert (format (tmp, 3.14159, 6) == "3.141590"); |
|---|
| 379 | 406 | assert (format (tmp, 3.14159, 4) == "3.1416"); |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic