README.md (2981B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Epsilon 22 23 > Difference between one and the smallest value greater than one that can be represented as a [double-precision floating-point number][ieee754]. 24 25 <section class="intro"> 26 27 [Epsilon][machine-epsilon] is defined as 28 29 <!-- <equation class="equation" label="eq:epsilon_float64" align="center" raw="\epsilon = b^{-(p-1)}" alt="Epsilon for a double-precision floating-point number."> --> 30 31 <div class="equation" align="center" data-raw-text="\epsilon = b^{-(p-1)}" data-equation="eq:epsilon_float64"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5d87cc7cb2c58aeb732872f89562d2c89571cc8a/lib/node_modules/@stdlib/constants/float64/eps/docs/img/equation_epsilon_float64.svg" alt="Epsilon for a double-precision floating-point number."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 where `b` is the radix (base) and `p` is the precision (number of radix bits in the significand). For [double-precision floating-point numbers][ieee754], `b` is `2` and `p` is `53`. 39 40 </section> 41 42 <!-- /.intro --> 43 44 <section class="usage"> 45 46 ## Usage 47 48 ```javascript 49 var FLOAT64_EPSILON = require( '@stdlib/constants/float64/eps' ); 50 ``` 51 52 #### FLOAT64_EPSILON 53 54 Difference between one and the smallest value greater than one that can be represented as a [double-precision floating-point number][ieee754]. 55 56 ```javascript 57 var bool = ( FLOAT64_EPSILON === 2.220446049250313e-16 ); 58 // returns true 59 ``` 60 61 </section> 62 63 <!-- /.usage --> 64 65 <section class="examples"> 66 67 ## Examples 68 69 <!-- eslint no-undef: "error" --> 70 71 ```javascript 72 var abs = require( '@stdlib/math/base/special/abs' ); 73 var max = require( '@stdlib/math/base/special/max' ); 74 var randu = require( '@stdlib/random/base/randu' ); 75 var FLOAT64_EPSILON = require( '@stdlib/constants/float64/eps' ); 76 77 var bool; 78 var a; 79 var b; 80 var i; 81 82 function isApprox( a, b ) { 83 var delta; 84 var tol; 85 86 delta = abs( a - b ); 87 tol = FLOAT64_EPSILON * max( abs( a ), abs( b ) ); 88 89 return ( delta <= tol ); 90 } 91 92 for ( i = 0; i < 100; i++ ) { 93 a = randu() * 10.0; 94 b = a + (randu()*5.0e-15) - 2.5e-15; 95 bool = isApprox( a, b ); 96 console.log( '%d %s approximately equal to %d. Delta: %d.', a, ( bool ) ? 'is' : 'is not', b, abs( a - b ) ); 97 } 98 ``` 99 100 </section> 101 102 <!-- /.examples --> 103 104 <section class="links"> 105 106 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 107 108 [machine-epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon 109 110 </section> 111 112 <!-- /.links -->