Skip to content

Commit

Permalink
fix: add self review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed Jul 11, 2023
1 parent 33c24d1 commit 8c7e7ef
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 89 deletions.
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/math/base/special/croundn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
-->

# roundn
# croundn

> Round a double-precision complex floating-point number to the nearest multiple of `10^n`.
Expand Down Expand Up @@ -159,7 +159,7 @@ for ( i = 0; i < 100; i++ ) {
#include "stdlib/math/base/special/croundn.h"
```

#### stdlib_base_croundn( z )
#### stdlib_base_croundn( z, n )

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import croundn = require( './index' );

// TESTS //

// The function returns an array of numbers...
// The function returns a double-precision complex floating-point number...
{
croundn( new Complex128( 1.0, 2.0 ), -2 ); // $ExpectType Complex128
}

// The compiler throws an error if the function is provided a real component which is not a number...
// The compiler throws an error if the first argument is not a complex number...
{
croundn( true, 3 ); // $ExpectError
croundn( false, 3 ); // $ExpectError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#/
# @license Apache-2.0
#
# Copyright (c) 2021 The Stdlib Authors.
# 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ for ( i = 0; i < 100; i++ ) {
#include "stdlib/math/base/special/roundn.h"
```

#### stdlib_base_roundn( x )
#### stdlib_base_roundn( x, n )

Rounds a double-precision floating-point number to the nearest multiple of `10^n`.

Expand Down
161 changes: 85 additions & 76 deletions lib/node_modules/@stdlib/math/base/special/roundn/manifest.json
Original file line number Diff line number Diff line change
@@ -1,78 +1,87 @@
{
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"task": "build",
"src": [
"./src/roundn.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/special/abs"
]
},
{
"task": "benchmark",
"src": [
"./src/roundn.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/abs"
]
},
{
"task": "examples",
"src": [
"./src/roundn.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/abs"
]
}
]
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"task": "build",
"src": [
"./src/roundn.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/special/abs",
"@stdlib/constants/float64/max",
"@stdlib/constants/float64/max-base10-exponent",
"@stdlib/constants/float64/min-base10-exponent"
]
},
{
"task": "benchmark",
"src": [
"./src/roundn.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/abs",
"@stdlib/constants/float64/max",
"@stdlib/constants/float64/max-base10-exponent",
"@stdlib/constants/float64/min-base10-exponent"
]
},
{
"task": "examples",
"src": [
"./src/roundn.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/abs",
"@stdlib/constants/float64/max",
"@stdlib/constants/float64/max-base10-exponent",
"@stdlib/constants/float64/min-base10-exponent"
]
}
]
}
17 changes: 10 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/roundn/src/roundn.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include "stdlib/math/base/special/roundn.h"
#include "stdlib/math/base/special/abs.h"
#include "stdlib/constants/float64/max.h"
#include "stdlib/constants/float64/max_base10_exponent.h"
#include "stdlib/constants/float64/min_base10_exponent.h"
#include <stdint.h>
#include <math.h>

Expand Down Expand Up @@ -119,30 +122,30 @@ double stdlib_base_roundn( const double x, const int32_t n ) {
x == 0.0 ||

// If `n` exceeds the maximum number of feasible decimal places (such as with subnormal numbers), nothing to round...
n < __DBL_MIN_10_EXP__ ||
n < STDLIB_CONSTANT_FLOAT64_MIN_BASE10_EXPONENT ||

// If `|x|` is large enough, no decimals to round...
( stdlib_base_abs( x ) > __DBL_MAX__ && n <= 0 )
( stdlib_base_abs( x ) > STDLIB_CONSTANT_FLOAT64_MAX && n <= 0 )
) {
return x;
}
// The maximum absolute double is ~1.8e308. Accordingly, any possible finite `x` rounded to the nearest >=10^309 is 0.0.
if ( n > __DBL_MAX_10_EXP__ ){
if ( n > STDLIB_CONSTANT_FLOAT64_MAX_BASE10_EXPONENT ){
return 0.0 * x; // preserve the sign (same behavior as round)
}
// If we overflow, return `x`, as the number of digits to the right of the decimal is too small (i.e., `x` is too large / lacks sufficient fractional precision) for there to be any effect when rounding...
if ( n < __DBL_MIN_10_EXP__ ){
s = pow( 10.0, -( n + __DBL_MAX_10_EXP__ ) );
if ( n < STDLIB_CONSTANT_FLOAT64_MIN_BASE10_EXPONENT ){
s = pow( 10.0, -( n + STDLIB_CONSTANT_FLOAT64_MAX_BASE10_EXPONENT ) );
y = ( x * HUGE ) * s; // order of operation matters!
if ( isinf( y ) ){
return x;
}
return ( round( y ) / HUGE ) / s;
return ( round( y ) / HUGE ) / s; // TODO: replace with stdlib round function once implemented
}
s = pow( 10.0, -n );
y = x * s;
if( isinf( y ) ){
return x;
}
return round( y ) / s;
return round( y ) / s; // TODO: replace with stdlib round function once implemented
}

0 comments on commit 8c7e7ef

Please sign in to comment.