time-to-botec

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

main.js (3245B)


      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 isFunction = require( '@stdlib/assert/is-function' );
     24 var isnan = require( './../../../../base/assert/is-nan' );
     25 var PINF = require( '@stdlib/constants/float64/pinf' );
     26 var NINF = require( '@stdlib/constants/float64/ninf' );
     27 var abs = require( './../../../../base/special/abs' );
     28 var SCALE = require( './scale.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Computes the relative difference of two real numbers.
     35 *
     36 * @param {number} x - first number
     37 * @param {number} y - second number
     38 * @param {(string|Function)} [scale='max-abs'] - scale function
     39 * @throws {Error} must provide a recognized scale function name
     40 * @returns {number} relative difference
     41 *
     42 * @example
     43 * var d = relativeDifference( 2.0, 5.0 ); // => 3/5
     44 * // returns 0.6
     45 *
     46 * @example
     47 * var d = relativeDifference( -1.0, 3.14 ); // => 4.14/3.14
     48 * // returns ~1.318
     49 *
     50 * @example
     51 * var d = relativeDifference( -2.0, 5.0, 'max-abs' ); // => |-7/5|
     52 * // returns 1.4
     53 *
     54 * @example
     55 * var d = relativeDifference( -2.0, 5.0, 'max' ); // => |-7/5|
     56 * // returns 1.4
     57 *
     58 * @example
     59 * var d = relativeDifference( -2.0, 5.0, 'min-abs' ); // => |-7/2|
     60 * // returns 3.5
     61 *
     62 * @example
     63 * var d = relativeDifference( -2.0, 5.0, 'min' ); // => |-7/-2|
     64 * // returns 3.5
     65 *
     66 * @example
     67 * var d = relativeDifference( -2.0, 5.0, 'mean-abs' ); // => |-7/3.5|
     68 * // returns 2.0
     69 *
     70 * @example
     71 * var d = relativeDifference( -2.0, 5.0, 'mean' ); // => |-7/1.5|
     72 * // returns ~4.67
     73 *
     74 * @example
     75 * var d = relativeDifference( -2.0, 5.0, 'x' ); // => |-7/-2|
     76 * // returns 3.5
     77 *
     78 * @example
     79 * var d = relativeDifference( 5.0, -2.0, 'x' ); // => |7/5|
     80 * // returns 1.4
     81 *
     82 * @example
     83 * var d = relativeDifference( -2.0, 5.0, 'y' ); // => |-7/5|
     84 * // returns 1.4
     85 *
     86 * @example
     87 * var d = relativeDifference( 5.0, -2.0, 'y' ); // => |7/-2|
     88 * // returns 3.5
     89 */
     90 function relativeDifference( x, y, scale ) {
     91 	var f;
     92 	var s;
     93 	if ( isnan( x ) || isnan( y ) ) {
     94 		return NaN;
     95 	}
     96 	if (
     97 		x === PINF ||
     98 		x === NINF ||
     99 		y === PINF ||
    100 		y === NINF
    101 	) {
    102 		if ( x === y ) {
    103 			return NaN;
    104 		}
    105 		return PINF;
    106 	}
    107 	// If the absolute difference is `0`, then so is the relative difference:
    108 	if ( x === y ) {
    109 		return 0.0;
    110 	}
    111 	if ( isFunction( scale ) ) {
    112 		f = scale;
    113 	} else {
    114 		if ( scale === void 0 ) {
    115 			scale = 'max-abs';
    116 		}
    117 		f = SCALE[ scale ];
    118 		if ( f === void 0 ) {
    119 			throw new Error( 'invalid argument. Unrecognized/unsupported scale function. Value: `' + scale + '`.' );
    120 		}
    121 	}
    122 	s = f( x, y );
    123 	if ( s === 0.0 ) {
    124 		// Bail. No possible return value which works for all possible scale functions...
    125 		return NaN;
    126 	}
    127 	return abs( (x - y) / s );
    128 }
    129 
    130 
    131 // EXPORTS //
    132 
    133 module.exports = relativeDifference;