Skip to content

Commit dc3487c

Browse files
gunjjoshikgryte
andauthored
refactor!: modify C implementation to accept float instead of int32 in math/base/special/tribonaccif
This commit updates the signature of the C API to accept a float rather than int32. The rationale was so that users get the same results/behavior in both JavaScript and C. This arose in the context of ufuncs, where the diverging type signatures meant differences in what dtypes would be permissible. Instead, we decided to unify and ensure the behavior is consistent. BREAKING CHANGE: update signature to accept floats User code should behave similarly in the primary case of providing integer-valued input values. However, no longer will real-values truncate. Now, real-valued inputs will result in `NaN`, which is, arguably, better behavior, as real-to-integer truncation can be a source of silent bugs. PR-URL: #8032 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 68d2365 commit dc3487c

File tree

9 files changed

+51
-28
lines changed

9 files changed

+51
-28
lines changed

lib/node_modules/@stdlib/math/base/special/tribonaccif/README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ logEachMap( 'tribonaccif(%d) = %0.4f', v, tribonaccif );
164164
Computes the nth [Tribonacci number][tribonacci-number] as a [single-precision floating-point number][ieee754].
165165

166166
```c
167-
float out = stdlib_base_tribonaccif( 0 );
167+
float out = stdlib_base_tribonaccif( 0.0f );
168168
// returns 0.0f
169169

170-
out = stdlib_base_tribonaccif( 4 );
170+
out = stdlib_base_tribonaccif( 4.0f );
171171
// returns 2.0f
172172
```
173173

174174
The function accepts the following arguments:
175175

176-
- **n**: `[in] int32_t` input value.
176+
- **n**: `[in] float` input value.
177177

178178
```c
179-
float stdlib_base_tribonaccif( const int32_t n );
179+
float stdlib_base_tribonaccif( const float n );
180180
```
181181
182182
</section>
@@ -200,15 +200,14 @@ float stdlib_base_tribonaccif( const int32_t n );
200200
```c
201201
#include "stdlib/math/base/special/tribonaccif.h"
202202
#include <stdio.h>
203-
#include <stdint.h>
204203
205204
int main( void ) {
206-
int32_t i;
205+
float i;
207206
float v;
208207
209-
for ( i = 0; i < 31; i++ ) {
208+
for ( i = 0.0f; i < 31.0f; i++ ) {
210209
v = stdlib_base_tribonaccif( i );
211-
printf( "tribonaccif(%d) = %f\n", i, v );
210+
printf( "tribonaccif(%f) = %f\n", i, v );
212211
}
213212
}
214213
```

lib/node_modules/@stdlib/math/base/special/tribonaccif/benchmark/c/native/benchmark.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ static float rand_float( void ) {
9191
* @return elapsed time in seconds
9292
*/
9393
static double benchmark( void ) {
94-
int32_t x[ 100 ];
94+
float x[ 100 ];
9595
double elapsed;
9696
double t;
9797
float y;
9898
int i;
9999

100100
for ( i = 0; i < 100; i++ ) {
101-
x[ i ] = (int32_t)floorf( 40.0f*rand_float() );
101+
x[ i ] = floorf( 40.0f*rand_float() );
102102
}
103103

104104
t = tic();

lib/node_modules/@stdlib/math/base/special/tribonaccif/examples/c/example.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818

1919
#include "stdlib/math/base/special/tribonaccif.h"
2020
#include <stdio.h>
21-
#include <stdint.h>
2221

2322
int main( void ) {
24-
int32_t i;
23+
float i;
2524
float v;
2625

27-
for ( i = 0; i < 31; i++ ) {
26+
for ( i = 0.0f; i < 31.0f; i++ ) {
2827
v = stdlib_base_tribonaccif( i );
29-
printf( "tribonaccif(%d) = %f\n", i, v );
28+
printf( "tribonaccif(%f) = %f\n", i, v );
3029
}
3130
}

lib/node_modules/@stdlib/math/base/special/tribonaccif/include/stdlib/math/base/special/tribonaccif.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919
#ifndef STDLIB_MATH_BASE_SPECIAL_TRIBONACCIF_H
2020
#define STDLIB_MATH_BASE_SPECIAL_TRIBONACCIF_H
2121

22-
#include <stdint.h>
23-
2422
#ifdef __cplusplus
2523
extern "C" {
2624
#endif
2725

2826
/**
2927
* Computes the nth Tribonacci number as a single-precision floating-point number.
3028
*/
31-
float stdlib_base_tribonaccif( const int32_t n );
29+
float stdlib_base_tribonaccif( const float n );
3230

3331
#ifdef __cplusplus
3432
};

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
24-
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
24+
var isNonnegativeIntegerf = require( '@stdlib/math/base/assert/is-nonnegative-integerf' );
2525
var FLOAT32_MAX_SAFE_NTH_TRIBONACCI = require( '@stdlib/constants/float32/max-safe-nth-tribonacci' ); // eslint-disable-line id-length
2626
var TRIBONACCIF = require( './tribonaccif.json' );
2727

@@ -77,8 +77,7 @@ var TRIBONACCIF = require( './tribonaccif.json' );
7777
function tribonaccif( n ) {
7878
if (
7979
isnanf( n ) ||
80-
isIntegerf( n ) === false ||
81-
n < 0 ||
80+
!isNonnegativeIntegerf( n ) ||
8281
n > FLOAT32_MAX_SAFE_NTH_TRIBONACCI
8382
) {
8483
return NaN;

lib/node_modules/@stdlib/math/base/special/tribonaccif/manifest.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40-
"@stdlib/constants/float32/max-safe-nth-tribonacci"
40+
"@stdlib/constants/float32/max-safe-nth-tribonacci",
41+
"@stdlib/math/base/assert/is-nonnegative-integerf",
42+
"@stdlib/math/base/assert/is-nanf"
4143
]
4244
},
4345
{
@@ -51,7 +53,9 @@
5153
"libraries": [],
5254
"libpath": [],
5355
"dependencies": [
54-
"@stdlib/constants/float32/max-safe-nth-tribonacci"
56+
"@stdlib/constants/float32/max-safe-nth-tribonacci",
57+
"@stdlib/math/base/assert/is-nonnegative-integerf",
58+
"@stdlib/math/base/assert/is-nanf"
5559
]
5660
},
5761
{
@@ -65,7 +69,9 @@
6569
"libraries": [],
6670
"libpath": [],
6771
"dependencies": [
68-
"@stdlib/constants/float32/max-safe-nth-tribonacci"
72+
"@stdlib/constants/float32/max-safe-nth-tribonacci",
73+
"@stdlib/math/base/assert/is-nonnegative-integerf",
74+
"@stdlib/math/base/assert/is-nanf"
6975
]
7076
}
7177
]

lib/node_modules/@stdlib/math/base/special/tribonaccif/src/addon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
#include "stdlib/math/base/special/tribonaccif.h"
2020
#include "stdlib/math/base/napi/unary.h"
2121

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_F( stdlib_base_tribonaccif )
22+
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_tribonaccif )

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
#include "stdlib/math/base/special/tribonaccif.h"
2020
#include "stdlib/constants/float32/max_safe_nth_tribonacci.h"
21+
#include "stdlib/math/base/assert/is_nonnegative_integerf.h"
22+
#include "stdlib/math/base/assert/is_nanf.h"
2123
#include <stdint.h>
24+
#include <stdlib.h>
2225

2326
static const int32_t tribonaccif_value[ 31 ] = {
2427
0,
@@ -61,12 +64,12 @@ static const int32_t tribonaccif_value[ 31 ] = {
6164
* @return output value
6265
*
6366
* @example
64-
* float out = stdlib_base_tribonaccif( 1 );
67+
* float out = stdlib_base_tribonaccif( 1.0f );
6568
* // returns 0.0f
6669
*/
67-
float stdlib_base_tribonaccif( const int32_t n ) {
68-
if ( n < 0 || n > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_TRIBONACCI ) {
70+
float stdlib_base_tribonaccif( const float n ) {
71+
if ( stdlib_base_is_nanf( n ) || !stdlib_base_is_nonnegative_integerf( n ) || n > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_TRIBONACCI ) {
6972
return 0.0f / 0.0f; // NaN
7073
}
71-
return tribonaccif_value[ n ];
74+
return tribonaccif_value[ (size_t)n ];
7275
}

lib/node_modules/@stdlib/math/base/special/tribonaccif/test/test.native.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var resolve = require( 'path' ).resolve;
2424
var tape = require( 'tape' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var PINF = require( '@stdlib/constants/float32/pinf' );
2627
var tryRequire = require( '@stdlib/utils/try-require' );
2728

2829

@@ -58,6 +59,24 @@ tape( 'if provided a negative number, the function returns `NaN`', opts, functio
5859
t.end();
5960
});
6061

62+
tape( 'if provided positive infinity, the function returns `NaN`', opts, function test( t ) {
63+
var v = tribonaccif( PINF );
64+
t.strictEqual( isnanf( v ), true, 'returns expected value' );
65+
t.end();
66+
});
67+
68+
tape( 'if provided `NaN`, the function returns `NaN`', opts, function test( t ) {
69+
var v = tribonaccif( NaN );
70+
t.strictEqual( isnanf( v ), true, 'returns expected value' );
71+
t.end();
72+
});
73+
74+
tape( 'if provided a non-integer, the function returns `NaN`', opts, function test( t ) {
75+
var v = tribonaccif( 3.14 );
76+
t.strictEqual( isnanf( v ), true, 'returns expected value' );
77+
t.end();
78+
});
79+
6180
tape( 'the function returns the nth Tribonacci number', opts, function test( t ) {
6281
var v;
6382
var i;

0 commit comments

Comments
 (0)