Skip to content

Commit c45c37c

Browse files
committed
Add Typescript definitions
1 parent 9429360 commit c45c37c

File tree

9 files changed

+465
-0
lines changed

9 files changed

+465
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Computes the absolute difference.
23+
*
24+
* @param x - first number
25+
* @param y - second number
26+
* @returns absolute difference
27+
*
28+
* @example
29+
* var d = absoluteDifference( 2.0, 5.0 );
30+
* // returns 3.0
31+
*
32+
* @example
33+
* var d = absoluteDifference( -1.0, 3.14 );
34+
* // returns ~4.14
35+
*
36+
* @example
37+
* var d = absoluteDifference( 10.1, -2.05 );
38+
* // returns ~12.15
39+
*
40+
* @example
41+
* var d = absoluteDifference( -0.0, 0.0 );
42+
* // returns +0.0
43+
*
44+
* @example
45+
* var d = absoluteDifference( NaN, 5.0 );
46+
* // returns NaN
47+
*
48+
* @example
49+
* var d = absoluteDifference( Infinity, -Infinity );
50+
* // returns Infinity
51+
*
52+
* @example
53+
* var d = absoluteDifference( Infinity, Infinity );
54+
* // returns NaN
55+
*/
56+
declare function absoluteDifference( x: number, y: number ): number;
57+
58+
59+
// EXPORTS //
60+
61+
export = absoluteDifference;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
import absoluteDifference = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
absoluteDifference( 8, 2 ); // $ExpectType number
27+
}
28+
29+
// The function does not compile if provided values other than two numbers...
30+
{
31+
absoluteDifference( true, 3 ); // $ExpectError
32+
absoluteDifference( false, 2 ); // $ExpectError
33+
absoluteDifference( '5', 1 ); // $ExpectError
34+
absoluteDifference( [], 1 ); // $ExpectError
35+
absoluteDifference( {}, 2 ); // $ExpectError
36+
absoluteDifference( ( x: number ): number => x, 2 ); // $ExpectError
37+
38+
absoluteDifference( 9, true ); // $ExpectError
39+
absoluteDifference( 9, false ); // $ExpectError
40+
absoluteDifference( 5, '5' ); // $ExpectError
41+
absoluteDifference( 8, [] ); // $ExpectError
42+
absoluteDifference( 9, {} ); // $ExpectError
43+
absoluteDifference( 8, ( x: number ): number => x ); // $ExpectError
44+
45+
absoluteDifference( [], true ); // $ExpectError
46+
absoluteDifference( {}, false ); // $ExpectError
47+
absoluteDifference( false, '5' ); // $ExpectError
48+
absoluteDifference( {}, [] ); // $ExpectError
49+
absoluteDifference( '5', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The function does not compile if provided insufficient arguments...
53+
{
54+
absoluteDifference(); // $ExpectError
55+
absoluteDifference( 3 ); // $ExpectError
56+
}

lib/node_modules/@stdlib/math/base/utils/absolute-difference/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Custom scale function.
23+
*
24+
* @param x - first value
25+
* @param y - second value
26+
* @returns custom scale
27+
*/
28+
type ScaleFunction = ( x: number, y: number ) => number;
29+
30+
/**
31+
* Scale function name.
32+
*
33+
* ## Notes
34+
*
35+
* - The following `scale` functions are supported:
36+
*
37+
* - 'max-abs': maximum absolute value of `x` and `y` (default).
38+
* - 'max': maximum value of `x` and `y`.
39+
* - 'min-abs': minimum absolute value of `x` and `y`.
40+
* - 'min': minimum value of `x` and `y`.
41+
* - 'mean-abs': arithmetic mean of the absolute values of `x` and `y`.
42+
* - 'mean': arithmetic mean of `x` and `y`.
43+
* - 'x': `x` (*noncommutative*).
44+
* - 'y': `y` (*noncommutative*).
45+
*/
46+
type ScaleNames = 'max-abs' | 'max' | 'min-abs' | 'min' | 'mean-abs' | 'mean' | 'x' | 'y'; // tslint-disable-line max-line-length
47+
48+
/**
49+
* Computes the relative difference in units of double-precision floating-point epsilon.
50+
*
51+
* ## Notes
52+
*
53+
* - By default, the function scales the absolute difference by dividing the absolute difference by the maximum absolute value of `x` and `y`. To scale by a different function, specify a scale function name or custom scale function.
54+
* - If computing the relative difference in units of epsilon will result in overflow, the function returns the maximum double-precision floating-point number.
55+
* - If the absolute difference of `x` and `y` is `0`, the relative difference is always `0`.
56+
* - If `|x| = |y| = infinity`, the function returns `NaN`.
57+
* - If `|x| = |-y| = infinity`, the relative difference is `+infinity`.
58+
* - If a `scale` function returns `0`, the function returns `NaN`.
59+
*
60+
* @param x - first number
61+
* @param y - second number
62+
* @param scale - scale function (default: 'max-abs')
63+
* @returns relative difference in units of double-precision floating-point epsilon
64+
*
65+
* @example
66+
* var d = epsilonDifference( 12.15, 12.149999999999999 ); // => ~0.658ε
67+
* // returns ~0.658
68+
*
69+
* @example
70+
* var d = epsilonDifference( 2.4341309458983933, 2.4341309458633909, 'mean-abs' ); // => ~64761.5ε => ~1.438e-11
71+
* // returns ~64761.5
72+
*
73+
* @example
74+
* function scale( x, y ) {
75+
* // Return the minimum value:
76+
* return ( x > y ) ? y : x;
77+
* }
78+
*
79+
* var d = epsilonDifference( 1.0000000000000002, 1.0000000000000100, scale ); // => ~44ε
80+
* // returns ~44
81+
*/
82+
declare function epsilonDifference( x: number, y: number, scale?: ScaleFunction | ScaleNames ): number; // tslint-disable-line max-line-length
83+
84+
85+
// EXPORTS //
86+
87+
export = epsilonDifference;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
import epsilonDifference = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
const scale = ( x: number ): number => x;
27+
epsilonDifference( 8.0, 2.0 ); // $ExpectType number
28+
epsilonDifference( 8.0, 2.0, 'mean' ); // $ExpectType number
29+
epsilonDifference( 8.0, 2.0, scale ); // $ExpectType number
30+
}
31+
32+
// The function does not compile if provided values other than two numbers...
33+
{
34+
epsilonDifference( true, 3 ); // $ExpectError
35+
epsilonDifference( false, 2 ); // $ExpectError
36+
epsilonDifference( '5', 1 ); // $ExpectError
37+
epsilonDifference( [], 1 ); // $ExpectError
38+
epsilonDifference( {}, 2 ); // $ExpectError
39+
epsilonDifference( ( x: number ): number => x, 2 ); // $ExpectError
40+
41+
epsilonDifference( 9, true ); // $ExpectError
42+
epsilonDifference( 9, false ); // $ExpectError
43+
epsilonDifference( 5, '5' ); // $ExpectError
44+
epsilonDifference( 8, [] ); // $ExpectError
45+
epsilonDifference( 9, {} ); // $ExpectError
46+
epsilonDifference( 8, ( x: number ): number => x ); // $ExpectError
47+
48+
epsilonDifference( [], true ); // $ExpectError
49+
epsilonDifference( {}, false ); // $ExpectError
50+
epsilonDifference( false, '5' ); // $ExpectError
51+
epsilonDifference( {}, [] ); // $ExpectError
52+
epsilonDifference( '5', ( x: number ): number => x ); // $ExpectError
53+
}
54+
55+
// The function does not compile if provided a third argument which is not a recognized scale function name or custom scale function...
56+
{
57+
epsilonDifference( 2.0, 5.0, 'abc' ); // $ExpectError
58+
epsilonDifference( 2.0, 5.0, 123 ); // $ExpectError
59+
epsilonDifference( 2.0, 5.0, true ); // $ExpectError
60+
epsilonDifference( 2.0, 5.0, false ); // $ExpectError
61+
epsilonDifference( 2.0, 5.0, [] ); // $ExpectError
62+
epsilonDifference( 2.0, 5.0, {} ); // $ExpectError
63+
epsilonDifference( 2.0, 5.0, ( wrong: string ): string => wrong ); // $ExpectError
64+
}
65+
66+
// The function does not compile if provided insufficient arguments...
67+
{
68+
epsilonDifference(); // $ExpectError
69+
epsilonDifference( 3 ); // $ExpectError
70+
}

lib/node_modules/@stdlib/math/base/utils/float64-epsilon-difference/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {

0 commit comments

Comments
 (0)