Author Topic: Exponential, Logarithmic, Power functions  (Read 5479 times)

0 Members and 1 Guest are viewing this topic.

Offline tk

  • Soldier
  • **
  • Posts: 235
Exponential, Logarithmic, Power functions
« on: March 22, 2009, 04:35:04 am »
Script Name: Exponential, Logarithmic, Power functions
Original Author(s): tk
Core Version: 2.6.3

function Power(Base: double; Exponent: integer): double;
function Root(Base: double; Exponent: integer): double;
function Cbrt(Base: double): double;

Code: (pascal) [Select]
const
  epsilon = 1e-6; // accuracy, number of decimal places (ie 1e-6 -> 6 decimal places accuracy)


function Power(Base: double; Exponent: integer): double;
var i: integer;
begin
  Result:=1;
  if Exponent = 0 then exit;
  for i:=1 to round(Abs(Exponent)) do Result:=Result*Base;
  if Exponent < 0 then Result:=1/Result;
end;

function Root(Base: double; Exponent: integer): double;
var r: double; neg_exp: boolean;
begin
  if Base < 0 then
    if Exponent mod 2 = 0 then exit;
  if Exponent < 0 then begin
    neg_exp:=true;
    Exponent:=-Exponent;
  end;
  Result:=1;
  repeat
    r:=Result;
    Result:=(Result*(Exponent-1)+Base/Power(Result,Exponent-1))/Exponent;
  until Abs(r-Result) < epsilon;
  if neg_exp then Result:=1/Result;
end;

function Cbrt(Base: double): double;
var r: double;
begin
  Result:=1;
  repeat
    r:=Result;
    Result:=(Result*2+Base/(Result*Result))/3;
  until Abs(r-Result) < epsilon;
end;



function Exp(X: double): double;
function Ln(X: double): double;
function Log(X: double): double;
function Power(Base: double; Exponent: double): double;
function Root(Base: double; Exponent: double): double;

Code: (pascal) [Select]
const
e = 2.71828182846; // value of e, don't touch
epsilon = 1e-6; // accuracy, number of decimal places (ie 1e-6 -> 6 decimal places accuracy)

function Exp(X: double): double;
var m, n: integer; r, fac, pow: double;
begin
m:=Round(Abs(X/10));
if m >= 2 then
X:=X/m;
Result:=1+X;
fac:=1;
pow:=X;
n:=2;
r:=1;
while r>epsilon do begin
fac:=fac*n;
pow:=pow*X;
n:=n+1;
r:=pow/fac;
Result:=Result+r;
end;
if m >= 2 then begin
pow:=Result;
for n:=2 to m do
Result:=Result*pow;
end;
end;

function Ln(X: double): double;
begin
Result:=LogN(e,X);
end;

function Log(X: double): double;
begin
Result:=LogN(10,X);
end;

function Power(Base: double; Exponent: double): double;
begin
Result:=Exp(LogN(e, Base)*Exponent);
end;

function Root(Base: double; Exponent: double): double;
begin
Result:=Exp(LogN(e, Base)/Exponent);
end;
« Last Edit: April 20, 2010, 08:08:03 am by tk »

DarkCrusade

  • Guest
Re: Exponential, Logarithmic, Power functions
« Reply #1 on: March 22, 2009, 08:29:49 am »
For what is this script for? :)

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Exponential, Logarithmic, Power functions
« Reply #2 on: March 22, 2009, 09:33:18 am »
Ready-to-use template. ;p If you was in need of any function like that you can C&P it into your script and give credits to tk. ;p
"My senses are so powerful that I can hear the blood pumping through your veins."

Offline Quantifier

  • Major
  • *
  • Posts: 70
Re: Exponential, Logarithmic, Power functions
« Reply #3 on: March 22, 2009, 12:10:47 pm »
Logarithmic and Exponential functions:

You do realize there is built-in:
function LogN(base, value:Extended):Extended;
in the server?

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Exponential, Logarithmic, Power functions
« Reply #4 on: March 22, 2009, 01:56:50 pm »
Since now. I tried Ln, Log, Log10, Lg, Exp names but they dont exist (I havent tried with logn). Ln, Exp exist in Pascal but they don't in the scripting core. What is more, Logn is not on any list with scriptcore functions.

Offline xmRipper

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 742
    • Personal
Re: Exponential, Logarithmic, Power functions
« Reply #5 on: March 22, 2009, 03:37:39 pm »
Hey tk, what's your next script? Where will you use these functions? =)
Co-Founder / CTO @ Macellan
Founder Turkish Soldat Community

Offline Quantifier

  • Major
  • *
  • Posts: 70
Re: Exponential, Logarithmic, Power functions
« Reply #6 on: March 23, 2009, 12:19:51 am »
Since now.

Actually since roughly year and a half ago, introduced in 2.6.2.

What is more, Logn is not on any list with scriptcore functions.

True. However, it works in scripts, and does the right thing.

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Exponential, Logarithmic, Power functions
« Reply #7 on: March 24, 2009, 01:07:04 pm »
Updated
Ln, Log10, Power and Root use now built-in LogN function.

Quote
Hey tk, what's your next script? Where will you use these functions? =)
I'm not going to use it soon, I made this bacause I needed to use power with float exponent in one of my scripts.
« Last Edit: March 24, 2009, 01:09:07 pm by tk »

Offline Mighty

  • Camper
  • ***
  • Posts: 276
Re: Exponential, Logarithmic, Power functions
« Reply #8 on: September 21, 2016, 11:33:45 am »
I need someone smarter than me to recreate these functions without using built-in logN, since it's not available anymore
xFire: macmil        e-mail: macekmil@gmail.com
My scripts: Accuracy Script       Flashbang       Punishments GUID
            CatchMe Gamemod       AntiFake
            CW System             AntiFakeGUID

Offline Savage

  • Soldier
  • **
  • Posts: 155
Re: Exponential, Logarithmic, Power functions
« Reply #9 on: September 22, 2016, 03:03:16 pm »
Can't you guys just load an unit to SC3? http://www.freepascal.org/docs-html/rtl/math/index-5.html

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: Exponential, Logarithmic, Power functions
« Reply #10 on: September 22, 2016, 10:35:54 pm »
It's already done, I'm kinda puzzled what's Mighty trying to accomplish.

EDIT: Ok, my bad, he wasn't aware of my changes.
« Last Edit: September 23, 2016, 02:19:29 am by Falcon` »
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Offline soldat-game

  • Camper
  • ***
  • Posts: 407