Posted By

adamdecaf on 06/01/09


Tagged

javascript float compare point int floating


Versions (?)

Compare (Floating Point ) Values


 / Published in: JavaScript
 

URL: http://SoonToBeAdded.example

  1. <script>
  2. //***********************************************************************************
  3. //* == Floating Point Comparison == *
  4. //* *
  5. //* Naturally JS stores all numbers (real/complex/rational/ect...) as floating *
  6. //* points, this is good and bad. *
  7. //* *
  8. //* Good: { *
  9. //* - No type conversion *
  10. //* - More percise calculations *
  11. //* } *
  12. //* *
  13. //* Bad: { *
  14. //* - Less control over data types *
  15. //* - You have to design these comparisons *
  16. //* - Slower to process (JS remains very fast though) *
  17. //* - Harder to deal with *
  18. //* } *
  19. //***********************************************************************************
  20.  
  21. // Call the function and ask for the three parameters.
  22. function compare(numb1, numb2, offset) {
  23. var tmp; // Declare a temp to be assigned and compared to the allowed offset.
  24.  
  25. // Subtract the two vales (Math.abs(x - y); may work but this is safer.)
  26. (numb1 < numb2) ? tmp = (numb1 - numb2) : tmp = (numb2 - numb1);
  27.  
  28. return tmp < offset;
  29. }
  30. </script>

Report this snippet  

You need to login to post a comment.