ソースを参照

* math.power/intpower(0,0) return 1, this is as recommended in IEEE 754
as well as compatible with other programming languages and delphi

git-svn-id: trunk@14873 -

florian 15 年 前
コミット
0c153a46df
3 ファイル変更29 行追加18 行削除
  1. 1 0
      .gitattributes
  2. 18 18
      rtl/objpas/math.pp
  3. 10 0
      tests/test/units/math/tpower.pp

+ 1 - 0
.gitattributes

@@ -9398,6 +9398,7 @@ tests/test/units/math/tmask.inc svneol=native#text/plain
 tests/test/units/math/tmask.pp svneol=native#text/plain
 tests/test/units/math/tmask2.pp svneol=native#text/plain
 tests/test/units/math/tnaninf.pp svneol=native#text/plain
+tests/test/units/math/tpower.pp svneol=native#text/pascal
 tests/test/units/math/ttrig1.pp svneol=native#text/plain
 tests/test/units/objects/testobj.pp svneol=native#text/plain
 tests/test/units/objects/testobj1.pp svneol=native#text/plain

+ 18 - 18
rtl/objpas/math.pp

@@ -102,7 +102,7 @@ interface
 
        tpaymenttime = (ptendofperiod,ptstartofperiod);
 
-       einvalidargument = class(ematherror);
+       EInvalidArgument = class(ematherror);
 
        TValueRelationship = -1..1;
 
@@ -875,10 +875,7 @@ function power(base,exponent : float) : float;
 
   begin
     if Exponent=0.0 then
-      if base <> 0.0 then
-        result:=1.0
-      else
-        InvalidArgument
+      result:=1.0
     else if (base=0.0) and (exponent>0.0) then
       result:=0.0
     else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
@@ -896,21 +893,24 @@ function intpower(base : float;const exponent : Integer) : float;
 
   begin
      if (base = 0.0) and (exponent = 0) then
-       InvalidArgument;
-     i:=abs(exponent);
-     intpower:=1.0;
-     while i>0 do
+       result:=1
+     else
        begin
-          while (i and 1)=0 do
-            begin
-               i:=i shr 1;
-               base:=sqr(base);
-            end;
-          i:=i-1;
-          intpower:=intpower*base;
+         i:=abs(exponent);
+         intpower:=1.0;
+         while i>0 do
+           begin
+              while (i and 1)=0 do
+                begin
+                   i:=i shr 1;
+                   base:=sqr(base);
+                end;
+              i:=i-1;
+              intpower:=intpower*base;
+           end;
+         if exponent<0 then
+           intpower:=1.0/intpower;
        end;
-     if exponent<0 then
-       intpower:=1.0/intpower;
   end;
 
 

+ 10 - 0
tests/test/units/math/tpower.pp

@@ -0,0 +1,10 @@
+uses
+  math;
+
+begin
+  if power(0,0)<>1 then
+    halt(1);
+  if intpower(0,0)<>1 then
+    halt(1);
+  writeln('ok');
+end.