00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 module mango.convert.Double;
00041
00042 private import mango.convert.Atoi;
00043
00044
00045
00046
00047
00048
00049
00050
00051 struct DoubleTemplate(T)
00052 {
00053 static if (!is (T == char) && !is (T == wchar) && !is (T == dchar))
00054 pragma (msg, "Template type must be char, wchar, or dchar");
00055
00056
00057 private alias AtoiTemplate!(T) Atoi;
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 static final T[] format (T[] dst, double x, uint decimals = 6, bool scientific = false)
00072 in {
00073 assert (dst.length >= 32);
00074 }
00075 body
00076 {
00077
00078
00079 static int toDigit (inout double v, inout int count)
00080 {
00081 int digit;
00082
00083
00084 if (++count > 17)
00085 digit = 0;
00086 else
00087 {
00088
00089 digit = cast(int) v;
00090 v = (v - digit) * 10.0;
00091 }
00092 return digit + '0';
00093 }
00094
00095
00096 if (((cast(ushort*) &x)[3] & 0x7ff0) == 0x7ff0)
00097 if (*(cast(ulong*) &x) & 0x000f_ffff_ffff_ffff)
00098 return "nan";
00099 else
00100 return "inf";
00101
00102 int exp;
00103 bool sign;
00104
00105
00106 if (x < 0.0)
00107 {
00108 x = -x;
00109 sign = true;
00110 }
00111
00112
00113 if (x > 0.0)
00114 {
00115
00116 x += 0.5 / pow10 (decimals);
00117
00118
00119 frexp (x, &exp);
00120 exp = cast(int) (0.301029995664 * exp);
00121
00122
00123 int len = exp;
00124 if (exp < 0)
00125 {
00126 --exp;
00127 x *= pow10 (len = -exp);
00128 }
00129 else
00130 x /= pow10 (exp);
00131
00132
00133 if (len + 32 > dst.length)
00134 scientific = true;
00135 }
00136
00137 T* p = dst;
00138 int count = 0;
00139
00140
00141 if (sign)
00142 *p++ = '-';
00143
00144
00145 if (scientific)
00146 {
00147
00148 *p++ = toDigit (x, count);
00149 *p++ = '.';
00150
00151
00152 while (decimals-- > 0)
00153 *p++ = toDigit (x, count);
00154
00155
00156 if (exp)
00157 {
00158 *p++ = 'e';
00159 *p++ = (exp < 0) ? '-' : '+';
00160 if (exp < 0)
00161 exp = -exp;
00162
00163 if (exp >= 100)
00164 {
00165 *p++ = (exp/100) + '0';
00166 exp %= 100;
00167 }
00168
00169 *p++ = (exp/10) + '0';
00170 *p++ = (exp%10) + '0';
00171 }
00172 }
00173 else
00174 {
00175
00176 if (exp < 0)
00177 *p++ = '0';
00178 else
00179
00180 for (; exp >= 0; --exp)
00181 *p++ = toDigit (x, count);
00182
00183
00184 *p++ = '.';
00185
00186
00187 for (++exp; exp < 0 && decimals > 0; --decimals, ++exp)
00188 *p++ = '0';
00189
00190
00191
00192 while (decimals-- > 0)
00193 *p++ = toDigit (x, count);
00194 }
00195
00196 return dst [0..(p - dst.ptr)];
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 final static double parse (T[] src, uint* ate=null)
00210 {
00211 T c;
00212 T* p;
00213 int exp;
00214 bool sign;
00215 uint radix;
00216 double value = 0.0;
00217
00218
00219 c = *(p = src.ptr + Atoi.trim (src, sign, radix));
00220
00221
00222 if (radix != 10)
00223 {
00224 long v = Atoi.parse (src, radix, ate);
00225 return *cast(double*) &v;
00226 }
00227
00228
00229
00230 while (c >= '0' && c <= '9')
00231 {
00232 value = value * 10 + (c - '0');
00233 c = *++p;
00234 }
00235
00236
00237 if (c == '.')
00238 c = *++p;
00239
00240
00241
00242
00243
00244
00245
00246
00247 while (c >= '0' && c <= '9')
00248 {
00249 value = value * 10 + (c - '0');
00250 c = *++p;
00251 --exp;
00252 }
00253
00254
00255 if (value)
00256 {
00257
00258 if (c == 'e' || c == 'E')
00259 {
00260 uint eaten;
00261 exp += Atoi.parse (src[(p-src.ptr)+1..length], 0, &eaten);
00262 p += eaten;
00263 }
00264
00265
00266
00267 if (exp < 0)
00268 value /= pow10 (-exp);
00269 else
00270 value *= pow10 (exp);
00271 }
00272 else
00273
00274 if (p == src.ptr)
00275 if (p[0..3] == "inf")
00276 p += 3, value = double.infinity;
00277 else
00278 if (p[0..3] == "nan")
00279 p += 3, value = double.nan;
00280
00281
00282 if (ate)
00283 *ate = p - src.ptr;
00284 return sign ? -value : value;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 private static final double pow10 (uint exp)
00296 in {
00297 assert (exp < 512);
00298 }
00299 body
00300 {
00301 static double[] Powers =
00302 [
00303 1.0e1,
00304 1.0e2,
00305 1.0e4,
00306 1.0e8,
00307 1.0e16,
00308 1.0e32,
00309 1.0e64,
00310 1.0e128,
00311 1.0e256,
00312 ];
00313
00314 double mult = 1.0;
00315 foreach (double power; Powers)
00316 {
00317 if (exp & 1)
00318 mult *= power;
00319 if ((exp >>= 1) == 0)
00320 break;
00321 }
00322 return mult;
00323 }
00324 }
00325
00326
00327
00328
00329
00330
00331 alias DoubleTemplate!(char) Double;
00332
00333
00334
00335
00336
00337
00338
00339
00340 version (X86)
00341 {
00342 private static double frexp (double v, int* n)
00343 {
00344 asm {
00345 fld v;
00346 mov EDI, n;
00347 ftst;
00348 fstsw AX;
00349 sahf;
00350 jnz nonZero;
00351 fld st(0);
00352 jmp done;
00353
00354 nonZero: fxtract;
00355 fld1;
00356 fld1;
00357 fadd;
00358 fdiv;
00359 fxch;
00360 fld1;
00361 fadd;
00362 fistp [EDI];
00363 done:;
00364 }
00365 }
00366 }
00367 else
00368 extern (C) double frexp (double, int*);
00369
00370