|
計算結果に演算誤差を含むとき、強制的に修正する。
但し、万能等ではなく、1.249999999999を1.25と修正できる程度。
使用には注意が必要です。(1000000/1000001を1としてしまう可能性がある)
PUBLIC NUMERIC FLG
LET X=8.99999999865/1.999999991572
PRINT "修正前";X;"修正後";NUM(X);"理想値";9/2
LET X=7.000000000357/3.000000001345*5.99999999993247
PRINT "修正前";X;"修正後";NUM(X);"理想値";14
LET X=-15/6.00000000352*10.000000034/2.999999999247
PRINT "修正前";X;"修正後";NUM(X);"理想値";-25/3
LET X=SQR(2.500000000354)*SQR(2.9999999999999871)
PRINT "修正前";X;"修正後";NUM(X);"理想値";SQR(7.5)
LET X=EXP(1.50000000321)*EXP(3.9999999999992417)
PRINT "修正前";X;"修正後";NUM(X);"理想値";EXP(5.5)
END
EXTERNAL FUNCTION INTNUM(X)
LET EPS=1E-5 !'(要)調整
FOR I=0 TO 4 !'(要)調整
FOR J=0 TO 1
LET Y=ABS(X)*10^I+J*EPS
IF ABS(Y)-INT(ABS(Y))<=EPS THEN
LET INTNUM=SGN(X)*INT(ABS(Y))/10^I
LET FLG=1
EXIT FUNCTION
END IF
NEXT J
NEXT I
LET INTNUM=X
LET FLG=0
END FUNCTION
EXTERNAL FUNCTION NUM(X)
LET Y=INT(ABS(X))
LET P=ABS(X)-Y
LET K=INTNUM(X)
IF FLG=1 THEN
LET NUM=K
EXIT FUNCTION
END IF
LET K=INTNUM(1/P)
IF FLG=1 THEN
LET NUM=(Y+1/K)*SGN(X)
EXIT FUNCTION
END IF
LET K=INTNUM(X*X)
IF FLG=1 THEN
LET NUM=SQR(K)*SGN(X)
EXIT FUNCTION
END IF
IF X>0 THEN
LET K=INTNUM(LOG(X))
IF FLG=1 THEN
LET NUM=EXP(K)
EXIT FUNCTION
END IF
END IF
IF ABS(X)<228 THEN
LET K=INTNUM(EXP(X))
IF FLG=1 THEN
LET NUM=LOG(K)*SGN(X)
EXIT FUNCTION
END IF
END IF
LET NUM=X
END FUNCTION
|
|