/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<script> //*********************************************************************************** //* == Floating Point Comparison == * //* * //* Naturally JS stores all numbers (real/complex/rational/ect...) as floating * //* points, this is good and bad. * //* * //* Good: { * //* - No type conversion * //* - More percise calculations * //* } * //* * //* Bad: { * //* - Less control over data types * //* - You have to design these comparisons * //* - Slower to process (JS remains very fast though) * //* - Harder to deal with * //* } * //*********************************************************************************** // Call the function and ask for the three parameters. function compare(numb1, numb2, offset) { var tmp; // Declare a temp to be assigned and compared to the allowed offset. // Subtract the two vales (Math.abs(x - y); may work but this is safer.) (numb1 < numb2) ? tmp = (numb1 - numb2) : tmp = (numb2 - numb1); return tmp < offset; } </script>
URL: http://SoonToBeAdded.example