User Tools

Site Tools


floating_point_comparison

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
floating_point_comparison [2022/04/29 23:40]
max
floating_point_comparison [2022/04/29 23:54] (current)
max
Line 1: Line 1:
-====== Floating-point number comparison inside Impulse Physics ======+====== Floating-point number comparison ======
  
  
 Floating-point numbers are almost always stored inaccurately in computers' memory. This is because of the [[https://en.wikipedia.org/wiki/IEEE_754|IEEE-754]] standard that is used to store the number. Floating-point numbers are almost always stored inaccurately in computers' memory. This is because of the [[https://en.wikipedia.org/wiki/IEEE_754|IEEE-754]] standard that is used to store the number.
  
-It also means that for example, 0.2 + 0.1 is not equal to 0.3; rather it is equal to 0.30000000000000004.+For example, that means ''0.2 + 0.1'' is not equal to ''0.3''; rather it is equal to ''0.30000000000000004''.
 Since computers are exact in comparison by default, you can see how you can run into issues with this. Since computers are exact in comparison by default, you can see how you can run into issues with this.
  
Line 11: Line 11:
 The default option is a combination of both the absolute and relative methods of epsilon comparison. ((http://www.realtimecollisiondetection.net/pubs/Tolerances/)) The default option is a combination of both the absolute and relative methods of epsilon comparison. ((http://www.realtimecollisiondetection.net/pubs/Tolerances/))
 The second option is simply the computer's default "==" operator. It is not recommended, but it is marginally faster.  The second option is simply the computer's default "==" operator. It is not recommended, but it is marginally faster. 
 +
 +Which method the engine uses can be changed in the source code in the ''IPCommon.h'' file.
  
 ===== The absolute method ===== ===== The absolute method =====
Line 20: Line 22:
 ===== The relative method ===== ===== The relative method =====
  
-The relative method takes the result of the absolute method and multiplies the epsilon by ''x'' or ''y'', depending on which is larger.+The relative method takes the absolute method, but multiplies the epsilon by ''x'' or ''y'', depending on which is larger.
  
 The drawback to this method is that it does not work on small values. The drawback to this method is that it does not work on small values.
Line 28: Line 30:
 Considering the strengths and drawbacks of the two methods above, you can combine the two to overcome these limitations.  Considering the strengths and drawbacks of the two methods above, you can combine the two to overcome these limitations. 
  
-The combined method takes the result of the absolute method and multiplies it by the largest absolute number out of a set of ''1'', ''x'', or ''y''. Very similar to the relative method.+The combined method takes the relative method, but multiplies the epsilon by the largest absolute number out of a set of ''1'', ''x'', or ''y''. Very similar to the relative method.
  
 This method gives us good results, but it is not without its drawbacks since it uses the same epsilon for the relative and the absolute tolerance.  This method gives us good results, but it is not without its drawbacks since it uses the same epsilon for the relative and the absolute tolerance. 
  
 For further reading, please check out [[http://www.realtimecollisiondetection.net/pubs/Tolerances/|Christer Erison's research on this topic.]] For further reading, please check out [[http://www.realtimecollisiondetection.net/pubs/Tolerances/|Christer Erison's research on this topic.]]
floating_point_comparison.1651275615.txt.gz ยท Last modified: 2022/04/29 23:40 by max