Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 241 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/croundnf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<!--

@license Apache-2.0

Copyright (c) 2026 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.

-->

# croundnf

> Round each component of a single-precision complex floating-point number to the nearest multiple of `10^n`.

<section class="usage">

## Usage

```javascript
var croundnf = require( '@stdlib/math/base/special/croundnf' );
```

#### croundnf( z, n )

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

```javascript
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var v = croundnf( new Complex64( -3.141592653589793, 3.141592653589793 ), -2 );
// returns <Complex64>[ ~-3.14, ~3.14 ]

v = croundnf( new Complex64( -3.141592653589793, 3.141592653589793 ), 0 );
// returns <Complex64>[ -3.0, 3.0 ]

v = croundnf( new Complex64( -12368.0, 12368.0 ), 3 );
// returns <Complex64>[ ~-12000.0, ~12000.0 ]

v = croundnf( new Complex64( NaN, NaN ), 3 );
// returns <Complex64>[ NaN, NaN ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- When operating on [floating-point numbers][ieee754] in bases other than `2`, rounding to specified digits can be **inexact**. For example,

```javascript
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var x = 0.2 + 0.1;
// returns 0.30000000000000004

// Should round components to 0.3:
var v = croundnf( new Complex64( x, x ), -7 );
// returns <Complex64>[ 0.30000001192092896, 0.30000001192092896 ]
```

- Ties are rounded toward positive infinity.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var uniform = require( '@stdlib/random/base/uniform' ).factory;
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var floor = require( '@stdlib/math/base/special/floor' );
var croundnf = require( '@stdlib/math/base/special/croundnf' );

var rand1 = uniform( -5.0, 0.0 );
var rand2 = uniform( -50.0, 50.0 );

var z;
var i;
var n;
for ( i = 0; i < 100; i++ ) {
z = new Complex64( rand2(), rand2() );
n = floor( rand1() );
console.log( 'croundnf(%s, %s) = %s', z, n, croundnf( z, n ) );
}
```

</section>

<!-- /.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/croundnf.h"
```

#### stdlib_base_croundnf( z, n )

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

```c
#include "stdlib/complex/float32/ctor.h"
#include "stdlib/complex/float32/real.h"
#include "stdlib/complex/float32/imag.h"

stdlib_complex64_t z = stdlib_complex64( -3.141592653589793f, 3.141592653589793f );

stdlib_complex64_t out = stdlib_base_croundnf( z, -2 );

float re = stdlib_complex64_real( out );
// returns ~-3.14f

float im = stdlib_complex64_imag( out );
// returns ~3.14f
```

The function accepts the following arguments:

- **z**: `[in] stdlib_complex64_t` input value.
- **n**: `[in] int32_t` integer power of 10.

```c
stdlib_complex64_t stdlib_base_croundnf( const stdlib_complex64_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/croundnf.h"
#include "stdlib/complex/float32/ctor.h"
#include "stdlib/complex/float32/reim.h"
#include <stdio.h>

int main( void ) {
const stdlib_complex64_t x[] = {
stdlib_complex64( 3.14f, 1.5f ),
stdlib_complex64( -3.14f, -1.5f ),
stdlib_complex64( 0.0f, 0.0f ),
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
};

stdlib_complex64_t v;
stdlib_complex64_t y;
float re1;
float im1;
float re2;
float im2;
int i;
for ( i = 0; i < 4; i++ ) {
v = x[ i ];
y = stdlib_base_croundnf( v, -2 );
stdlib_complex64_reim( v, &re1, &im1 );
stdlib_complex64_reim( y, &re2, &im2 );
printf( "croundnf(%f + %fi, -2) = %f + %fi\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">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var floor = require( '@stdlib/math/base/special/floor' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var realf = require( '@stdlib/complex/float32/real' );
var imagf = require( '@stdlib/complex/float32/imag' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var croundnf = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var xre;
var xim;
var n;

xre = uniform( len, -500.0, 500.0, {
'dtype': 'float32'
});
xim = uniform( len, -500.0, 500.0, {
'dtype': 'float32'
});
n = filledarrayBy( len, 'generic', discreteUniform );

return benchmark;

/**
* Returns a pseudorandom integer.
*
* @private
* @returns {integer} pseudorandom integer
*/
function discreteUniform() {
return floor( ( randu()*10.0 ) - 5.0 );
}

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var z;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = new Complex64( xre[ i%len ], xim[ i%len ] );
y = croundnf( z, n[ i%len ] );
if ( isnanf( realf( y ) ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( realf( y ) ) || isnanf( imagf( y ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

bench( pkg, createBenchmark( 10 ) );

bench( format( '%s::len=%d', pkg, 100 ), createBenchmark( 100 ) );
Loading