BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Rounding errors with the Extended type

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi ObjectPascal
View previous topic :: View next topic  
Author Message
Michael Vinther
Guest





PostPosted: Mon Dec 08, 2003 10:34 pm    Post subject: Rounding errors with the Extended type Reply with quote



I just came across a very strange problem with the Extended type in
Delphi. When computing 0+A-A where A is of type Extended the result is
not always zero.

In FormCreate for my main form I do get zero as expected, but in some
other event handler, e.g. Button1Click, I get -1.225e-16 when A is set
to the Pi constant See the example code below. Note that
TForm1.FormCreate and TForm1.Button1Click contain EXACTLY the same
lines. If Extended is replaced by Double the result is zero both
times.

Why don't I get zero both times with Extended calculatios? Is there
anything I can do to fix it except for changing to the Double type?
Is it a problem with Delphi or with the CPU? The program was testet on
an Athlon XP running Win2k.


procedure TForm1.FormCreate(Sender: TObject);
var A, B : Extended;
begin
A:=Pi;
B:=0;
B:=B+A;
B:=B-A;
ShowMessage(FloatToStrF(B,ffGeneral,5,5)); // Displays 0
end;

procedure TForm1.Button1Click(Sender: TObject);
var A, B : Extended;
begin
A:=Pi;
B:=0;
B:=B+A;
B:=B-A;
ShowMessage(FloatToStrF(B,ffGeneral,5,5)); // Displays -1.225e-16
end;
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi ObjectPascal All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.