double trouble

John C johnch_atms at hotmail.com
Sat Feb 25 03:42:39 PST 2006


I'm writing routines for numeric formatting and this involves round-tripping 
of floating-point numbers. I'm storing the integral and fractional parts in 
a string buffer, and in a separate variable storing the decimal point 
position, and these are converted back into a double when needed. 
Unfortunately the algorithm I'm using is too lossy and as a result a number 
like 2.78 (stored in the buffer as "278", with the decimal pos being 1) 
becomes 2.7799999999998.

I believe the problem is with my scaling code. Any ideas to increase the 
accuracy?

  // v is a numeric representation of the digits, eg 278.
  // exponent is either the exponent for scientific numbers, or the number 
of fractional digits, eg 2.
  double p10 = 10.0;
  int n = exponent;
  if (n < 0)
    n = -n;
  while (n) {
    if (n & 1) {
      if (exponent < 0)
        v /= p10;
      else
        v *= p10;
    }
    n >>= 1;
    p10 *= p10;
  }





More information about the Digitalmars-d mailing list