代码如下
×××××××××××××××××××××××××××××××××××××××××××××××××
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); 这行出现错误  
但是这个语句没有除式啊  
有没有知道这个问题的   

解决方案 »

  1.   

    这是个蒙特卡洛法的一小段程序  出现错误的那行没有除式啊   而且所有变量的数值都不会超出范围啊   循环变量n定义的式real 而不是int  
      

  2.   

    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);
      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;楼主没有发现错误啊.
      

  3.   

    呵呵,是因为ln(r1)的原因,当r1随机取0的时候会出错,
    ln(e)=1, 那么ln(0)是无解的。