repl.txt (2539B)
1 2 {{alias}}( x, y[, scale] ) 3 Computes the relative difference of two real numbers. 4 5 By default, the function scales the absolute difference by dividing the 6 absolute difference by the maximum absolute value of `x` and `y`. To scale 7 by a different function, specify a scale function name. 8 9 The following `scale` functions are supported: 10 11 - 'max-abs': maximum absolute value of `x` and `y` (default). 12 - 'max': maximum value of `x` and `y`. 13 - 'min-abs': minimum absolute value of `x` and `y`. 14 - 'min': minimum value of `x` and `y`. 15 - 'mean-abs': arithmetic mean of the absolute values of `x` and `y`. 16 - 'mean': arithmetic mean of `x` and `y`. 17 - 'x': `x` (*noncommutative*). 18 - 'y': `y` (*noncommutative*). 19 20 To use a custom scale function, provide a function which accepts two numeric 21 arguments `x` and `y`. 22 23 If the absolute difference of `x` and `y` is `0`, the relative difference is 24 always `0`. 25 26 If `|x| = |y| = infinity`, the function returns `NaN`. 27 28 If `|x| = |-y| = infinity`, the relative difference is `+infinity`. 29 30 If a `scale` function returns `0`, the function returns `NaN`. 31 32 Parameters 33 ---------- 34 x: number 35 First number. 36 37 y: number 38 Second number. 39 40 scale: string|Function (optional) 41 Scale function. Default: `'max-abs'`. 42 43 Returns 44 ------- 45 out: number 46 Relative difference. 47 48 Examples 49 -------- 50 > var d = {{alias}}( 2.0, 5.0 ) 51 0.6 52 > d = {{alias}}( -1.0, 3.14 ) 53 ~1.318 54 > d = {{alias}}( -2.0, 5.0, 'max-abs' ) 55 1.4 56 > d = {{alias}}( -2.0, 5.0, 'max' ) 57 1.4 58 > d = {{alias}}( -2.0, 5.0, 'min-abs' ) 59 3.5 60 > d = {{alias}}( -2.0, 5.0, 'min' ) 61 3.5 62 > d = {{alias}}( -2.0, 5.0, 'mean-abs' ) 63 2.0 64 > d = {{alias}}( -2.0, 5.0, 'mean' ) 65 ~4.667 66 > d = {{alias}}( -2.0, 5.0, 'x' ) 67 3.5 68 > d = {{alias}}( 5.0, -2.0, 'x' ) 69 1.4 70 > d = {{alias}}( -2.0, 5.0, 'y' ) 71 1.4 72 > d = {{alias}}( 5.0, -2.0, 'y' ) 73 3.5 74 75 // Custom scale function: 76 > function scale( x, y ) { 77 ... var s; 78 ... 79 ... x = {{alias:@stdlib/math/base/special/abs}}( x ); 80 ... y = {{alias:@stdlib/math/base/special/abs}}( y ); 81 ... 82 ... // Maximum absolute value: 83 ... s = (x < y ) ? y : x; 84 ... 85 ... // Scale in units of epsilon: 86 ... return s * {{alias:@stdlib/constants/float64/eps}}; 87 ... }; 88 > d = {{alias}}( 12.15, 12.149999999999999, scale ) 89 ~0.658 90 91 See Also 92 -------- 93