Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
171 changes: 146 additions & 25 deletions lib/node_modules/@stdlib/math/base/special/cfloor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.

# Floor

> Round a complex number toward negative infinity.
> Round a double-precision complex floating-point number toward negative infinity.

<section class="usage">

Expand All @@ -30,36 +30,50 @@ limitations under the License.
var cfloor = require( '@stdlib/math/base/special/cfloor' );
```

#### cfloor( \[out,] re, im )
#### cfloor( z )

Rounds a `complex` number comprised of a **real** component `re` and an **imaginary** component `im` toward negative infinity.
Rounds a double-precision complex floating-point number toward negative infinity.

```javascript
var v = cfloor( -4.2, 5.5 );
// returns [ -5.0, 5.0 ]
var Complex128 = require( '@stdlib/complex/float64' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );

v = cfloor( 9.99999, 0.1 );
// returns [ 9.0, 0.0 ]
var v = cfloor( new Complex128( -4.2, 5.5 ) );
// returns <Complex128>

v = cfloor( 0.0, 0.0 );
// returns [ 0.0, 0.0 ]
var re = real( v );
// returns -5.0

v = cfloor( NaN, NaN );
// returns [ NaN, NaN ]
```
var im = imag( v );
// returns 5.0

By default, the function returns real and imaginary components as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
v = cfloor( new Complex128( 9.99999, 0.1 ) );
// returns <Complex128>

```javascript
var Float32Array = require( '@stdlib/array/float32' );
re = real( v );
// returns 9.0

im = imag( v );
// returns 0.0

v = cfloor( new Complex128( 0.0, 0.0 ) );
// returns <Complex128>

re = real( v );
// returns 0.0

var out = new Float32Array( 2 );
im = imag( v );
// returns 0.0

var v = cfloor( out, -4.2, 5.5 );
// returns <Float32Array>[ -5.0, 5.0 ]
v = cfloor( new Complex128( NaN, NaN ) );
// returns <Complex128>

var bool = ( v === out );
// returns true
re = real( v );
// returns NaN

im = imag( v );
// returns NaN
```

</section>
Expand All @@ -75,23 +89,19 @@ var bool = ( v === out );
```javascript
var Complex128 = require( '@stdlib/complex/float64' );
var randu = require( '@stdlib/random/base/randu' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var cfloor = require( '@stdlib/math/base/special/cfloor' );

var re;
var im;
var z;
var o;
var w;
var i;

for ( i = 0; i < 100; i++ ) {
re = ( randu()*100.0 ) - 50.0;
im = ( randu()*100.0 ) - 50.0;
z = new Complex128( re, im );
o = cfloor( real(z), imag(z) );
w = new Complex128( o[ 0 ], o[ 1 ] );
w = cfloor( z );
console.log( 'floor(%s) = %s', z.toString(), w.toString() );
}
```
Expand All @@ -100,6 +110,117 @@ 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/cfloor.h"
```

#### stdlib_base_cfloor( z )

Rounds a double-precision complex floating-point number toward negative infinity.

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

stdlib_complex128_t z = stdlib_complex128( 2.5, -1.5 );

stdlib_complex128_t out = stdlib_base_cfloor( z );

double re = stdlib_real( out );
// returns 2.0

double im = stdlib_imag( out );
// returns -2.0
```

The function accepts the following arguments:

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

```c
stdlib_complex128_t stdlib_base_cfloor( const stdlib_complex128_t z );
```

</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/cfloor.h"
#include "stdlib/complex/float64.h"
#include "stdlib/complex/reim.h"
#include <stdio.h>

int main() {
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_cfloor( v );
stdlib_reim( v, &re1, &im1 );
stdlib_reim( y, &re2, &im2 );
printf( "cfloor(%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
Expand Up @@ -22,6 +22,11 @@

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var Complex128 = require( '@stdlib/complex/float64' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var uniform = require( '@stdlib/random/base/uniform' );
var isArray = require( '@stdlib/assert/is-array' );
var floor = require( '@stdlib/math/base/special/floor' );
var pkg = require( './../package.json' ).name;
Expand All @@ -31,49 +36,25 @@ var cfloor = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var re;
var im;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = cfloor( re, im );
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
}
}
b.toc();
if ( !isArray( y ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::memory_reuse', function benchmark( b ) {
var out;
var re;
var im;
var values;
var y;
var i;

out = new Array( 2 );
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++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = cfloor( out, re, im );
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
y = cfloor( ( values[ i%values.length ] ) );
if ( isnan( real( y ) ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( !isArray( y ) ) {
b.fail( 'should return an array' );
if ( isnan( imag( y ) ) ) {
b.fail( 'should not return not NaN' );
Comment thread
kgryte marked this conversation as resolved.
Outdated
}
b.pass( 'benchmark finished' );
b.end();
Expand Down Expand Up @@ -101,29 +82,3 @@ bench( pkg+'::manual', function benchmark( b ) {
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::manual,memory_reuse', function benchmark( b ) {
var re;
var im;
var y;
var i;

y = new Array( 2 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y[ 0 ] = floor( re );
y[ 1 ] = floor( im );
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
}
}
b.toc();
if ( !isArray( y ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
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 cfloor = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( cfloor 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 = cfloor( values[ i%values.length ] );
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