mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-11-02 06:29:25 +01:00
* 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 -
This commit is contained in:
parent
879e487ee8
commit
0c153a46df
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -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
|
||||
|
||||
@ -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
tests/test/units/math/tpower.pp
Normal file
10
tests/test/units/math/tpower.pp
Normal file
@ -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.
|
||||
Loading…
Reference in New Issue
Block a user