Michael Justin’s Weblog

Fun with Software Development

Why 1.99 suddenly no longer equals 1.99

with 2 comments

A strange problem suddenly occurs in all my Delphi 7 and Delphi 2009 unit tests.

I used DUnit CheckEquals to compare two Currency values, like

CheckEquals(1.99, SomeCurrencyValue);

This worked fine in the IDE and in our build scripts, which run using the same version of DUnit for two years. Now, all of a sudden, all tests fail with strange error messages like

Expected: <1.99> - Found: <1.99>

With some newsgroup help I found that something has changed a processor flag which is responsible for floating point operations. Digging deeper in my unit tests, I found the root cause in the IBX component TIBDatabase, which seems to modify this flag.

TIBDataBase.Create(nil) changes the value of Get8087CW from $1372 to $1272, a very strange value.

procedure TForm2.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add(Format('FPU CW = $%4.4X',[Get8087CW]));
TIBDataBase.Create(nil);
Memo1.Lines.Add(Format('FPU CW = $%4.4X',[Get8087CW]));
end;

Tested on two systems running Windows 2000 SP 4, InterBase 7.5.1 and 2007 libraries (gds32.dll), Delphi 7 and Delphi 2009

Written by Michael Justin

May 4, 2009 at 4:11 pm

2 Responses

Subscribe to comments with RSS.

  1. I did some more checks with Delphi 2007 and InterBase 2007: the failure does not occur.

    But: if I manually call Set8087CW with the values you mention, I can reproduce.

    The difference between $1372 (default) and $1272 (failure) is the internal mantissa precision. It changes from 64 bits to 53 bits, which is enough to make 1.99 not equal to 1.99.

    I have seen behaviour like this in the past with some networking stacks in the Turbo Pascal 7 era, with some C++ DLL’s in the Delphi 1-3 era, and some printer drivers in the Delphi 5-7 era.

    What kind of system are you using? (Processor, OS, 32-bit/64-bit, drivers, etc)?

    –jeroen

    jpluimers

    May 5, 2009 at 10:50 pm

  2. [...] 1.7 of the Habari Active MQ Client components) posted a blog entry about a strange circumstance when 1.99 would not compare equally to 1.99. He tracked it down to the 8087 (more formally: Intel FPU) Control Word being hosed on his [...]


Leave a Reply

You must be logged in to post a comment.