Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make base argument compulsory in math/base/special/floorsd #2617

Merged
merged 1 commit into from
Jul 18, 2024
Merged
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
16 changes: 6 additions & 10 deletions lib/node_modules/@stdlib/math/base/special/floorsd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,21 @@ limitations under the License.
var floorsd = require( '@stdlib/math/base/special/floorsd' );
```

#### floorsd( x, n\[, b] )
#### floorsd( x, n, b )

Rounds a `numeric` value to the nearest `number` toward negative infinity with `n` significant figures.

```javascript
var v = floorsd( 3.141592653589793, 5 );
var v = floorsd( 3.141592653589793, 5, 10 );
// returns 3.1415

v = floorsd( 3.141592653589793, 1 );
v = floorsd( 3.141592653589793, 1, 10 );
// returns 3.0

v = floorsd( 12368.0, 2 );
v = floorsd( 12368.0, 2, 10 );
// returns 12000.0
```

The default base is `10` (decimal). To round using a different base, provide a third argument.

```javascript
var v = floorsd( 0.0313, 2, 2 );
v = floorsd( 0.0313, 2, 2 );
// returns 0.03125
```

Expand Down Expand Up @@ -78,7 +74,7 @@ var i;

for ( i = 0; i < 100; i++ ) {
x = (randu()*10000.0) - 5000.0;
y = floorsd( x, 5 );
y = floorsd( x, 5, 10 );
console.log( 'x: %d. Rounded: %d.', x, y );
}
```
Expand Down
12 changes: 6 additions & 6 deletions lib/node_modules/@stdlib/math/base/special/floorsd/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{{alias}}( x, n[, b] )
{{alias}}( x, n, b )
Rounds a numeric value to the nearest number toward negative infinity with
`n` significant figures.

Expand All @@ -11,8 +11,8 @@
n: integer
Number of significant figures. Must be greater than 0.

b: integer (optional)
Base. Must be greater than 0. Default: 10.
b: integer
Base. Must be greater than 0.

Returns
-------
Expand All @@ -21,11 +21,11 @@

Examples
--------
> var y = {{alias}}( 3.14159, 5 )
> var y = {{alias}}( 3.14159, 5, 10 )
3.1415
> y = {{alias}}( 3.14159, 1 )
> y = {{alias}}( 3.14159, 1, 10 )
3.0
> y = {{alias}}( 12368.0, 2 )
> y = {{alias}}( 12368.0, 2, 10 )
12000.0
> y = {{alias}}( 0.0313, 2, 2 )
0.03125
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@
*
* @param x - input value
* @param n - number of significant figures
* @param b - base (default: 10)
* @param b - base
* @returns rounded value
*
* @example
* var v = floorsd( 3.141592653589793, 5 );
* var v = floorsd( 3.141592653589793, 5, 10 );
* // returns 3.1415
*
* @example
* var v = floorsd( 3.141592653589793, 1 );
* var v = floorsd( 3.141592653589793, 1, 10 );
* // returns 3.0
*
* @example
* var v = floorsd( 12368.0, 2 );
* var v = floorsd( 12368.0, 2, 10 );
* // returns 12000.0
*
* @example
* var v = floorsd( 0.0313, 2, 2 );
* // returns 0.03125
*/
declare function floorsd( x: number, n: number, b?: number ): number;
declare function floorsd( x: number, n: number, b: number ): number;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import floorsd = require( './index' );
// The function returns a number...
{
floorsd( 3.141592653589793, 4, 10 ); // $ExpectType number
floorsd( 3.141592653589793, 4 ); // $ExpectType number
}

// The compiler throws an error if the function is provided values other than numbers...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ var i;

for ( i = 0; i < 100; i++ ) {
x = (randu()*10000.0) - 5000.0;
y = floorsd( x, 5 );
y = floorsd( x, 5, 10 );
console.log( 'x: %d. Rounded: %d.', x, y );
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
* @example
* var floorsd = require( '@stdlib/math/base/special/floorsd' );
*
* var v = floorsd( 3.141592653589793, 5 );
* var v = floorsd( 3.141592653589793, 5, 10 );
* // returns 3.1415
*
* v = floorsd( 3.141592653589793, 1 );
* v = floorsd( 3.141592653589793, 1, 10 );
* // returns 3.0
*
* v = floorsd( 12368.0, 2 );
* v = floorsd( 12368.0, 2, 10 );
* // returns 12000.0
*
* v = floorsd( 0.0313, 2, 2 );
Expand Down
34 changes: 14 additions & 20 deletions lib/node_modules/@stdlib/math/base/special/floorsd/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,26 @@ var exponent = require( '@stdlib/number/float64/base/exponent' );
*
* @param {number} x - input value
* @param {PositiveInteger} n - number of significant figures
* @param {PositiveInteger} [b=10] - base
* @param {PositiveInteger} b - base
* @returns {number} rounded value
*
* @example
* var v = floorsd( 3.141592653589793, 5 );
* var v = floorsd( 3.141592653589793, 5, 10 );
* // returns 3.1415
*
* @example
* var v = floorsd( 3.141592653589793, 1 );
* var v = floorsd( 3.141592653589793, 1, 10 );
* // returns 3.0
*
* @example
* var v = floorsd( 12368.0, 2 );
* var v = floorsd( 12368.0, 2, 10 );
* // returns 12000.0
*
* @example
* var v = floorsd( 0.0313, 2, 2 );
* // returns 0.03125
*/
function floorsd( x, n, b ) {
var base;
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
var exp;
var s;
var y;
Expand All @@ -69,32 +68,27 @@ function floorsd( x, n, b ) {
) {
return NaN;
}
if ( arguments.length > 2 ) {
if (
isnan( b ) ||
b <= 0 ||
isInfinite( b )
) {
return NaN;
}
base = b;
} else {
base = 10;
if (
isnan( b ) ||
b <= 0 ||
isInfinite( b )
) {
return NaN;
}
if ( isInfinite( x ) || x === 0.0 ) {
return x;
}
if ( base === 10 ) {
if ( b === 10 ) {
exp = log10( abs( x ) );
}
else if ( base === 2 ) {
else if ( b === 2 ) {
exp = exponent( abs( x ) );
}
else {
exp = ln( abs(x) ) / ln( base );
exp = ln( abs(x) ) / ln( b );
}
exp = floor( exp - n + 1.0 );
s = pow( base, abs( exp ) );
s = pow( b, abs( exp ) );

// Check for overflow:
if ( isInfinite( s ) ) {
Expand Down
43 changes: 13 additions & 30 deletions lib/node_modules/@stdlib/math/base/special/floorsd/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ tape( 'main export is a function', function test( t ) {
tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
var v;

v = floorsd( NaN, 2 );
v = floorsd( NaN, 2, 10 );
t.strictEqual( isnan( v ), true, 'returns NaN' );

v = floorsd( 12368.0, NaN );
v = floorsd( 12368.0, NaN, 10 );
t.strictEqual( isnan( v ), true, 'returns NaN' );

v = floorsd( NaN, NaN );
v = floorsd( NaN, NaN, 10 );
t.strictEqual( isnan( v ), true, 'returns NaN' );

v = floorsd( NaN, NaN, 10 );
Expand All @@ -68,10 +68,10 @@ tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
tape( 'the function returns `NaN` if provided `n = +-infinity`', function test( t ) {
var v;

v = floorsd( PI, PINF );
v = floorsd( PI, PINF, 10 );
t.strictEqual( isnan( v ), true, 'returns NaN' );

v = floorsd( PI, NINF );
v = floorsd( PI, NINF, 10 );
t.strictEqual( isnan( v ), true, 'returns NaN' );

t.end();
Expand All @@ -80,10 +80,10 @@ tape( 'the function returns `NaN` if provided `n = +-infinity`', function test(
tape( 'the function returns `NaN` if provided `n < 1`', function test( t ) {
var v;

v = floorsd( PI, 0 );
v = floorsd( PI, 0, 10 );
t.strictEqual( isnan( v ), true, 'returns NaN' );

v = floorsd( PI, -1 );
v = floorsd( PI, -1, 10 );
t.strictEqual( isnan( v ), true, 'returns NaN' );

t.end();
Expand Down Expand Up @@ -114,24 +114,24 @@ tape( 'the function returns `NaN` if provided `b <= 0`', function test( t ) {
});

tape( 'the function returns `+infinity` if provided `+infinity`', function test( t ) {
var v = floorsd( PINF, 5 );
var v = floorsd( PINF, 5, 10 );
t.strictEqual( v, PINF, 'returns +infinity' );
t.end();
});

tape( 'the function returns `-infinity` if provided `-infinity`', function test( t ) {
var v = floorsd( NINF, 3 );
var v = floorsd( NINF, 3, 10 );
t.strictEqual( v, NINF, 'returns -infinity' );
t.end();
});

tape( 'the function returns `-0` if provided `-0`', function test( t ) {
var v;

v = floorsd( -0.0, 1 );
v = floorsd( -0.0, 1, 10 );
t.strictEqual( isNegativeZero( v ), true, 'returns -0' );

v = floorsd( -0.0, 2 );
v = floorsd( -0.0, 2, 10 );
t.strictEqual( isNegativeZero( v ), true, 'returns -0' );

t.end();
Expand All @@ -140,32 +140,15 @@ tape( 'the function returns `-0` if provided `-0`', function test( t ) {
tape( 'the function returns `+0` if provided `+0`', function test( t ) {
var v;

v = floorsd( 0.0, 1 );
v = floorsd( 0.0, 1, 10 );
t.strictEqual( isPositiveZero( v ), true, 'returns +0' );

v = floorsd( +0.0, 2 );
v = floorsd( +0.0, 2, 10 );
t.strictEqual( isPositiveZero( v ), true, 'returns +0' );

t.end();
});

tape( 'the function supports rounding a numeric value with a specified number of significant figures', function test( t ) {
t.strictEqual( floorsd( PI, 1 ), 3.0, 'returns expected value' );
t.strictEqual( floorsd( -PI, 1 ), -4.0, 'returns expected value' );
t.strictEqual( floorsd( PI, 2 ), 3.1, 'returns expected value' );
t.strictEqual( floorsd( -PI, 2 ), -3.2, 'returns expected value' );
t.strictEqual( floorsd( PI, 3 ), 3.14, 'returns expected value' );
t.strictEqual( floorsd( -PI, 3 ), -3.15, 'returns expected value' );
t.strictEqual( floorsd( PI, 5 ), 3.1415, 'returns expected value' );
t.strictEqual( floorsd( -PI, 5 ), -3.1416, 'returns expected value' );
t.strictEqual( floorsd( 0.0, 2 ), 0.0, 'returns expected value' );
t.strictEqual( floorsd( 12368.0, 3 ), 12300.0, 'returns expected value' );
t.strictEqual( floorsd( -12368.0, 3 ), -12400.0, 'returns expected value' );
t.strictEqual( floorsd( 12368.0, 2 ), 12000.0, 'returns expected value' );
t.strictEqual( floorsd( -12368.0, 2 ), -13000.0, 'returns expected value' );
t.end();
});

tape( 'the function supports rounding a numeric value with a specified number of significant figures (base 10)', function test( t ) {
t.strictEqual( floorsd( PI, 1, 10 ), 3.0, 'returns expected value' );
t.strictEqual( floorsd( -PI, 1, 10 ), -4.0, 'returns expected value' );
Expand Down
Loading