Skip to main content
gamedev.net
🔒 Locked

Huge float inaccuracy?

Started by schupf 1 reply 700+ views
Original Post
schupf
schupf
Hi, I have written a function to extract the 6 frustum planes. The code to extract the far plane looks like this:
  frustumPlanes[5].normal.x = -(viewProjMatrix._14 - viewProjMatrix._13);
  frustumPlanes[5].normal.y = -(viewProjMatrix._24 - viewProjMatrix._23);
  frustumPlanes[5].normal.z = -(viewProjMatrix._34 - viewProjMatrix._33);
  frustumPlanes[5].distance = -(viewProjMatrix._44 - viewProjMatrix._43);
  frustumPlanes[5].normalize();
This is the approach from this document (page 3): http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf normalize() normalizes the plane and looks like this:
void Plane::normalize() { 
   float denom = 1.0 / sqrt( normal.x * normal.x +normal.y * normal.y + normal.z * normal.z );
   normal.x = normal.x * denom;
   normal.y = normal.y * denom;
   normal.z = normal.z * denom;
   distance = distance * denom;
}
The Problem: If I create a frustum with an eyepoint at the origin (0,0,0), a view vector of (0, 0, 1), the near plane at 100 and the far plane at 6 000 000 (6 million), I get these results: near plane: normal = (0,0,-1) and distance = 100.00002 (which is both correct) far plane: normal = (0,0,1) (correct) and distance = -6013439.5 The distance should be 6000000, shouldnt it? Whats the reason for this huge (!) difference between the expected result and the value I get (more than 13439 difference!). Is something wrong with the algorithm from the paper, or my implemenation or is the reason really just float inaccuracies? I also tried to calculate everything in long double, but I got the same results. Thanks for any answers!
swiftcoder
swiftcoder
Quote:
Original post by schupf
The distance should be 6000000, shouldnt it? Whats the reason for this huge (!) difference between the expected result and the value I get (more than 13439 difference!).
By my reckoning that is only 0.2 percent difference, so it isn't nearly as large as it sounds.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
OrangyTang
OrangyTang
What ever computer scientist should know about floating point arithmetic.

Edit: Slightly more helpfully, the "floating point" bit means that the actual position of the decimal place varies based on the number being represented. The upshot of this is that while you maintain the same number of significant digits, your actual precision varies depending on how far the values are from zero. When you start talking about numbers like six million then this starts being much more noticable.

(The above is a gross oversimplification, read the link for a more complete one).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.