Sign in to follow this  
domok

if condition should be false but isn't

Recommended Posts

Hi

I've experienced a very strange case, in which an if condition turns out to be true although it should absolutely not!
The function tests if a point is inside a triangle and it works 99.99% of the time... I can also give you the values of A, B, C and Point.
The point is absolutely not inside the triangle and if you reproduce it (call the functions with the same arguments) it usually works correctly...

Should i use doubles for such a function? But it doesn't seem to have anything to do with floating point precision....?

Below are two images of the debugging (note the yellow arrow!)
[img]http://i811.photobucket.com/albums/zz38/dominicmoret/Untitled-1.jpg[/img]

Share this post


Link to post
Share on other sites
[quote name='Mussi' timestamp='1318673695' post='4872779']
You could try putting the outcome of bc * ac - cc * ab into a variable and check what value you get in the debug environment.
[/quote]


Thanks for that bc * ac - cc * ab is exactly 0.0 when I put it into a variable. bc * ac and cc * ab were something written in scientific notation but I can't remember if very small or very big (the problem is quite difficult to reproduce because it occurs so rarely).

I have no clue what the problem could be... I mean bc and ac were normal values around 10000.0 or something....

Share this post


Link to post
Share on other sites
Ok now I've had the problem again and some more info:


ab = 47583.820
ac = 51721.422
bc = 47070.406
cc = 51163.359

bc * ac = 2.4345482e+009
cc * ab = 2.4345482e+009

I've tried this just somewhere in the code with all floats and it really is like this...
the problem doesn't occur with a double for the result of bc * ac (but bc and ac can stay floats)

double bcXac = bc * ac;

this gives the correct value.

But although it works now I'm not really happy with it!
Could someone explain this behaviour, please!
And does this mean that I have to use double precision in all my collision routines?

Share this post


Link to post
Share on other sites
[quote name='domok' timestamp='1318677283' post='4872791']
Ok now I've had the problem again and some more info:


ab = 47583.820
ac = 51721.422
bc = 47070.406
cc = 51163.359

bc * ac = 2.4345482e+009
cc * ab = 2.4345482e+009[/quote]

Let's rewrite the equation:[code]
bc * ac - cc * ab < 0
bc * ac < cc * ab[/code]
Plugging above numbers into the equation results in: 2.43... < 2.43... Or, left isn't less than right.

[quote]I've tried this just somewhere in the code with all floats and it really is like this...
the problem doesn't occur with a double for the result of bc * ac (but bc and ac can stay floats)[/quote]
It's typical accuracy problem. Switching to doubles gives you bigger range, but doesn't eliminate the issue.

Try using different testing algorithm.

Share this post


Link to post
Share on other sites
Or should I maybe use /fp:strict instead of /fp:precise?

This accuracy problem is just really bad because the application now sometimes (very rarely but still..) gets the info that the player is below a lake triangle and so he gets hurt although he is not really near to the lake...

But if it's the only solution I'll rewrite it to double precision or how do you all handle this?

Share this post


Link to post
Share on other sites
[quote name='domok' timestamp='1318679561' post='4872803']
Or should I maybe use /fp:strict instead of /fp:precise?

But if it's the only solution I'll rewrite it to double precision or how do you all handle this?
[/quote]

Floating point math is inaccurate. No flag or different type can change it, they merely affect particular values for which the problems occur.

[quote]This accuracy problem is just really bad because the application now sometimes (very rarely but still..) gets the info that the player is below a lake triangle and so he gets hurt although he is not really near to the lake...[/quote]

Try a [url="http://www.blackpawn.com/texts/pointinpoly/default.html"]different algorithm first[/url].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this