time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (3107B)


      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 [single-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_float32" align="center" raw="\epsilon = b^{-(p-1)}" alt="Epsilon for a single-precision floating-point number."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="\epsilon = b^{-(p-1)}" data-equation="eq:epsilon_float32">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5d87cc7cb2c58aeb732872f89562d2c89571cc8a/lib/node_modules/@stdlib/constants/float32/eps/docs/img/equation_epsilon_float32.svg" alt="Epsilon for a single-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 [single-precision floating-point numbers][ieee754], `b` is `2` and `p` is `24`.
     39 
     40 </section>
     41 
     42 <!-- /.intro -->
     43 
     44 <section class="usage">
     45 
     46 ## Usage
     47 
     48 ```javascript
     49 var FLOAT32_EPSILON = require( '@stdlib/constants/float32/eps' );
     50 ```
     51 
     52 #### FLOAT32_EPSILON
     53 
     54 Difference between one and the smallest value greater than one that can be represented as a [single-precision floating-point number][ieee754].
     55 
     56 ```javascript
     57 var bool = ( FLOAT32_EPSILON === 1.1920928955078125e-7 );
     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 float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
     75 var randu = require( '@stdlib/random/base/randu' );
     76 var FLOAT32_EPSILON = require( '@stdlib/constants/float32/eps' );
     77 
     78 var bool;
     79 var a;
     80 var b;
     81 var i;
     82 
     83 function isApprox( a, b ) {
     84     var delta;
     85     var tol;
     86 
     87     delta = float64ToFloat32( abs( a - b ) );
     88     tol = float64ToFloat32( FLOAT32_EPSILON * max( abs( a ), abs( b ) ) );
     89 
     90     return ( delta <= tol );
     91 }
     92 
     93 for ( i = 0; i < 100; i++ ) {
     94     a = float64ToFloat32( randu()*10.0 );
     95     b = float64ToFloat32( a + (randu()*2.0e-6) - 1.0e-6 );
     96     bool = isApprox( a, b );
     97     console.log( '%d %s approximately equal to %d', a, ( bool ) ? 'is' : 'is not', b );
     98 }
     99 ```
    100 
    101 </section>
    102 
    103 <!-- /.examples -->
    104 
    105 <section class="links">
    106 
    107 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
    108 
    109 [machine-epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
    110 
    111 </section>
    112 
    113 <!-- /.links -->