function PointDistance(p1,p2:tpoint):extended;
var
pow1,pow2:extended;
dx,dy:integer;
begin
  dx:=p1.X-p2.X;
  dy:=p1.Y-p2.Y;  pow1:=dx*dx;//<<===================这里产生异常。
  pow2:=dy*dy;
  result:=sqrt(pow1+pow2);
end;function PointDistance(p1,p2:tpoint):extended;
var
pow1,pow2:extended;
dx,dy:integer;
begin
  dx:=p1.X-p2.X;
  dy:=p1.Y-p2.Y;showmessage(inttostr(dx));
showmessage(inttostr(dy));// 添加如上两句就不会有异常,哪怕p1,p2一样的值!!! 为啥?????????  pow1:=dx*dx;
  pow2:=dy*dy;
  result:=sqrt(pow1+pow2);
end;////////////////////////////////////////////win2000 professional sp4delphi6企业版 sp2

解决方案 »

  1.   

    以前写的:
    function DotDis(x1,x2,y1,y2:Integer):Double;
    Var X,y:Double;
    //需要先将整形数据转换成双精度浮点型才不容易溢出。
    begin
      x:=(x1-x2)/5000000;
      y:=(y1-y2)/5000000;
      Result:=(SQRT(SQR(x)+SQR(y)))*5000000;
    end;
      

  2.   

    function PointDistance(p1,p2:tpoint):extended;
    Var X,y:Double;
    //&ETH;è&Ograve;&ordf;&Iuml;&Egrave;&frac12;&laquo;&Otilde;&ucirc;&ETH;&Icirc;&Ecirc;&yacute;&frac34;&Yacute;×&ordf;&raquo;&raquo;&sup3;&Eacute;&Euml;&laquo;&frac34;&laquo;&para;&Egrave;&cedil;&iexcl;&micro;&atilde;&ETH;&Iacute;&sup2;&Aring;&sup2;&raquo;&Egrave;&Yacute;&Ograve;×&Ograve;&ccedil;&sup3;&ouml;&iexcl;&pound;
    x1,x2,y1,y2:integer;
    begin
      x1:=p1.X;
      x2:=p2.X;
      y1:=p1.Y;
      y2:=p2.Y;
      x:=(x1-x2)/5000000;
      y:=(y1-y2)/5000000;
      Result:=(SQRT(SQR(x)+SQR(y)))*5000000;
    end;
    异常照旧阿
      

  3.   

    http://expert.csdn.net/Expert/topic/2965/2965630.xml?temp=.557873我试过你的代码,function PointDistance(p1,p2:tpoint):extended;
    var
    pow1,pow2:extended;
    dx,dy:integer;
    begin
      dx:=p1.X-p2.X;
      dy:=p1.Y-p2.Y;  pow1:=dx*dx;
      pow2:=dy*dy;
      result:=sqrt(pow1+pow2);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var p1,p2:tpoint;
        e: extended;
    begin
      p1.X := 2147483647;
      p1.Y := 2147483647;
      p2.X := 2147483647;
      p2.Y := 2147483647;
      e :=  PointDistance(p1, p2);
      edit1.Text := FloatToStr(e);
    end;只是回溢出而已, 不会出错啊!!!
    可能你打开了溢出检查!!!
    在菜单 Project - Options 中的Compile 中的
     Overflow checked
      

  4.   

    是一个异常对话框,EInvalidOP异常
      

  5.   

    不用Extended,用标准的浮点类型single或double试试
      

  6.   

    我用的D7,没这个问题列:)如果是很早的机器,没有FPU,也会出现这类异常的  :P
      

  7.   

    你直接编译成个 exe, 运行试验下
      

  8.   

    在出错地方设置段点,停下来后Alt+Ctrl+C叫出CPU Window,看看是哪一句出错乐
      

  9.   

    直接运行exe就是替是这个异常阿
      

  10.   

    这个跟IDE应该无关的。因为脱离IDE环境运行EXE他说也是这个异常啊,stanely你设置个端点然后叫出CPU Window,然后一步一步的跟踪,出错后把出错的地方的汇编代码告诉我啊,我这里没这个异常不方便啊
      

  11.   

    EInvalidOp is the exception class for undefined floating-point operations.UnitSysUtilsDescriptionEInvalidOp is raised when the CPU encounters an undefined instruction, invalid operation, or floating-point stack overflow.Run-time exception information is saved in fields provided by EExternal.就帮助看来, 还是 floating-point stack overflow 了
      

  12.   

    没Delphi用,没个好点的PC用
    工作没镐头,要准备跳槽,但不知道去边到,郁闷
      

  13.   

    我用D板Delphi,现在还是PII电脑,
      

  14.   

    //单独调用这个函数确实没什么异常,但是如下调用的时候就有问题:unit Math_san;interface
    uses
    windows,sysutils,math;type
      TDestination=(dLeft,dRight,dUp,DDown);
    function OnDestination(p,center:tpoint;
            Destination:TDestination;
            Include45:boolean=true
            ):boolean;overload;function OnDestination(r,center:TRect;
            Destination:TDestination;
            Include45:boolean=true
            ):boolean;overload;
            
    function RectTopRight(rect:TRect):TPoint;function PointDistance(p1,p2:tpoint):extended;
    function RectDistance(r1,r2:trect;VisualDistance:boolean):extended;function RectBottomLeft(rect:TRect):TPoint;function PointInRect(Point:TPoint;Rect:TRect):boolean;function NearestRect(TheIndex:integer;Destination:TDestination;
            const Rects:array of TRect; VisualDistance:boolean):integer;function OverLappedRects(Rect1,Rect2:TRect):boolean;function RectCenter(rect:TRect):TPoint;function NearestPoint(TheIndex:integer;Destination:TDestination;
            const points:array of TPoint):integer;function ExtremeIndex(const a:array of integer;Max_true_Min_false:boolean):integer;
    implementation
    function ExtremeIndex(const a:array of integer;Max_true_Min_false:boolean):integer;
    var
    e,i:integer;
    begin
      result:=low(a);
      e:=a[result];
      if max_true_min_false then
      for i:=low(a)+1 to high(a) do
      begin
        if a[i]>e then
        begin
          result:=i;
          e:=a[i];
        end;
      end else
      for i:=low(a)+1 to high(a) do
      begin
        if a[i]<e then
        begin
          result:=i;
          e:=a[i];
        end;
      end;
      
    end;
    function PointDistance(p1,p2:tpoint):extended;
    var
    pow1,pow2:extended;
    dx,dy:extended;
    a,b,c,d: extended;
    begin
    //  msg2(p1.x);
    //  beep;
      a := p1.X;
      b := p1.Y;
      c := p2.X;
      d := p2.Y;
      dx:=a-c;
      dy:=b-d;  pow1:=dx*dx;
      pow2:=dy*dy;
      result:=sqrt(pow1+pow2);
    end;function RectDistance(r1,r2:trect;VisualDistance:boolean):extended;
    var
    r1o,r2o:tpoint;
    d1,d2,d:integer;
    rate:double;
    begin
      result:=0;
      r1o:=rectcenter(r1);
      r2o:=rectcenter(r2);
      if VisualDistance and (not overlappedrects(r1,r2)) then
      begin    d:=abs(r1o.X-r2o.X);
        if d<>0 then
        begin
          d1:=abs(r1o.X-r1.Left);
          d2:=abs(r2o.X-r2.Left);
        end else begin
          d:=abs(r1o.Y-r2o.Y);
          if d=0 then exit;
          d1:=abs(r1o.Y-r1.Top);
          d2:=abs(r2o.Y-r2.Top);
        end;
    //    beep;
        rate:=((1.0*d1)+(d2+0.0))/d;    result:=pointdistance(r1o,r2o)*(1-rate);
        
      end else result:=pointdistance(r1o,r2o);end;
    ///////////////////////
    function OnDestination(p,center:tpoint;
            Destination:TDestination;
            Include45:boolean=true):boolean;
    var
    dx,dy:integer;
    begin
      result:=false;
      dx:=abs(p.X-center.X);
      dy:=abs(p.Y-center.Y);
      case destination of
        dleft:   result:=(dy<dx) and (p.X<center.X);
        dright:  result:=(dy<dx) and (p.X>center.X);
        dup:     result:=(dy>dx) and (p.Y<center.Y);
        ddown:   result:=(dy>dx) and (p.Y>center.Y);
      end;
      if include45 then result:=result or (dx=dy);
    end;
    function OnDestination(r,center:TRect;
            Destination:TDestination;
            Include45:boolean=true
            ):boolean;
    var
    tl,bl,tr,br,cp:tpoint;
    begin
      cp:=rectcenter(center);
      tl:=r.TopLeft;
      br:=r.BottomRight;
      tr:=recttopright(r);
      bl:=rectbottomleft(r);
      result:=ondestination(tl,cp,destination,include45)
            or ondestination(tr,cp,destination,include45)
            or ondestination(bl,cp,destination,include45)
            or ondestination(br,cp,destination,include45);
    end;function NearestPoint(TheIndex:integer;Destination:TDestination;const points:array of TPoint):integer;
    var
    t,j,i:integer;
    p,pp:tpoint;
    b:boolean;
    ia,indexa:array of integer;
    begin
      result:=theindex;
      p:=points[theindex];  setlength(ia,length(points));
      setlength(indexa,length(ia));
      
      j:=0;
      for i:=low(points) to high(points) do
      begin
        pp:=points[i];
        if (i=theindex)or(not(ondestination(pp,p,destination))) then continue;    case destination of
        dLeft,dright: t:=pp.X;
        dup,ddown: t:=pp.Y;    
        end;
        ia[j]:=t;
        indexa[j]:=i;
        inc(j);
      end;
      setlength(ia,j);
      setlength(indexa,j);
      case destination of
      dleft,dup: b:=true;
      dright,ddown: b:=false;
      end;
    //  msg2(length(ia));
    //  msgbool(length(ia)=length(indexa));
      if j<=0 then exit;
      
      i:=extremeindex(ia,b);
      result:=indexa[i];
    //  beep;
      setlength(ia,0);
      setlength(indexa,0);
    end;function RectCenter(rect:TRect):TPoint;
    begin
      result.X:=((rect.Right-rect.Left) div 2)+rect.Left;
      result.Y:=((rect.Bottom-rect.Top) div 2)+rect.Top;
    end;
    function RectTopRight(rect:TRect):TPoint;
    begin
      result.X:=rect.Right;
      result.Y:=rect.Top;
    end;
    function RectBottomLeft(rect:TRect):TPoint;
    begin
      result.X:=rect.Left;
      result.Y:=rect.Bottom;
    end;function PointInRect(Point:TPoint;Rect:TRect):boolean;
    begin
      result:=(point.X>=rect.Left) and (point.X<=rect.Right)
            and (point.Y>=rect.Top) and (point.Y<=rect.Bottom);;
    end;
    function OverLappedRects(Rect1,Rect2:TRect):boolean;
    begin
      result:=(pointinrect(rect1.TopLeft,rect2))or
            (pointinrect(rect1.BottomRight,rect2))or
            (pointinrect(recttopright(rect1),rect2))or
            (pointinrect(rectbottomleft(rect1),rect2))or
            (pointinrect(rect2.TopLeft,rect1))or
            (pointinrect(rect2.BottomRight,rect1))or
            (pointinrect(recttopright(rect2),rect1))or
            (pointinrect(rectbottomleft(rect2),rect1));
    end;function NearestRect(TheIndex:integer;Destination:TDestination;
            const Rects:array of TRect;VisualDistance:boolean):integer;
    varj:integer;
    points:array of tpoint;i,l:integer;
    min,d:extended;
    r,tr:trect;
    begin{    setlength(points,length(rects));
        j:=low(rects)-low(points);
        for i:=low(points) to high(points) do
        begin
          points[i]:=rectcenter(rects[j+i]);
        end;
        result:= nearestpoint(theindex-j,destination,points);
        setlength(points,0);    exit;  }
    //////////////////////////////////// Curious  Exceptions!!!!!!!!  r:=rects[theindex];
      result:=theindex;
      l:=low(rects);
      for i:=l to high(rects) do
      begin
        tr:=rects[i];
        if (i=theindex)or(not ondestination(tr,r,destination,true)) then continue;
        d:=rectdistance(tr,r,visualdistance);
        if i=l then
          min:=d
        else
        begin
          if d<min then
          begin
            min:=d;
            result:=i;
          end;
        end;  end;
    end;end.///////////////////////////////////////////在窗体上多方几个控件。procedure Tform1.button1clicl(sender:tobject);
    var
    i:integer;
    a:array of trect;
    begin
        setlength(a,controlCount);
        for i:=low(a) to high(a) do
        begin      a[i]:=controls[i].boundsrect;//<<===改一下
        end;
        i:=math_san.nearestrect(0,dleft,a,false);
        setlength(a,0); 
    end;  //测试代码的意思就是:
    找到根第0个control最近的control,
    math_san.nearestrect(0,dleft,a,false);
    代表中心最近的,
    math_san.nearestrect(0,dleft,a,true);
    代表视觉上最近的,就是两个rect中心的连线与各自rect的边相交的两点的距离最小
     
    nearestrect(theindex:integer//<<==这个参数代表基准rect再a数组中的下标,返回值是属组中根此rect相距最近的rect的下标 ,destination参数代表在哪个方向上找 
    /////////////////////////////////////////请帮我测试一下,是不是有EInvalidOP异常?谢谢!
      

  15.   

    同意Eastunfail(浴血雏龙)==(恶鱼杀手)我已经试过了.
    用Single或者Double,就不会有这个问题了.
      

  16.   

    好了没有啊?
    我Delphi坏了。5555555555~~~~打开后就出现下面几个警告:
    Error reading CDKSmartClipboard.BufferScrollBars: Not enough memory for operationNot enough storage is available to process this command.Error reading MemExpansion.LeftMargin: Division by zeroThe instruction at "0x40008e48" referenced memory at "0x01db0910". The memory could not be "red".