main.js (1870B)
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 PINF = require( '@stdlib/constants/float64/pinf' ); 24 var abs = require( './../../../../base/special/abs' ); 25 var isnan = require( './../../../../base/assert/is-nan' ); 26 var isInfinite = require( './../../../../base/assert/is-infinite' ); 27 28 29 // MAIN // 30 31 /** 32 * Computes the absolute difference. 33 * 34 * @param {number} x - first number 35 * @param {number} y - second number 36 * @returns {number} absolute difference 37 * 38 * @example 39 * var d = absoluteDifference( 2.0, 5.0 ); 40 * // returns 3.0 41 * 42 * @example 43 * var d = absoluteDifference( -1.0, 3.14 ); 44 * // returns ~4.14 45 * 46 * @example 47 * var d = absoluteDifference( 10.1, -2.05 ); 48 * // returns ~12.15 49 * 50 * @example 51 * var d = absoluteDifference( -0.0, 0.0 ); 52 * // returns +0.0 53 * 54 * @example 55 * var d = absoluteDifference( NaN, 5.0 ); 56 * // returns NaN 57 * 58 * @example 59 * var d = absoluteDifference( Infinity, -Infinity ); 60 * // returns Infinity 61 * 62 * @example 63 * var d = absoluteDifference( Infinity, Infinity ); 64 * // returns NaN 65 */ 66 function absoluteDifference( x, y ) { 67 if ( isnan( x ) || isnan( y ) ) { 68 return NaN; 69 } 70 if ( isInfinite( x ) || isInfinite( y ) ) { 71 if ( x === y ) { 72 return NaN; 73 } 74 return PINF; 75 } 76 return abs( x - y ); 77 } 78 79 80 // EXPORTS // 81 82 module.exports = absoluteDifference;