Return to Snippet

Revision: 14359
at June 1, 2009 16:42 by adamdecaf


Initial Code
<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>

Initial URL
http://SoonToBeAdded.example

Initial Description


Initial Title
Compare (Floating Point ) Values

Initial Tags
javascript

Initial Language
JavaScript