Re: Solving Triangles
- From: "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Sat, 28 Jan 2006 23:40:02 -0500
> Dim Dx As Double, Dy As Double
> .....<snip>.....
> Dx = X2 - X1
> .....<snip>.....
> If Dx = 0 Then
> .....<snip>.....
It is not advisable to test the contents of a Double variable for equality
to zero. Here is an example why...
X1 = 12.543
X2 = 12.4 + 0.143
Dx = X2 - X1
If Dx = 0 Then
MsgBox "Equal"
Else
MsgBox "Not Equal"
End If
Even though, mathematically, 12.4 plus 0.143 does equal 12.543, the
realities of implementing floating point numbers on a PC means this will not
always be the case. Here are some links that explain the problem...
INFO: Visual Basic and Arithmetic Precision
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q279/7/55.ASP&NoWebContent=1
(Complete) Tutorial to Understand IEEE Floating-Point Errors
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q42/9/80.ASP&NoWebContent=1
What Every Computer Scientist Should Know About Floating Point
http://docs.sun.com/source/806-3568/ncg_goldberg.html
So, what are you supposed to do? Simple. Decide on a value which is near
enough to zero to be considered zero and see if the absolute value is
smaller than it. If so, consider it to be zero. An example, using your
If-Then statement above...
If Abs(Dx) < 1e-13 Then
Rick
.
- Follow-Ups:
- Re: Solving Triangles
- From: Desertphile
- Re: Solving Triangles
- From: Steve Gerrard
- Re: Solving Triangles
- References:
- Solving Triangles
- From: Desertphile
- Re: Solving Triangles
- From: Rick Rothstein [MVP - Visual Basic]
- Re: Solving Triangles
- From: Desertphile
- Re: Solving Triangles
- From: Steve Gerrard
- Re: Solving Triangles
- From: Desertphile
- Re: Solving Triangles
- From: Desertphile
- Solving Triangles
- Prev by Date: Re: Select query and user defined function
- Next by Date: Using the printer object
- Previous by thread: Re: Solving Triangles
- Next by thread: Re: Solving Triangles
- Index(es):
Relevant Pages
|