Skip to content

Commit

Permalink
feat: add native addon and C implementation to croundn API
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed May 16, 2023
1 parent 2c5c0d3 commit 33c24d1
Show file tree
Hide file tree
Showing 17 changed files with 1,655 additions and 76 deletions.
152 changes: 76 additions & 76 deletions lib/node_modules/@stdlib/math/base/special/cceil/manifest.json
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
{
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"task": "build",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/complex/float64",
"@stdlib/complex/reim",
"@stdlib/math/base/special/ceil"
]
},
{
"task": "benchmark",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/complex/float64",
"@stdlib/complex/reim",
"@stdlib/math/base/special/ceil"
]
},
{
"task": "examples",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/complex/float64",
"@stdlib/complex/reim",
"@stdlib/math/base/special/ceil"
]
}
]
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"task": "build",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/complex/float64",
"@stdlib/complex/reim",
"@stdlib/math/base/special/ceil"
]
},
{
"task": "benchmark",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/complex/float64",
"@stdlib/complex/reim",
"@stdlib/math/base/special/ceil"
]
},
{
"task": "examples",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/complex/float64",
"@stdlib/complex/reim",
"@stdlib/math/base/special/ceil"
]
}
]
}

112 changes: 112 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/croundn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,118 @@ for ( i = 0; i < 100; i++ ) {

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/math/base/special/croundn.h"
```

#### stdlib_base_croundn( z )

Rounds a double-precision complex floating-point number to the nearest multiple of `10^n`.

```c
#include "stdlib/complex/float64.h"
#include "stdlib/complex/real.h"
#include "stdlib/complex/imag.h"
stdlib_complex128_t z = stdlib_complex128( -3.141592653589793, 3.141592653589793 );
stdlib_complex128_t out = stdlib_base_croundn( z );
double re = stdlib_real( out );
// returns -3.14
double im = stdlib_imag( out );
// returns 3.14
```

The function accepts the following arguments:

- **z**: `[in] stdlib_complex128_t` input value.
- **n**: `[in] int32_t` input value.

```c
stdlib_complex128_t stdlib_base_croundn( const stdlib_complex128_t z, const int32_t n );
```
</section>
<!-- /.usage -->
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">
</section>
<!-- /.notes -->
<!-- C API usage examples. -->
<section class="examples">
### Examples
```c
#include "stdlib/math/base/special/croundn.h"
#include "stdlib/complex/float64.h"
#include "stdlib/complex/reim.h"
#include <stdio.h>

int main( void ) {
const stdlib_complex128_t x[] = {
stdlib_complex128( 3.14, 1.5 ),
stdlib_complex128( -3.14, -1.5 ),
stdlib_complex128( 0.0, 0.0 ),
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
};

stdlib_complex128_t v;
stdlib_complex128_t y;
double re1;
double im1;
double re2;
double im2;
int i;
for ( i = 0; i < 4; i++ ) {
v = x[ i ];
y = stdlib_base_croundn( v, -2 );
stdlib_reim( v, &re1, &im1 );
stdlib_reim( y, &re2, &im2 );
printf( "croundn(%lf + %lfi) = %lf + %lfi\n", re1, im1, re2, im2 );
}
}
```
</section>
<!-- /.examples -->
</section>
<!-- /.c -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var Complex128 = require( '@stdlib/complex/float64' );
var real = require( '@stdlib/complex/real' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var croundn = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( croundn instanceof Error )
};


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var values;
var y;
var i;

values = [
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = croundn( values[ i%values.length ], -2 );
if ( isnan( real( y ) ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( real( y ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading

0 comments on commit 33c24d1

Please sign in to comment.