PDA

View Full Version : Exponentiation with real numbers.



nicostream
16-12-2012, 04:32 PM
Hi, everyone. I'm currently working on a little fun program. Basically it just does some computing with input values, that's not the point anyway. My problem is that I can't write the function that I need.

I need a function like this:

Function exp(x,y:real):real;

And it would return the value of x^y. Note that y is real, not an integer so a simple for loop will not do it, if I want to calculate 5.678^4.350. Basically it would be 5.678^(4350/1000)=5.678^(87/20) So that means (5.678^87) under the root with an exponent of 20. But I have no idea how would I calculate that. I've searched some forums but haven't been able to find a working solution yet.

Oh... FYI I have Free Pascal. Thanks, in advance!

Ñuño Martínez
16-12-2012, 05:06 PM
What you are asking for is not "exponentiation", but "power of". Exponentiation is to calculate the exponent of a number, that is, e^x or "the number e to the power x", being e a mathematical constant (wiki (http://en.wikipedia.org/wiki/E_%28mathematical_constant%29)).

Math unit has function Power (http://lazarus-ccr.sourceforge.net/fpcdoc/rtl/math/power.html) that does what you are looking for. If you need to build the function by your self, download RTL sources and look in them.

nicostream
16-12-2012, 05:30 PM
Yeah, sorry my english is not that great when it comes to the name of exact math functions. I just used the google translator. Thanks a lot, it should be working fine now :)

pitfiend
23-12-2012, 04:27 PM
there's a way to calculate power of a number using logarithms, natural ones, if you want to do x^y you wrote exp(ln(x)*y), works with integers, floating point, negatives.