实践胜于理论:
对所有数字执行测试,如下所示:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
{$IFNDEF DEBUG}
{$DEFINE DEBUG}
{$ENDIF}
procedure Test1(Value: Cardinal);
var
Root: Cardinal;
begin
Root:= Trunc(Sqrt(Value));
Assert(Root * Root <= Value);
if Root < $FFFF then
Assert((Root + 1) * (Root + 1) > Value);
end;
procedure Test2(Value: UInt64);
var
Root: UInt64;
begin
Root:= Trunc(Sqrt(Value));
Assert(Root * Root <= Value);
if Root < $FFFFFFFF then
Assert((Root + 1) * (Root + 1) > Value);
end;
var
VCar: Cardinal;
VUInt: UInt64;
const
Limit1: Cardinal = $FFFFFFFF;
Limit2: UInt64 = $FFFFFFFFFFFFFFFF;
begin
try
for VCar := 0 to Limit1 do
begin
if (VCar mod 10000000) = 0 then
Writeln('VCarTest ', VCar, ' ', (VCar / Limit1 * 100):0:2, '%');
Test1(VCar);
end;
Writeln('VCarTest 0 .. $', IntToHex(Limit1, 8), ' passed');
{ commented because cannot be executed in a reasonable time
VUInt := 0;
while (VUInt <= Limit2) do
begin
if (VUInt mod 2500000) = 0 then
Writeln('VUIntTest ', VUInt, ' ', (VUInt / Limit2 * 100):0:2, '%');
Test2(VUInt);
Inc(VUInt);
end;
Writeln('VUIntTest ', VUInt);
Writeln('All passed');
}
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
由于测试UInt64的整个范围确实需要很长时间,所以我稍微更改了测试以测试所有完美的正方形,之前的数字和每个之后的数字,只是为了让它更快并且有更好的想法。我个人对 32 位进行了一段时间的测试而没有失败(占整个测试的 1%),而在 64 位上它显示失败的速度非常快。我仍在寻找更接近此的方法,但我发布了代码以防万一您感兴趣:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
{$IFNDEF DEBUG}
{$message error 'change your build configuration to Debug!'}
{$ENDIF}
procedure Test2(Value: UInt64);
var
Root: UInt64;
begin
//try/except block only for 64 bits, since in 32 bits it makes the process much slower
{$ifdef CPUX64}
try
{$endif}
Root:= Trunc(Sqrt(Value));
Assert(Root * Root <= Value);
if Root < $FFFFFFFF then
Assert((Root + 1) * (Root + 1) > Value);
{$ifdef CPUX64}
except
Writeln('Fails for value: ', Value, ' root: ', Root
, ' test: ', (Root + 1) * (Root + 1));
raise;
end;
{$endif}
end;
var
RUInt, VUInt: UInt64;
const
Limit2: UInt64 = $FFFFFFFFFFF00000;
begin
try
RUInt := 1;
repeat
Inc(RUInt);
VUInt := RUInt * RUInt;
if (RUInt mod 2500000) = 0 then
Writeln('VUIntTest ', VUInt, ' ', (VUInt / Limit2 * 100):0:4, '%');
Test2(VUInt - 1);
Test2(VUInt);
Test2(VUInt + 1);
until (VUInt >= Limit2);
Writeln('VUIntTest ', VUInt);
Writeln('All passed');
except
on E:EAssertionFailed do
Writeln('The assertion failed for value ', VUInt, ' root base ', RUInt);
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.