SRL Archive - OSR Help

nth root function returns weird


g0tp0t

2010-09-29 08:42:55

program new;
var x,sqr:double; n,i,iterations:integer;

function calc:double;
begin
x:= 0.5*((n-1)*x+sqr/(pow(x,n-1)))
result := x;
end;

begin
n := 2
sqr := 145
x := 99
iterations:= 10
for i := 1 to iterations do writeln(calc);
end.
The output was:

864
62208
4478976
322486272
23219011584
1671768834048
120367356051456
8.66644963570483E15
6.23984373770748E17
4.49268749114938E19
Successfully executed.

it should have had numbers approaching the square root of 145


EDIT: i added n := 2, that changed the output and it is accurate, to the acuracy of double var type. how would i get it to be even more accurate than just those decimal places.

Nava2

2010-09-29 08:46:29

(pow(x,n-1)))

should be:

(pow(x,1/(n-1)))
otherwise you have no roots even occurring. :p

g0tp0t

2010-09-29 14:20:36

oh, ill add the formula, so you can see what im working off of.

program new;
var x,sqr:double; n,i,iterations:integer;

function calc:double;
begin
x:= (1/n)*((n-1)*x+sqr/(pow(x,n-1)))
result := x;
end;

begin
n := 2
sqr := 1280
x := 99
iterations:= 10
for i := 1 to iterations do writeln(calc);
end.

result is:

Compiled succesfully in 63 ms.
55.9646464646464
39.4181142259158
35.9452473155634
35.777480983743
35.7770876421588
35.7770876399966
35.7770876399966
35.7770876399966
35.7770876399966
35.7770876399966
Successfully executed.

converges very fast, but i was haveing a divide by zero problem when n was not equal to 2. if n=3 then i get this output:

Compiled succesfully in 63 ms.
0
Error: divide by Zero at line 6

i went through the entire formula, it looks correct and i dont see any divide by 0 problems anywere. =\

here is the formula link:http://en.wikipedia.org/wiki/Nth_root_algorithm

Markus

2010-09-29 15:53:23

x is still 0 as it's not initialized. sqr / pow(0, 1) will lead to 0 as 0^1 = 0, so you'll divide by 0.

Nava2

2010-09-29 16:16:51

You are doing integer division.

g0tp0t

2010-09-29 18:01:46

how? i set x to 99 before i call calc, i even tested all vars by having the script wrtie all vars to debug box just before i called the calc function. every variable had a value

Edit: by changing n to double instead of int i got it to work =D

now to calc pi:shifty:

Edit2: how do i make the nmber even more accurate. like 200 decimal places

Nava2

2010-09-29 19:29:09

From: g0tp0t
how? i set x to 99 before i call calc, i even tested all vars by having the script wrtie all vars to debug box just before i called the calc function. every variable had a value

Edit: by changing n to double instead of int i got it to work =D

now to calc pi:shifty:

Edit2: how do i make the nmber even more accurate. like 200 decimal places


Not easily.

Yakman

2010-09-29 22:08:28


function nth_root(x, n: double): double;
begin
Result:= exp(ln(x) / n);
end;

g0tp0t

2010-09-30 13:39:38

im getting the divide by 0 error again, but all the vars are extended.
program MathStuffs;

function Estimate_Root(n,x:extended):extended;
var est:extended;
begin
est :=x/n
while abs(pow(est,n)-x) > (x*0.05) do
begin
if pow(est,n) > x then est := est-(x*0.001);
if pow(est,n) < x then est := est+(x*0.001);
end;
while abs(pow(est,n)-x) > 200 do
begin
if pow(est,n) > x then est := est-100;
if pow(est,n) < x then est := est+100;
end;
result := est;
end;


Function Nth_Root(n,x:extended;acc:integer):extended;
var est:extended;i:integer;
begin
est := Estimate_Root(n,x);
while abs(pow(est,n)-x) > 1/pow(10,acc) do
begin
est := (1/n)*((n-1)*est+x/(pow(est,n-1)));
i := i+1
if i > 50 then break;
end;
result:= est;
end;


begin
Nth_Root(3,
end.

Error:

[Error] (38:1): Syntax error at line 37
Compiling failed.

Naum

2010-09-30 13:51:21

Nth_Root(3,
You need to finish off the parameters and close the bracket

g0tp0t

2010-09-30 13:54:28

yup, that was just a typo, but not the problem. i did that before copying but after running the script.