元件內無法作除法運算 |
尚未結案
|
chienboss
一般會員 發表:2 回覆:4 積分:1 註冊:2003-02-26 發送簡訊給我 |
我在一個元件內做了一個數學 cos 角度的運算(costh=x/(x^2 y^2)) a,b為整數
但是總是無法成功 可否請教前輩們幫我看看是哪裡出了問題 部份程式附錄於後
謝謝 chienboss
type
Tmymath = class(TGraphicControl)
private
ftempcal,temp:real;
sinth,costh:real;
FX1,FY1,FX2,FY2:integer;//x=fx1-fx2,y=fy1-fy2;
procedure Compute;
procedure setX1(value:integer);
procedure SetY1(value:integer);
procedure setX2(value:integer);
procedure SetY2(value:integer);
published
property tempcal:real read ftempcal write settempcal;// default 1;
property X1:integer read FX1 write SetX1 default 0;
property Y1:integer read FY1 write SetY1 default 0;
property X2:integer read FX2 write SetX2 default 0;
property Y2:integer read FY2 write SetY2 default 0;
end;
constructor Tmymath.Create (AOwner: TComponent);
begin
...
fx1:=2;fy1:=2;fx2:=3;fy2:=5;
end;
procedure Tmymath.SetX1(value:integer);
begin
if value<>FX1 then
begin
FX1:=value;
Compute;
invalidate;
end;
end;
.....
procedure Tmymath.Compute;
begin temp:= (fx1-fx2)*(fx1-fx2) (fy1-fy2)* (fy1-fy2);//temp:=x^2 y^2
ftempcal:=power(temp,0.5);//temp開平方
costh:=(tempfx1-tempfx2)/ftempcal;//costh
sinth:=(tempfy1-tempfy2)/ftempcal;//sinth
end;
//執行到ftempcal時仍正常
但執行到costh那一行時總出現'invalid floating point operation'
如果我將ftempcal設為一個固定值(例如:100)error就不會出現
究竟是何原因?
我在form內用上述相同程式執行可以很正常執行, 在元件內部執行就會有 error
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
syntax
尊榮會員 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
power() 是 extended type size 10 byte
real 是 double type size 8 byte
costh 那一行的 ftempcal 已被編譯器轉為 extended type
最好不要用不同型態的值來運算,又不做強型別的轉換 在 Form 下,與在 Classes 內 變數的 assign 實做,是不一樣的方法
所以才會出錯!
改這樣 ftempcal,temp:extended;
sinth,costh:extended; 或是這樣 ftempcal:=real(power(temp,0.5));
引言: 我在一個元件內做了一個數學 cos 角度的運算(costh=x/(x^2 y^2)) a,b為整數 但是總是無法成功 可否請教前輩們幫我看看是哪裡出了問題 部份程式附錄於後 謝謝 chienboss type Tmymath = class(TGraphicControl) private ftempcal,temp:real; sinth,costh:real; FX1,FY1,FX2,FY2:integer;//x=fx1-fx2,y=fy1-fy2; procedure Compute; procedure setX1(value:integer); procedure SetY1(value:integer); procedure setX2(value:integer); procedure SetY2(value:integer); published property tempcal:real read ftempcal write settempcal;// default 1; property X1:integer read FX1 write SetX1 default 0; property Y1:integer read FY1 write SetY1 default 0; property X2:integer read FX2 write SetX2 default 0; property Y2:integer read FY2 write SetY2 default 0; end; constructor Tmymath.Create (AOwner: TComponent); begin ... fx1:=2;fy1:=2;fx2:=3;fy2:=5; end; procedure Tmymath.SetX1(value:integer); begin if value<>FX1 then begin FX1:=value; Compute; invalidate; end; end; ..... procedure Tmymath.Compute; begin temp:= (fx1-fx2)*(fx1-fx2) (fy1-fy2)* (fy1-fy2);//temp:=x^2 y^2 ftempcal:=power(temp,0.5);//temp開平方 costh:=(tempfx1-tempfx2)/ftempcal;//costh sinth:=(tempfy1-tempfy2)/ftempcal;//sinth end; //執行到ftempcal時仍正常 但執行到costh那一行時總出現'invalid floating point operation' 如果我將ftempcal設為一個固定值(例如:100)error就不會出現 究竟是何原因? 我在form內用上述相同程式執行可以很正常執行, 在元件內部執行就會有 error |
chienboss
一般會員 發表:2 回覆:4 積分:1 註冊:2003-02-26 發送簡訊給我 |
|
danny
版主 發表:100 回覆:522 積分:595 註冊:2002-03-11 發送簡訊給我 |
引言: 我在一個元件內做了一個數學 cos 角度的運算(costh=x/(x^2 y^2)) a,b為整數 但是總是無法成功 可否請教前輩們幫我看看是哪裡出了問題 部份程式附錄於後 謝謝 chienboss type Tmymath = class(TGraphicControl) private ftempcal,temp:real; sinth,costh:real; ..............您的問題蠻奇怪的, real 是 Delphi 為了相容以前的程式才存在的, 新的程式就不要再用了; 您改用 Extended 或 Currency 看看行不行. 另外以查了一下 power 的宣告: function Power(Base, Exponent: Extended): Extended; 我想應該是 real 和 Extended 的解析度不同所致. 發表人 - danny 於 2003/04/01 15:08:36
------
將問題盡快結案也是一種禮貌! |
chienboss
一般會員 發表:2 回覆:4 積分:1 註冊:2003-02-26 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言: 各位前輩好 昨天我又試了多次,結果問題可能不是在power的問題,因為只要有變數在分母 就會產生'divided by zero'的error,我想是不是compute函數呼叫的問題? compute可否作作其他方式的改變? 請前輩指點 謝謝 chienboss上I think you need to handle the exceptional cases: 1) ftempcal = 0 2) (tempfx1-tempfx2) = 0 and ftempcal = 0 3) (tempfy1-tempfy2) = 0 and ftempcal = 0 (1) will raise divided by zero while (2) & (3) will raise invalid floating point operation exceptions in your expression. |
chienboss
一般會員 發表:2 回覆:4 積分:1 註冊:2003-02-26 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |