0

What is a good way to create a double from a coefficient and a power of 10? Or in other words, what is a good way to create a double from the significand and the exponent of a written scientific notation value at runtime?

sFuller
  • 1,305
  • 2
  • 14
  • 23

1 Answers1

3

Not sure about numerical quality, but the obvious way would be:

double make_double(double coefficient, int power)
{
  return coefficient * pow(10.0, power);
}
unwind
  • 391,730
  • 64
  • 469
  • 606
  • If you are worried about numerical quality, then to play safe you could write a string and pass it to `strtod`. The precision of the coefficient might still cause problems, but there's not a lot you can do about inaccuracies that are already there in your input. – Steve Jessop Jan 28 '14 at 11:00