Skip to content

Latest commit

 

History

History
343 lines (256 loc) · 7.95 KB

README.md

File metadata and controls

343 lines (256 loc) · 7.95 KB

roundn

NPM version Build Status Coverage Status Dependencies

Round values to the nearest multiple of 10^n.

Installation

$ npm install compute-roundn

For use in the browser, use browserify.

Usage

var roundn = require( 'compute-roundn' );

roundn( x[, n[, options ] ] )

Rounds values to the nearest multiple of 10^n. x may be either a number, an array, a typed array, or a matrix. . n must be an integer specifying the multiple of 10^n to which the value(s) should be rounded. If n and options are not supplied, the function behaves like Math.round.

var matrix = require( 'dstructs-matrix' ),
	data,
	mat,
	out,
	i;

out = roundn( 2.32 );
// returns 2

out = roundn( 2.4567, -2 );
// returns 2.46

out = roundn( 12368, 3 )
// returns 12,000

data = [ Math.PI, Math.PI, Math.PI ];
out = roundn( data, -2 );
// returns [ 3.14, 3.14, 3.14 ]

data = new Float64Array( data );
out = roundn( data, -2 );
// returns Float64Array( [3.14, 3.14, 3.14] )

data = new Float64Array( 6 );
for ( i = 0; i < 6; i++ ) {
	data[ i ] = Math.PI;
}
mat = matrix( data, [3,2], 'float64' );
/*
	[  Math.PI Math.PI
	   Math.PI Math.PI
	   Math.PI Math.PI ]
*/

out = roundn( mat, -2 );
/*
	[ 3.14 3.14
	  3.14 3.14
	  3.14 3.14 ]
*/

The function accepts the following options:

  • accessor: accessor function for accessing array values.
  • dtype: output typed array or matrix data type. Default: float64.
  • copy: boolean indicating if the function should return a new data structure. Default: true.
  • path: deepget/deepset key path.
  • sep: deepget/deepset key path separator. Default: '.'.

For non-numeric arrays, provide an accessor function for accessing array values.

data = [
	{'x': Math.PI},
	{'x': Math.PI},
	{'x': Math.PI},
	{'x': Math.PI},
	{'x': Math.PI},
];
function getValue( d, i ) {
	return d.x;
}

var out = roundn( data, -2,  {
	'accessor': getValue
});
// returns [ 3.14, 3.14, 3.14, 3.14, 3.14 ]

To deepset an object array, provide a key path and, optionally, a key path separator.

data = [
	{'x':[9,Math.PI]},
	{'x':[9,Math.PI]},
	{'x':[9,Math.PI]},
	{'x':[9,Math.PI]},
	{'x':[9,Math.PI]},
	{'x':[9,Math.PI]}
];

var out = roundn( data, -2, {
	'path': 'x|1',
	'sep': '|'
});
/*
	[
		{'x':[9,3.14]},
		{'x':[9,3.14]},
		{'x':[9,3.14]},
		{'x':[9,3.14]},
		{'x':[9,3.14]},
		{'x':[9,3.14]}
	]
*/

var bool = ( data === out );
// returns true

By default, when provided a typed array or matrix, the output data structure is float64 in order to preserve precision. To specify a different data type, set the dtype option (see matrix for a list of acceptable data types).

var data, out;

data = new Float32Array( [ -0.1, 0.1, 10, -10] );

out = abs( data, {
	'dtype': 'int32'
});
// returns Int32Array( [0, 0, 10, 10] )

// Works for plain arrays, as well...
out = abs( [ -0.1, 0.1, 10, -10], {
	'dtype': 'uint8'
});
// returns Uint8Array( [0, 0, 10, 10] )

By default, the function returns a new data structure. To mutate the input data structure, set the copy option to false.

var data,
	bool,
	mat,
	out,
	i;

data = [ Math.PI, Math.PI, Math.PI ];

out = roundn( data, -2, {
	'copy': false
});
// returns [ 3.14, 3.14, 3.14 ]

bool = ( data === out );
// returns true

data = new Float64Array( 6 );
for ( i = 0; i < 6; i++ ) {
	data[ i ] = Math.PI;
}
mat = matrix( data, [3,2], 'float64' );
/*
	[  Math.PI Math.PI
	   Math.PI Math.PI
	   Math.PI Math.PI ]
*/

out = roundn( mat, -2, {
	'copy': false
});
/*
	[ 3.14 3.14
	  3.14 3.14
	  3.14 3.14 ]
*/

bool = ( mat === out );
// returns true

Notes

  • If an element is not a numeric value, the element's absolute value is NaN.

    var data, out;
    
    out = roundn( null );
    // returns NaN
    
    out = roundn( true );
    // returns NaN
    
    out = roundn( {'a':'b'} );
    // returns NaN
    
    out = roundn( [ -1, null, -2 ] );
    // returns [ -1, NaN, -2 ]
    
    function getValue( d, i ) {
    	return d.x;
    }
    data = [
    	{'x': Math.PI},
    	{'x': Math.PI},
    	{'x': Math.PI},
    	{'x': Math.PI},
    	{'x': Math.PI},
    	{'x': null}
    ];
    
    out = roundn( data, -2, {
    	'accessor': getValue
    });
    // returns [ 3.14, 3.14, 3.14, 3.14, 3.14, NaN ]
    
    out = roundn( data, -2, {
    	'path': 'x'
    });
    /*
    	[
    		{'x': 3.14},
    		{'x': 3.14},
    		{'x': 3.14},
    		{'x': 3.14},
    		{'x': 3.14},
    		{'x': NaN}
    	]
    */
  • Be careful when providing a data structure which contains non-numeric elements and specifying an integer output array, as NaN values are cast to 0.

    var out = roundn( [ -1, null, -2 ], 0, {
    	'dtype': 'int8'
    });
    // returns Int8Array( [-1,0,-2] );

Examples

// Round a value to 2 decimal places:
console.log( roundn( Math.PI, -2 ) );
// returns 3.14

// If `n=0`, then `roundn()` behaves like `Math.round()`:
console.log( roundn( Math.PI, 0 ) );
// returns 3

console.log( Math.round( Math.PI ) );
// returns 3

// Round a value to the nearest thousand:
console.log( roundn( 12368, 3 ) );
// returns 12000

// Round each array value to 2 decimal places...
var data = new Array( 5 );

for ( var i = 0; i < data.length; i++ ) {
	data[ i ] = Math.PI;
}

console.log( roundn( data, -2 ) );
// returns [ 3.14, 3.14, 3.14, 3.14, 3.14 ]

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2014-2015. The Compute.io Authors.