Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with my small program - invalid floting operation?

Status
Not open for further replies.

noze2000

Programmer
Aug 3, 2004
1
DK
I have a problem with my code. it compiles fine on TP7.0, but gives an error when running..?

Code:
program Nis

{$N+};

Uses
	Dos;

Const
	n=333  ;

Type
	Tritype=Array[1..3,-n..n] Of Extended;
	mtype=Array[-n..n] Of Extended;

Var
tridiag: Tritype;
slut,di,fi : mtype;
i,j,i1,i2,j_mcm : Integer;
s0,k,rr,s2 : Extended;
dt,ksi_nul,dksi,ksi_max,ksi_min,ksi_n_nul : Extended  ;

Function ksi(a,b:Integer):Extended   ;
Begin
	ksi:=ksi_nul+b*dksi-a/n;
	end;

Function max(a,b:Extended):Extended   ;
Begin
If a>b Then
	Begin
		max:=a;
	End
Else
	Begin
		max:=a
	End;
End;

Procedure Slut_rand;
Var
j1: integer;
Begin
For j1:=n DownTo -n Do
	Begin
	Slut[j1]:=Max(0,-ksi(n,j1));
	di[j1]:=slut[j1];
	End;
End;

Function a_value(a,b:Integer):Extended;
Var
tmp:Extended        ;
	Begin
	tmp:=ksi(a,b);
	a_value:=1*(rr*tmp*dt)/(2*dksi)-(0.5*s2*sqr(dksi));
	End;

Function b_value(a,b:Integer):Extended;
Var
tmp:Extended        ;
	Begin
	tmp:=ksi(a,b);
	b_value:=1 - rr * dt + s2 * sqr(tmp) * dt / sqrt(dksi);
	End;

Function c_value(a,b:Integer):Extended;
Var
tmp:Extended        ;
	Begin
	tmp:=ksi(a,b);
	c_value:=1 * ( rr * tmp)* dt/(s2*dksi) - (0.5 * s2 * sqr(tmp) * dt) / sqr(dksi);
	End;

Procedure tridiagonal(a:Integer);
Var
j1:Integer;
	Begin
	For j1:=n DownTo -n Do
	Begin
		tridiag[1,j1]:=a_value(a,j1);
		tridiag[2,j1]:=b_value(a,j1);
		tridiag[3,j1]:=c_value(a,j1);
	End;
End;

Procedure Gauss;
Var
g,e,f:Extended;
j1:Integer;
	Begin
	 g := tridiag[2,n];
	 e := tridiag[3,n]/g;
	 f := di[n] / g;
	 tridiag[2,n]:=g;
	 tridiag[3,n]:=e;
	 fi[n]:=f;
	 For j1:=(n-1) DownTo -(n-1) Do
		Begin
		g := tridiag[2,j1] - tridiag[1,j1] * tridiag[3,j1+1];
		e := tridiag[3,j1] / g;
		f := (di[j1]-tridiag[1,j1] * fi[j1+1]) / g;
		tridiag[2,j1] := g;
		tridiag[3,j1] := e;
		fi[j1] := f;
		End;
	g := tridiag[2,-n] - tridiag[1,-n] * tridiag[3,-n+1];
	tridiag[2,-n] := g;
	f := (di[-n] - tridiag[1,-n] * fi[-n+1] )/ g;
	fi[0] := f;
End;

Procedure Back_sub;
Var
j1:Integer;
Begin
	di[-n]:=fi[-n];
	For j1:=(1-n) To n Do
	Begin
		di[j1]:=fi[j1]-tridiag[3,j1]*di[j1-1];
	End;
End;

Procedure Initialiser;
Begin
	s0:=100;
	k:=100;
	rr:=0.1;
	s2:=sqr(0.5);
	dt:=1/n;
	ksi_nul:=k/s0;
	ksi_n_nul:=ksi_nul-1;
	if (ksi_nul <= 1) Then ksi_max := -( s0 * rr / 2 + s0 -k);
	if (ksi_nul > 1) Then ksi_max := -( s0 * rr / 2 +( 10 * sqrt(s2) ) );
	 dksi	:=	(ksi_max + ksi_n_nul) / n;
End;

Begin
Initialiser;
Slut_rand;
For i:=(n-1) DownTo 0 Do
	Begin
	Tridiagonal(i);
	Gauss;
	Back_sub;
	End;
Writeln('Delta Ksi',dksi,'ksi Max',ksi_max);
Writeln('Resultat',s0*exp(-rr)*di[0]:1:6);
End.
 
Wellll .... hard to say.

BUT, your var dksi assumes a negative value AND you try to get the square root of that in b_value <=> as sqrt() doesn't handle complex numbers, this is where you would start looking.

Regards


Jakob
 
Runtime Error 207: Invalid floating-point operation.

Which occurs on the following line:

b_value:=1 - rr * dt + s2 * sqr(tmp) * dt / sqrt(dksi);

dksi is a negative value.
 
you could put in
squrt(dksi + 2 * dksi)
so that it would sqare root a positive value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top