代码如下
×××××××××××××××××××××××××××××××××××××××××××××××××
procedure TForm1.Button1Click(Sender: TObject);
var
w,k,p,s,m,i,n:real;
Pf:real;
r1,r2:real;
begin
m:=0;
i:=0;
n:=strtofloat(edit1.Text); while i<n do
begin
r1:=rnd(0,1);
r2:=rnd(0,1);
w:=0.1+0.1*0.05*sqrt(-2*ln(r1))*cos(2*pi*r2);
k:=1.5e-5+0.05*1.5e-5*sqrt(-2*ln(r1))*cos(2*pi*r2);
p:=42.73+0.1*42.73*sqrt(-2*ln(r1))*cos(2*pi*r2);
s:=84.33+0.04*84.33*sqrt(-2*ln(r1))*cos(2*pi*r2);
if w-25185.0798*k*p/(3*s)<0 then
m:=m+1;
i:=i+1;
end;
Pf:=m/n;
edit2.Text:=floattostr(Pf);end;end.
××××××××××××××××××××××××××××××××××××××××××××××××××××××8
其中rnd(0,1)是生成0,1之间均匀随机数 函数输入n=1000时没有错误,但是n=10000时 运行会出现错误 "floating point division by zero" 循环到 w:=0.1+0.1*0.05*sqrt(-2*ln(r1))*cos(2*pi*r2); 这行出现错误
但是这个语句没有除式啊
有没有知道这个问题的
×××××××××××××××××××××××××××××××××××××××××××××××××
procedure TForm1.Button1Click(Sender: TObject);
var
w,k,p,s,m,i,n:real;
Pf:real;
r1,r2:real;
begin
m:=0;
i:=0;
n:=strtofloat(edit1.Text); while i<n do
begin
r1:=rnd(0,1);
r2:=rnd(0,1);
w:=0.1+0.1*0.05*sqrt(-2*ln(r1))*cos(2*pi*r2);
k:=1.5e-5+0.05*1.5e-5*sqrt(-2*ln(r1))*cos(2*pi*r2);
p:=42.73+0.1*42.73*sqrt(-2*ln(r1))*cos(2*pi*r2);
s:=84.33+0.04*84.33*sqrt(-2*ln(r1))*cos(2*pi*r2);
if w-25185.0798*k*p/(3*s)<0 then
m:=m+1;
i:=i+1;
end;
Pf:=m/n;
edit2.Text:=floattostr(Pf);end;end.
××××××××××××××××××××××××××××××××××××××××××××××××××××××8
其中rnd(0,1)是生成0,1之间均匀随机数 函数输入n=1000时没有错误,但是n=10000时 运行会出现错误 "floating point division by zero" 循环到 w:=0.1+0.1*0.05*sqrt(-2*ln(r1))*cos(2*pi*r2); 这行出现错误
但是这个语句没有除式啊
有没有知道这个问题的
var
w,k,p,s,m,i,n:real;
Pf:real;
r1,r2:real;
begin
m:=0;
i:=0;
n:=strtofloat(edit1.Text);
randomize;
while i <n do
begin
r1:=random;//rnd(0,1);
r2:=random;//rnd(0,1);
w:=0.1+0.1*0.05*sqrt(-2*ln(r1))*cos(2*pi*r2);
k:=1.5e-5+0.05*1.5e-5*sqrt(-2*ln(r1))*cos(2*pi*r2);
p:=42.73+0.1*42.73*sqrt(-2*ln(r1))*cos(2*pi*r2);
s:=84.33+0.04*84.33*sqrt(-2*ln(r1))*cos(2*pi*r2);
if w-25185.0798*k*p/(3*s) <0 then
m:=m+1;
i:=i+1;
end;
Pf:=m/n;
edit2.Text:=floattostr(Pf);
end;楼主没有发现错误啊.
ln(e)=1, 那么ln(0)是无解的。