Skip to content

Commit 138b658

Browse files
committed
fix: use single-precision functions and handle overflow in truncbf
- Replace pow/trunc with powf/truncf for consistency with float32 operations - Add overflow handling when scale factor b^n becomes infinite - Remove unused double-precision dependencies from package.json - Add test case for scale factor overflow - Ensure C implementation matches JavaScript precision Addresses maintainer feedback: - Uses single-precision packages (powf, truncf) - NAPI macro already correctly implemented - manifest.json structure already correct Closes #649
1 parent 8935b9e commit 138b658

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

lib/node_modules/@stdlib/math/base/special/truncbf/lib/main.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2424
var isInfinitef = require( '@stdlib/math/base/assert/is-infinitef' );
25-
var pow = require( '@stdlib/math/base/special/pow' );
26-
var trunc = require( '@stdlib/math/base/special/trunc' );
25+
var powf = require( '@stdlib/math/base/special/powf' );
26+
var truncf = require( '@stdlib/math/base/special/truncf' );
2727
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
2828

2929

@@ -70,11 +70,16 @@ function truncbf( x, n, b ) {
7070
return NaN;
7171
}
7272

73-
// Compute scale factor: s = b^n
74-
s = pow( b, n );
73+
// Compute scale factor in single precision: s = b^n
74+
s = powf( float64ToFloat32( b ), float64ToFloat32( n ) );
75+
76+
// Handle overflow in scale factor
77+
if ( !isFinite( s ) ) {
78+
return x;
79+
}
7580

7681
// Multiply by scale, truncate, then divide by scale
77-
y = trunc( x * s ) / s;
82+
y = truncf( x * s ) / s;
7883

7984
return float64ToFloat32( y );
8085
}

lib/node_modules/@stdlib/math/base/special/truncbf/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
"@stdlib/math/base/assert/is-infinitef": "^0.2.2",
3939
"@stdlib/math/base/assert/is-nanf": "^0.2.2",
4040
"@stdlib/math/base/napi/ternary": "^0.2.2",
41-
"@stdlib/math/base/special/pow": "^0.2.2",
4241
"@stdlib/math/base/special/powf": "^0.2.2",
43-
"@stdlib/math/base/special/trunc": "^0.2.2",
4442
"@stdlib/math/base/special/truncf": "^0.2.2",
4543
"@stdlib/number/float64/base/to-float32": "^0.2.2",
4644
"@stdlib/utils/library-manifest": "^0.2.2"

lib/node_modules/@stdlib/math/base/special/truncbf/src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ float stdlib_base_truncbf( const float x, const int32_t n, const int32_t b ) {
4444
return 0.0f / 0.0f;
4545
}
4646
s = stdlib_base_powf( (float)b, (float)n );
47+
if ( isinf( s ) ) {
48+
return x;
49+
}
4750
return stdlib_base_truncf( x * s ) / s;
4851
}
4952

lib/node_modules/@stdlib/math/base/special/truncbf/test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var tape = require( 'tape' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' );
2727
var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' );
28+
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
2829
var PINF = require( '@stdlib/constants/float32/pinf' );
2930
var NINF = require( '@stdlib/constants/float32/ninf' );
3031
var EPS = require( '@stdlib/constants/float32/eps' );
@@ -146,3 +147,16 @@ tape( 'the function returns -infinity if provided -infinity', function test( t )
146147
t.strictEqual( v, NINF, 'returns expected value' );
147148
t.end();
148149
});
150+
151+
tape( 'the function handles scale factor overflow gracefully', function test( t ) {
152+
var v;
153+
154+
// When b^n overflows to Infinity, should return x unchanged
155+
v = truncbf( 3.14, 40, 10 );
156+
t.strictEqual( v, float64ToFloat32( 3.14 ), 'handles large exponent' );
157+
158+
v = truncbf( -2.5, 50, 10 );
159+
t.strictEqual( v, float64ToFloat32( -2.5 ), 'handles large exponent with negative x' );
160+
161+
t.end();
162+
});

0 commit comments

Comments
 (0)