Exponentiation of real numbers

2019-09-05 02:03发布

I've come across an interesting exercise and it says: Implement a function x^y using standard functions of Turbo Pascal

For integer variables I can use for loop but I cannot understand how to work with real variables in this case.

I've been thinking about how to do this using Taylor series (can't understand how to use it for exponentiation) and I also found out that x^y = exp(y*log(x)) but there is only ln (natural logarithm) in standard functions...

PS I'm not asking you to write code: give me advise or link or something that will help to solve this problem, please.

2条回答
爷的心禁止访问
2楼-- · 2019-09-05 02:17

log(x) in your formula is natural logarithm, so you can use

x^y = exp(y*ln(x))

without any doubts. Both exp and ln are standard Turbo Pascal functions

(general formula is x^y = b^(y * base-b logarithm of x)

查看更多
Juvenile、少年°
3楼-- · 2019-09-05 02:25

log x base y = ln(x) / ln(y) = (log x base 10)/(log y base 10)

Following link has more information regarding logarithms. Check out the "Changing the Base" section. http://en.wikipedia.org/wiki/List_of_logarithmic_identities

You can change your base to natural logarithm and compute accordingly.

For x = 3.2, y = 2.5,
Say 3.2^2.5 = m
ln(m) = 2.5*ln(3.2)
Hence m = exp( 2.5 * ln(3.2) )

Actually for the above, you do not even need to change bases

查看更多
登录 后发表回答