Usage of sqr is not safe for parameters of type Variant
Original Reporter info from Mantis: asd-tm
-
Reporter name:
Original Reporter info from Mantis: asd-tm
- Reporter name:
Description:
Arithmetics with Variant variable gives significantly different results when calling sqr(variant) and variant*variant. In the attached sample the difference leads even to different signs of the result value. Calculation with single, double and extended do not cause the calculation error.
Steps to reproduce:
procedure TForm1.Button1Click(Sender: TObject);
var
res1, res2, rxy, rxz, ryz: variant;
xes1, xes2, xxy, xxz, xyz: extended;
des1, des2, dxy, dxz, dyz: double;
ses1, ses2, sxy, sxz, syz: double;
ms: string;
begin
rxy := 0.217648676;
rxz := 0.133636231;
ryz := 0.268654257;
res1 := sqr(rxy) + sqr(rxz) - 2 * rxy * rxz * ryz;
res2 := rxy * rxy + rxz * rxz - 2 * rxy * rxz * ryz;
ms := 'Variants'#13#10 + 'Sqr: ' + Floattostr(res1) + #13#10 +
'Mult: ' + Floattostr(res2) + #13#10#13#10;
xxy := 0.217648676;
xxz := 0.133636231;
xyz := 0.268654257;
xes1 := sqr(xxy) + sqr(xxz) - 2 * xxy * xxz * xyz;
xes2 := xxy * xxy + xxz * xxz - 2 * xxy * xxz * xyz;
ms := ms + 'Extended'#13#10 + 'Sqr: ' + Floattostr(xes1) + #13#10 +
'Mult: ' + Floattostr(xes2) + #13#10#13#10;
dxy := 0.217648676;
dxz := 0.133636231;
dyz := 0.268654257;
des1 := sqr(dxy) + sqr(dxz) - 2 * dxy * dxz * dyz;
des2 := dxy * dxy + dxz * dxz - 2 * dxy * dxz * dyz;
ms := ms + 'Double'#13#10 + 'Sqr: ' + Floattostr(des1) + #13#10 +
'Mult: ' + Floattostr(des2) + #13#10#13#10;
sxy := 0.217648676;
sxz := 0.133636231;
syz := 0.268654257;
ses1 := sqr(sxy) + sqr(sxz) - 2 * sxy * sxz * syz;
ses2 := sxy * sxy + sxz * sxz - 2 * sxy * sxz * syz;
ms := ms + 'Single'#13#10 + 'Sqr: ' + Floattostr(ses1) + #13#10 +
'Mult: ' + Floattostr(ses2);
ShowMessage(ms);
end;
Additional information:
The bug does not happen on Delphi (at least on XE5 Pro)
Mantis conversion info:
- Mantis ID: 32539
- OS: Win 10 x64
- OS Build: Home
- Platform: Windows
- Version: 1.6.4
- Fixed in version: 3.1.1
- Fixed in revision: 38531 (#3e7af376)