Skip to content

Commit 5bad5d8

Browse files
committed
feat: add number/float64/base/identity
Ref: #2261 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent a0ae68f commit 5bad5d8

File tree

25 files changed

+1769
-0
lines changed

25 files changed

+1769
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Identity Function
22+
23+
> Evaluate the [identity function][identity-function] of a double-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [identity-function][identity-function] is defined as
28+
29+
<!-- <equation class="equation" label="eq:identity_function" align="center" raw="f(x) = x" alt="Identity function"> -->
30+
31+
```math
32+
f(x) = x
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="f(x) = x" data-equation="eq:identity_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@ad7afa5d7ec1b1596f8a4828153d8c2e87a90161/lib/node_modules/@stdlib/number/float64/base/identity/docs/img/equation_identity_function.svg" alt="Identity function">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
for all `x`.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var identity = require( '@stdlib/number/float64/base/identity' );
54+
```
55+
56+
#### identity( x )
57+
58+
Evaluates the [identity function][identity-function] for a double-precision floating-point number.
59+
60+
```javascript
61+
var v = identity( -1.0 );
62+
// returns -1.0
63+
64+
v = identity( 2.0 );
65+
// returns 2.0
66+
67+
v = identity( 0.0 );
68+
// returns 0.0
69+
70+
v = identity( -0.0 );
71+
// returns -0.0
72+
73+
v = identity( NaN );
74+
// returns NaN
75+
```
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var randu = require( '@stdlib/random/base/randu' );
89+
var round = require( '@stdlib/math/base/special/round' );
90+
var identity = require( '@stdlib/number/float64/base/identity' );
91+
92+
var rand;
93+
var i;
94+
95+
for ( i = 0; i < 100; i++ ) {
96+
rand = round( randu() * 100.0 ) - 50.0;
97+
console.log( 'identity(%d) = %d', rand, identity( rand ) );
98+
}
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- C interface documentation. -->
106+
107+
* * *
108+
109+
<section class="c">
110+
111+
## C APIs
112+
113+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
114+
115+
<section class="intro">
116+
117+
</section>
118+
119+
<!-- /.intro -->
120+
121+
<!-- C usage documentation. -->
122+
123+
<section class="usage">
124+
125+
### Usage
126+
127+
```c
128+
#include "stdlib/number/float64/base/identity.h"
129+
```
130+
131+
#### stdlib_base_float64_identity( x )
132+
133+
Evaluates the identity function for a double-precision floating-point number.
134+
135+
```c
136+
double y = stdlib_base_float64_identity( 2.0 );
137+
// returns 2.0
138+
```
139+
140+
The function accepts the following arguments:
141+
142+
- **x**: `[in] double` input value.
143+
144+
```c
145+
double stdlib_base_float64_identity( const double x );
146+
```
147+
148+
</section>
149+
150+
<!-- /.usage -->
151+
152+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="notes">
155+
156+
</section>
157+
158+
<!-- /.notes -->
159+
160+
<!-- C API usage examples. -->
161+
162+
<section class="examples">
163+
164+
### Examples
165+
166+
```c
167+
#include "stdlib/number/float64/base/identity.h"
168+
#include <stdio.h>
169+
170+
int main( void ) {
171+
const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 };
172+
173+
double y;
174+
int i;
175+
for ( i = 0; i < 4; i++ ) {
176+
y = stdlib_base_float64_identity( x[ i ] );
177+
printf( "f(%lf) = %lf\n", x[ i ], y );
178+
}
179+
}
180+
```
181+
182+
</section>
183+
184+
<!-- /.examples -->
185+
186+
</section>
187+
188+
<!-- /.c -->
189+
190+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
191+
192+
<section class="related">
193+
194+
</section>
195+
196+
<!-- /.related -->
197+
198+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
199+
200+
<section class="links">
201+
202+
[identity-function]: https://en.wikipedia.org/wiki/Identity_function
203+
204+
</section>
205+
206+
<!-- /.links -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var identity = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
x = ( randu()*1000.0 ) - 500.0;
40+
y = identity( x );
41+
if ( isnan( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var identity = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( identity instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
x = ( randu()*1000.0 ) - 500.0;
49+
y = identity( x );
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)