Contact me sending an e-mail (antispam defense activated) |
Title: Raise A to the power of B Author: Sandro Tosi Last modified: 2005-04-03 The easiest way to obtain A raised to the power of B, A^B from now, is this: exp ( B * ln ( A ) ) let us explain why: 1. exp ( X ) means e^X, e is the Euler number, base of the natural logarithm, ln. 2. ln ( X ) is the number Y such that e^Y = X; 3. ln ( X^Y ) = Y * ln ( X ) 4. e^ln( X ) = X 5. exp ( B * ln ( A ) ) = e^( B * ln ( A ) ) = e^( ln ( A^B ) ) = A^B If this math is not clear, go and read your high school math books! Every programming language has these mathematical functions, maybe with a different name from the ones above. Usually they return floating point numbers, with no integer equivalence (it's often useless); but with A and B integer, A^B will be an integer too. In this situation, use the function ``floor'' (or something equivalent) that returns the integer part of a float number. |