Exponentiation of real numbers

纵饮孤独 提交于 2020-01-07 05:43:22

问题


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.


回答1:


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)




回答2:


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



来源:https://stackoverflow.com/questions/18769584/exponentiation-of-real-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!