SFML community forums

Help => General => Topic started by: Mortal on January 24, 2017, 04:55:45 am

Title: comparing floating points
Post by: Mortal on January 24, 2017, 04:55:45 am
hello.

first of all, apologies for the question wasn't directly related to SFML, but i was so frustrated about what's the proper way to compare floating points.  if someone had similar problem how can we do it.

i read alot of online papers about this subject. the latest one was about ULP which is used the integer comparison instead of comparing the float/double data. i've implemented this already but it seems not working either.
i made minimal example about ULPs method. which is, by the way taken from google units test. and i have changed it a little bit to suite my engine that i'm working on.
the example shows the failure of comparing two matrices for assign operator operator== when i tested the sf::Transform against A A-1 = A-1 A = I.

(click to show/hide)
Title: Re: comparing floating points
Post by: eXpl0it3r on January 24, 2017, 04:40:45 pm
Use an epsilon to compare. See also this (http://floating-point-gui.de/errors/comparison/).
Title: Re: comparing floating points
Post by: Mortal on January 25, 2017, 04:39:34 am
thanks, this will fix the matrix example. but i was looking for proper technique to compare any arbitrary float numbers that could be zero or non-zero and whether they close to each other or not. and of course it will handle over/under-flow since in floating points there are many entities like (+0-0) and (+inf-inf) and nan... etc
Title: Re: comparing floating points
Post by: Mortal on January 29, 2017, 01:45:01 pm
here my humble conclusion about this subject. it seems that there are no other methods to compare floating points except the epsilon and ULPs comparisons which is used the integer representation of floating point. it works similarly like relative epsilon and both methods will fail if the difference of two values or both of them are closed to zero.

this doesn't give me choice rather than run multiple tests to single pair once with fixed epsilon and others with relative epsilon and ULPs for double checking.

here the update code after i fixed a bug with matrix example.
(click to show/hide)