I'm executing the following query:
Select guiPolygonID, dbo.fn_Yval(p1X, p1Y, p2X, p2Y, 4.003318)
From [vPolygonSegments]
Where dbo.fn_Yval(p1X, p1Y, p2X, p2Y, 4.003318) > 0
The important parts of function fn_Yval (all params are of type float):
set @m = (@p2Y - @p1Y)/(@p2X - @p1X)
set @b = @p1Y - (@m*@p1X)
set @result = (@m*@xval+@b)
The view vPolygonSegments contains no records where p1X = p2X (those records are excluded). Yet, when I execute my query, SQL Server returns an error: "Divide by zero error encountered." Curiously, if I execute only the first two lines (without the where clause), the query returns results just fine.
How do I fix this, and/or what is causing this behavior?
Edit: Here is my view:
Select P1.guiPolygonID,
P1.decX as p1X, P1.decY as p1Y,
P2.decX as p2X, P2.decY as p2Y
From PolygonPoints P1, PolygonPoints P2
Where P1.guiPolygonID = P2.guiPolygonID
and (
P1.lPointNumber - P2.lPointNumber = 1
or (
-- Some other unimportant code
)
)
and P1.decX <> P2.decX