Let’s kick this off with what may be a surprising result. What should this code return?
It would seem that @i should be the point where @g and @h intersect, and so a test whether @i intersects @g and @h should clearly return true (or 1). However, for some reason @i does not appear to intersect either @g or @h. How can that be true?
The reason is that geometric calculations are by nature imprecise, and this is an unavoidable result of the imprecise nature of the data types upon which these calculations are built. To illustrate, let’s take a look at a non-geometric example. First, some values to work with:
Note that none of these values are particularly large: they all fit comfortably within our 64-bit floating point variables. Next let’s look at a pair of equations:
If we think back to basic algebra, we know that these two equations should be the same: in the second one we’ve simply distributed @u over (@v + @w). But, if we test this…
…we find that SQL Server doesn’t think they’re equal. We can look at each of these values out to see what’s going on:
Ack! If we were working with true rational numbers, these values should be exactly the same (and should be exactly .1). Our floating point calculations don’t return the correct result, and don’t even return the same results: they will never compare as equal.
There is, in fact, no way to avoid this type of problem completely with any bounded numerical representation, so we are stuck dealing with imprecise calculations no matter how we represent values. One result of this is the maxim that you should never test floating point numbers for equality, and this general rule extends to geometries (and geographies) as well.
Let’s look again at our geometry example. If we again do a little algebra, we find that the actual intersection point should be (2/3, 2). If we as SQL Server, however, our results are a little off:
Cheers, and enjoy SQL Server 2008.