4

I want to compute 10 raised to the power minus m. In addition to use the math function pow(10, -m), is there any fast and efficient way to do that?

What I ask such a simple question to the c++ gurus from SO is that, as you know, just like base 2, 10 is also a special base. If some value n times the 10's power minus m, it is equivalent to move n's decimal point to the left m times. I think it must be a fast and efficient way to cope with.

GoldenLee
  • 737
  • 2
  • 13
  • 28
  • Is m an integer ? Is it always positive ? What is its maximum value ? Unless you list constraints such as this then you won't get an optimal solution. – Paul R May 27 '11 at 07:13
  • 2
    As some of the answers remarked, 9+1 is _not_ a special base, unless you happen to be a human being who, coincidentally, turns out to have that number of fingers. But your computer doesn't, so avoid base 10. It's only considerable to use it when dealing with data where somebody will actually read the numbers, but that's only interesting when there are no more than, like, 100 of them - and for such small amounts, performance doesn't really matter at all. – leftaroundabout May 27 '11 at 07:33
  • Do you need an integral solution or a floating-point one? Are `n` or `m` compile-time constants? – MSalters May 27 '11 at 08:25

8 Answers8

5

For floating point m, so long as your standard library implementation is well written, then pow will be efficient.

If m is an integer, and you hinted that it is, then you could use an array of pre calculated values.

You should only be worrying about this kind of thing if that routine is a bottleneck in your code. That is if the calls to that routine take a significant proportion of the total running time.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
4

Ten is not a special value on a binary machine, only two is. Use pow or exponentiation by squaring.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Yes. You're right! 10 is a special number in our everyday life, and in elementary school math class, but not in computer world. – GoldenLee May 27 '11 at 07:15
2

Unfortunately there is no fast and efficient way to calculate it using IEEE 754 floating point representation. The fastest way to get the result is to build a table for every value of m that you care about, and then just perform a lookup.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thank you. I agree with your suggestion to build a lookup table for a reasonable (not too big) m exponent. – GoldenLee May 27 '11 at 07:18
0

If there's a fast and efficient way to do it then I'm sure your CPU supports it, unless you're running on an embedded system in which case I'd hope that the pow(...) implementation is well written.

10 is special to us as most of us have ten fingers. Computers only have two digits, so 2 is special to them. :)

Will A
  • 24,780
  • 5
  • 50
  • 61
0

Use lookup table there cant be more than 1000 floats and especially if m is integer.

Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
0

IEEE 754 specifies a bunch of floating-point formats. Those that are in widespread use are binary, which means that base 10 isn't in any way special. This is contrary to your assumption that "10 is also a special base".

Interestingly, IEEE 754-2008 does add decimal floating-point formats (decimal32 and friends). However, I'm yet to come across hardware implementations of those.

In any case, you shouldn't be micro-optimizing your code before you've profiled it and established that this is indeed the bottleneck.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

If you could operate with log n instead of n for a significant time, you could save time because instead of

n = pow(10*n,-m)

you now have to calculate (using the definition l = log10(n))

l = -m*(l+1)
Peter G.
  • 14,786
  • 7
  • 57
  • 75
  • Dear Peter, thank you for your attention. I don't fully understand you solution to my question. I want to evaluate the value x = n * pow(10, -m). So, further suggestions will be highly appreciated! – GoldenLee May 27 '11 at 07:27
0

Just some more ideas which may lead you to further solutions...

  1. If you are interested in optimization on algorithm level you might look for a parallelized approach.

  2. You may speed up on system/archtectural level on using Ipp (for Intel Processors), or e.g. AMD Core Math Library (ACML) for AMD

  3. To use the power of your graphics card may be another way (e.g. CUDA for NVIDEA cards)

  4. I think it's also worth to look at OpenCL

Uhli
  • 323
  • 4
  • 19
  • Dear Uhli, Thank you for your kind suggestions. The edge-cutting techs such as parallel compuattion, Intel/LPP, CUDA and OPenCl, as you mentioned here, will be bound to improve the performance of the coputatioal-extensive applications. – GoldenLee May 27 '11 at 07:36