Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a power function is often computed as a logarithm?

I am studying some x86 ASM code and what the code really does, it's my understanding that a power function (x^y) internally works as a logarithm function. By internally I mean the CPU registers.

Why is this? What is the benefit? Is it a benefit that can be replicated and borrowed by other high level languages like C++?

like image 830
user2348816 Avatar asked Dec 04 '25 10:12

user2348816


2 Answers

You should take a look at this MATH,

What the answer says is that the log functions can be achieved through taylor series and a look-up table combined. If there is a look-up table, then it may be hashed and the look-up will be easier than pow which only can be obtained through computation.

so for xy you can write it as

y = log<sub>10</sub> x . 

or

y = (ln x)/(ln 10);

now if there is no log implementation for pow then it should be achieved through Addition chain exponentiation Wiki which requires runtime computation which might take longer than finding log.

EDIT : Thanks to @harold exponentiation can be performed more optimally using Exponentiation by squaring.

like image 186
Koushik Shetty Avatar answered Dec 07 '25 01:12

Koushik Shetty


See the answer to this question: How to: pow(real, real) in x86

Think back to your logarithm rules: the base of 2 cancels with log2(x) leaving just x^y.

Edit: We have an x86 instruction to calculate each of the components needed.

like image 37
Derek Avatar answered Dec 07 '25 01:12

Derek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!