Skip to content

Latest commit

 

History

History
241 lines (155 loc) · 5.38 KB

File metadata and controls

241 lines (155 loc) · 5.38 KB

croundnf

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

Usage

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.

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 ]

Notes

  • When operating on floating-point numbers in bases other than 2, rounding to specified digits can be inexact. For example,

    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.

Examples

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 ) );
}

C APIs

Usage

#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.

#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.
stdlib_complex64_t stdlib_base_croundnf( const stdlib_complex64_t z, const int32_t n );

Examples

#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 );
    }
}