~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/dtoa.cc

  • Committer: Monty Taylor
  • Date: 2008-12-06 07:22:02 UTC
  • mto: (656.1.7 devel) (660.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206072202-2g25o9doqr1l8euu
OOOh doggie. Got rid of my_alloca.

Show diffs side-by-side

added added

removed removed

Lines of Context:
90
90
  char *res, *src, *end, *dst= to;
91
91
  char buf[DTOA_BUFF_SIZE];
92
92
  assert(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
93
 
 
 
93
  
94
94
  res= dtoa(x, 5, precision, &decpt, &sign, &end, buf, sizeof(buf));
95
95
 
96
96
  if (decpt == DTOA_OVERFLOW)
130
130
  {
131
131
    if (len <= decpt)
132
132
      *dst++= '.';
133
 
 
 
133
    
134
134
    for (i= precision - cmax(0, (len - decpt)); i > 0; i--)
135
135
      *dst++= '0';
136
136
  }
137
 
 
 
137
  
138
138
  *dst= '\0';
139
139
  if (error != NULL)
140
140
    *error= false;
197
197
     my_gcvt(55, ..., 1, ...);
198
198
 
199
199
   We do our best to minimize such cases by:
200
 
 
 
200
   
201
201
   - passing to dtoa() the field width as the number of significant digits
202
 
 
 
202
   
203
203
   - removing the sign of the number early (and decreasing the width before
204
204
     passing it to dtoa())
205
 
 
 
205
   
206
206
   - choosing the proper format to preserve the most number of significant
207
207
     digits.
208
208
*/
215
215
  char buf[DTOA_BUFF_SIZE];
216
216
  bool have_space, force_e_format;
217
217
  assert(width > 0 && to != NULL);
218
 
 
 
218
  
219
219
  /* We want to remove '-' from equations early */
220
220
  if (x < 0.)
221
221
    width--;
244
244
     to count it here.
245
245
   */
246
246
  exp_len= 1 + (decpt >= 101 || decpt <= -99) + (decpt >= 11 || decpt <= -9);
247
 
 
 
247
  
248
248
  /*
249
249
     Do we have enough space for all digits in the 'f' format?
250
250
     Let 'len' be the number of significant digits returned by dtoa,
297
297
       ((decpt <= width && (decpt >= -1 || (decpt == -2 &&
298
298
                                            (len > 1 || !force_e_format)))) &&
299
299
         !force_e_format)) &&
300
 
 
 
300
      
301
301
       /*
302
302
         Use the 'e' format in some cases even if we have enough space for the
303
303
         'f' one. See comment for MAX_DECPT_FOR_F_FORMAT.
319
319
          *error= true;
320
320
        width= decpt;
321
321
      }
322
 
 
 
322
      
323
323
      /*
324
324
        We want to truncate (len - width) least significant digits after the
325
325
        decimal point. For this we are calling dtoa with mode=5, passing the
337
337
      *dst++= '0';
338
338
      goto end;
339
339
    }
340
 
 
 
340
    
341
341
    /*
342
342
      At this point we are sure we have enough space to put all digits
343
343
      returned by dtoa
386
386
        *error= true;
387
387
      width= 0;
388
388
    }
389
 
 
 
389
      
390
390
    /* Do we have to truncate any digits? */
391
391
    if (width < len)
392
392
    {
451
451
                  rejected character.
452
452
   @param error   Upon return is set to EOVERFLOW in case of underflow or
453
453
                  overflow.
454
 
 
 
454
   
455
455
   @return        The resulting double value. In case of underflow, 0.0 is
456
456
                  returned. In case overflow, signed DBL_MAX is returned.
457
457
*/
770
770
  b= Balloc(k, alloc);
771
771
  b->p.x[0]= y9;
772
772
  b->wds= 1;
773
 
 
 
773
  
774
774
  i= 9;
775
775
  if (9 < nd0)
776
776
  {
1267
1267
  9007199254740992.*9007199254740992.e-256 /* = 2^106 * 1e-53 */
1268
1268
};
1269
1269
/*
1270
 
  The factor of 2^53 in tinytens[4] helps us avoid setting the underflow
 
1270
  The factor of 2^53 in tinytens[4] helps us avoid setting the underflow 
1271
1271
  flag unnecessarily.  It leads to a song and dance at the end of strtod.
1272
1272
*/
1273
1273
#define Scale_Bit 0x10
1275
1275
 
1276
1276
/*
1277
1277
  strtod for IEEE--arithmetic machines.
1278
 
 
 
1278
 
1279
1279
  This strtod returns a nearest machine number to the input decimal
1280
1280
  string (or sets errno to EOVERFLOW). Ties are broken by the IEEE round-even
1281
1281
  rule.
1282
 
 
 
1282
 
1283
1283
  Inspired loosely by William D. Clinger's paper "How to Read Floating
1284
1284
  Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
1285
 
 
 
1285
 
1286
1286
  Modifications:
1287
 
 
 
1287
 
1288
1288
   1. We only require IEEE (not IEEE double-extended).
1289
1289
   2. We get by with floating-point arithmetic in a case that
1290
1290
     Clinger missed -- when we're computing d * 10^n
1347
1347
 break2:
1348
1348
  if (s >= end)
1349
1349
    goto ret0;
1350
 
 
 
1350
  
1351
1351
  if (*s == '0')
1352
1352
  {
1353
1353
    nz0= 1;
2013
2013
  double d2, ds, eps;
2014
2014
  char *s, *s0;
2015
2015
  Stack_alloc alloc;
2016
 
 
 
2016
  
2017
2017
  alloc.begin= alloc.free= buf;
2018
2018
  alloc.end= buf + buf_size;
2019
2019
  memset(alloc.freelist, 0, sizeof(alloc.freelist));
2039
2039
      *rve= res + 1;
2040
2040
    return res;
2041
2041
  }
2042
 
 
 
2042
  
2043
2043
 
2044
2044
  b= d2b(dval(d), &be, &bbits, &alloc);
2045
2045
  if ((i= (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))))
2053
2053
      log10(x)      =  log(x) / log(10)
2054
2054
                   ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2055
2055
      log10(d)= (i-Bias)*log(2)/log(10) + log10(d2)
2056
 
 
 
2056
     
2057
2057
      This suggests computing an approximation k to log10(d) by
2058
 
 
 
2058
     
2059
2059
      k= (i - Bias)*0.301029995663981
2060
2060
           + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2061
 
 
 
2061
     
2062
2062
      We want k to be too large rather than too small.
2063
2063
      The error in the first-order Taylor series approximation
2064
2064
      is in our favor, so we just round up the constant enough
2371
2371
  /*
2372
2372
    Arrange for convenient computation of quotients:
2373
2373
    shift left if necessary so divisor has 4 leading 0 bits.
2374
 
 
 
2374
    
2375
2375
    Perhaps we should just compute leading 28 bits of S once
2376
2376
    a nd for all and pass them and a shift to quorem, so it
2377
2377
    can do shifts and ors to compute the numerator for q.