time-to-botec

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

main.js (3423B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var frexp = require( '@stdlib/math/base/special/frexp' );
     24 var ldexp = require( '@stdlib/math/base/special/ldexp' );
     25 
     26 
     27 // VARIABLES //
     28 
     29 // `frexp` workspace:
     30 var PARTS = [ 0.0, 0 ]; // WARNING: not thread safe
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns an accumulator function which incrementally computes a product.
     37 *
     38 * ## Method
     39 *
     40 * To avoid overflow/underflow, we store the fractional and exponent parts of intermediate results separately. By keeping a normalized fraction, we prevent underflow/overflow of the fraction. Underflow of the exponent is impossible, as IEEE 754 floating-point exponents are integer values. Overflow of the exponent is possible, but highly unlikely. In the worst case, an intermediate exponent is greater than the minimum safe integer, and adding the exponent of an incoming value does not change the intermediate result. While incorrect, such behavior does not lead to exponent overflow.
     41 *
     42 * While intermediate results are largely immune to overflow and not subject to underflow, this does not mean that returned results will never be zero or infinite. In fact, zero (underflow) and infinite (overflow) results may be transient (i.e., infinity followed by a finite number).
     43 *
     44 *
     45 * ## References
     46 *
     47 * -   Ueberhuber, Christoph W. 1997. _Numerical Computation 1: Methods, Software, and Analysis_. Springer-Verlag Berlin Heidelberg. doi:[10.1007/978-3-642-59118-1](https://doi.org/10.1007/978-3-642-59118-1).
     48 *
     49 * @returns {Function} accumulator function
     50 *
     51 * @example
     52 * var accumulator = incrprod();
     53 *
     54 * var prod = accumulator();
     55 * // returns null
     56 *
     57 * prod = accumulator( 2.0 );
     58 * // returns 2.0
     59 *
     60 * prod = accumulator( -5.0 );
     61 * // returns -10.0
     62 *
     63 * prod = accumulator();
     64 * // returns -10.0
     65 */
     66 function incrprod() {
     67 	var frac;
     68 	var prod;
     69 	var exp;
     70 
     71 	// Initial product is 1.0, which may be split into its fractional and exponent parts (0.5 x 2.0**1 = 1.0):
     72 	frac = 0.5;
     73 	exp = 1.0;
     74 
     75 	return accumulator;
     76 
     77 	/**
     78 	* If provided a value, the accumulator function returns an updated product. If not provided a value, the accumulator function returns the current product.
     79 	*
     80 	* @private
     81 	* @param {number} [x] - new value
     82 	* @returns {(number|null)} product or null
     83 	*/
     84 	function accumulator( x ) {
     85 		if ( arguments.length === 0 ) {
     86 			return ( prod === void 0 ) ? null : prod;
     87 		}
     88 		// Split the incoming value into a normalized fraction and exponent:
     89 		frexp( PARTS, x );
     90 
     91 		// Update the accumulated fraction:
     92 		frac *= PARTS[ 0 ];
     93 
     94 		// Update the accumulated exponent:
     95 		exp += PARTS[ 1 ];
     96 
     97 		// Ensure fraction remains normalized to avoid overflow/underflow...
     98 		if ( frac > -0.5 && frac < 0.5 ) {
     99 			frexp( PARTS, frac );
    100 			frac = PARTS[ 0 ];
    101 			exp += PARTS[ 1 ];
    102 		}
    103 		prod = ldexp( frac, exp );
    104 		return prod;
    105 	}
    106 }
    107 
    108 
    109 // EXPORTS //
    110 
    111 module.exports = incrprod;