Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: comparing floating points  (Read 1855 times)

0 Members and 1 Guest are viewing this topic.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
comparing floating points
« 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)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: comparing floating points
« Reply #1 on: January 24, 2017, 04:40:45 pm »
Use an epsilon to compare. See also this.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: comparing floating points
« Reply #2 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

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: comparing floating points
« Reply #3 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)

« Last Edit: January 29, 2017, 08:43:40 pm by Mortal »