开发者

C++ function that do base 10 significant + exponent calculation from double

I need to represent numbers using the fol开发者_开发百科lowing structure. The purpose of this structure is not to lose the precision.

struct PreciseNumber
{
   long significand;
   int exponent;
}

Using this structure actual double value can be represented as value = significand * 10e^exponent.

Now I need to write utility function which can covert double into PreciseNumber.

Can you please let me know how to extract the exponent and significand from the double?


The prelude is somewhat flawed.

Firstly, barring any restrictions on storage space, conversion from a double to a base 10 significand-exponent form won't alter the precision in any form. To understand that, consider the following: any binary terminating fraction (like the one that forms the mantissa on a typical IEEE-754 float) can be written as a sum of negative powers of two. Each negative power of two is a terminating fraction itself, and hence it follows that their sum must be terminating as well.

However, the converse isn't necessarily true. For instance, 0.3 base 10 is equivalent to the non-terminating 0.01 0011 0011 0011 ... in base 2. Fitting this into a fixed size mantissa would blow some precision out of it (which is why 0.3 is actually stored as something that translates back to 0.29999999999999999.)

By this, we may assume that any precision that is intended by storing the numbers in decimal significand-exponent form is either lost, or isn't simply gained at all.

Of course, you might think of the apparent loss of accuracy generated by storing a decimal number as a float as loss in precision, in which case the Decimal32 and Decimal64 floating point formats may be of some interest -- check out http://en.wikipedia.org/wiki/Decimal64_floating-point_format.


This is a very difficult problem. You might want to see how much code it takes to implement a double-to-string conversion (for printf, e.g.). You might steal the code from gnu's implementation of gcc.


You cannot convert an "imprecise" double into a "precise" decimal number, because the required "precision" simply isn't there to begin with (otherwise why would you even want to convert?).

This is what happens if you try something like it in Java:

BigDecimal x = new BigDecimal(0.1);
System.out.println(x);

The output of the program is:

0.1000000000000000055511151231257827021181583404541015625


Well you're at less precision than a typical double. Your significand is a long giving you a range from -2 billion to +2 billion which is more than 9 but fewer than 10 digits of precision.

Here's an untested starting point on what you'd want to do for some simple math on PreciseNumbers

PreciseNumber Multiply(PreciseNumber lhs, PreciseNumber rhs)
{
  PreciseNumber ret;
  ret.s=lhs.s;
  ret.e=lhs.e;
  ret.s*=rhs.s;
  ret.e+=lhs.e;
  return ret;
}

PreciseNumber Add(PreciseNumber lhs, PreciseNumber rhs)
{
  PreciseNumber ret;
  ret.s=lhs.s;
  ret.e=lhs.e;
  ret.s+=(rhs.s*pow(10,rhs.e-lhs.e));
}

I didn't take care of any renormalization, but in both cases there are places where you have to worry about over/under flows and loss of precision. Just because you're doing it yourself rather than letting the computer take care of it in a double, doesn't meat the same pitfalls aren't there. The only way to not lose precision is to keep track of all of the digits.


Here's a very rough algorithm. I'll try to fill in some details later.

Take the log10 of the number to get the exponent. Multiply the double by 10^x if positive, or divide by 10^-x if negative.

Start with a significand of zero. Repeat the following 15 times, since a double contains 15 digits of significance:

  • Multiply the previous significand by 10.
  • Take the integer portion of the double, add it to the significand, and subtract it from the double.
  • Subtract 1 from the exponent.
  • Multiply the double by 10.

When finished, take the remaining double value and use it for rounding: if it's >= 5, add one to the significand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜