|
Exponentation and power function in Pascal and Cpp
|
|
01-09-2010, 12:47 AM
(This post was last modified: 01-09-2010 12:59 AM by drdebcol.)
Post: #1
|
|||
|
|||
|
Exponentation and power function in Pascal and Cpp
There are loads of tasks that you need to do with exponentation. More about exponentation you have here :
http://en.wikipedia.org/wiki/Exponentation I was working with Turbo Pascal for Windows that has no "math" library (Free Pascal has it). But what if you don't have "math" lib in Pascal or "math.h" in C or C++ and you need to calculate a^n. Well i was trying to find alternatives. If you have two positive integer numbers and you need to calculate a^n, well that is easy. First to say it mathematically : ![]() If there are two positive integers (including zero) and you do exponentation on them you will get and integer that is in set of positive integer numbers. Okay now let's see special cases. What if your number is is zero (a=0) and you need to calculate 0^n, than you need to return zero. So 0^n=0 and n>0. Now another case. What if your exponent is zero (n=0), well than you need to return 1. Mathematically : ![]() And proof : ![]() And now special extra case, which you probably know is if you have exponent and number equal to zero (a=0 and n=0). For this case mathematical explanation above is not right. That is extra case. The best way is that your algorithm return one in this case. But if you want to go further into problem you have it here : http://en.wikipedia.org/wiki/Exponentati...zero_power That would be mathematical explanation and now it is easy to write code for that. I will post Pascal and C or C++ functions for this. In C or C++ i used unsigned int for this because it is the nearest to set of positive numbers. Pascal power function : Code: function power(a,n:integer):integer;Code: unsigned int power(unsigned int a,unsigned int n) Code in Pascal : Code: function sqr(n:integer):integer;Code: unsigned int sqr(unsigned int n) Okay all of that was because of integer numbers exponentation. Now if we want to make real power() function which will work with real numbers and calculate for example (3.5^-1.5) Than we need to use more complex formula. The good thing is that there is very structured formula for that : ![]() Okay now let's explain it. Basically you have exp() function inside which does not anything different than algorithm here : http://www.pro9ramming.com/exponent-of-m...t-792.html So exp() function returns exponent of mathematical constant e. If it is exp(1) it will return e^1=e which equals : Code: 2.71828 18284 59045 23536 . . .And ln() function is natural logarithm. More about it you have here : http://en.wikipedia.org/wiki/Natural_logarithm ln() function is basically exp()^(-1), because : Code: exp(ln(n))=n;![]() That is everything you need to know about this. Now let's pass to making an ultimate power function. So power recursive function in Pascal with errors when you enter something that function can not calculate is : Code: function power(a,n:real):real;http://en.wikipedia.org/wiki/Logarithms That would be everything about this subject of exponentation. I like when it is mathematically explained. Hope that you will use this function ! "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
01-10-2010, 04:11 AM
Post: #2
|
|||
|
|||
|
RE: Exponentation and power function in Pascal and Cpp
Nice work that pretty much covers what the power function does under the hood.
|
|||
|
01-10-2010, 07:12 AM
Post: #3
|
|||
|
|||
RE: Exponentation and power function in Pascal and Cpp
(01-10-2010 04:11 AM)codecaine Wrote: Nice work that pretty much covers what the power function does under the hood.Yeah like you say "under the hood", well i just wanted to explain everything. People must know what they use ! "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
01-10-2010, 11:04 PM
Post: #4
|
|||
|
|||
|
RE: Exponentation and power function in Pascal and Cpp
good and short algorithms for those,can you make your own symbol for for example exponentation?I mean can you declare your own sighn,2^3(=8)?can you declare the sign-^- for your function so that when you use ^ pascal knows that?as same as + - x / ... ?
Gholamreza Takhti who was he? find out |
|||
|
01-10-2010, 11:13 PM
Post: #5
|
|||
|
|||
|
RE: Exponentation and power function in Pascal and Cpp
I think that is not possible. You need to use function. But it is easy with the function too !
"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
01-11-2010, 09:24 PM
Post: #6
|
|||
|
|||
|
RE: Exponentation and power function in Pascal and Cpp
Nice code Bro.
|
|||
|
01-12-2010, 07:40 PM
Post: #7
|
|||
|
|||
|
RE: Exponentation and power function in Pascal and Cpp
"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
« Next Oldest | Next Newest »
|




![[Image: 32161_exponentation1.jpg]](http://myph.us/pics/32161_exponentation1.jpg)
![[Image: 32162_exponentation2.jpg]](http://myph.us/pics/32162_exponentation2.jpg)
![[Image: 32164_exponentation3.jpg]](http://myph.us/pics/32164_exponentation3.jpg)
![[Image: 32168_exponentation4.jpg]](http://myph.us/pics/32168_exponentation4.jpg)
![[Image: 32188_exponentation5.jpg]](http://myph.us/pics/32188_exponentation5.jpg)






