Skip to content

Commit 709c1ab

Browse files
authored
refactor!: modify C implementation to accept float instead of int32 in math/base/special/fibonaccif
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: #7938 Reviewed-by: Athan Reines <[email protected]>
1 parent c5b3e0a commit 709c1ab

File tree

8 files changed

+28
-28
lines changed

8 files changed

+28
-28
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ logEachMap( 'fibonaccif(%d) = %0.1f', x, fibonaccif );
164164
Computes the nth [Fibonacci number][fibonacci-number] as a single-precision floating-point number.
165165

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

170-
out = stdlib_base_fibonaccif( 1 );
170+
out = stdlib_base_fibonaccif( 1.0f );
171171
// returns 1.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_fibonaccif( const int32_t n );
179+
float stdlib_base_fibonaccif( const float n );
180180
```
181181
182182
</section>
@@ -200,15 +200,14 @@ float stdlib_base_fibonaccif( const int32_t n );
200200
```c
201201
#include "stdlib/math/base/special/fibonaccif.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 < 37; i++ ) {
208+
for ( i = 0.0f; i < 37.0f; i++ ) {
210209
v = stdlib_base_fibonaccif( i );
211-
printf( "fibonaccif(%d) = %f\n", i, v );
210+
printf( "fibonaccif(%f) = %f\n", i, v );
212211
}
213212
}
214213
```

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

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

1919
#include "stdlib/math/base/special/fibonaccif.h"
2020
#include <stdlib.h>
21-
#include <stdint.h>
2221
#include <stdio.h>
2322
#include <math.h>
2423
#include <time.h>
@@ -92,14 +91,14 @@ static float rand_float( void ) {
9291
*/
9392
static double benchmark( void ) {
9493
double elapsed;
95-
int32_t x;
9694
double t;
97-
double y;
95+
float x;
96+
float y;
9897
int i;
9998

10099
t = tic();
101100
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = (int32_t)floor( 36.0*rand_float() );
101+
x = floorf( 36.0f*rand_float() );
103102
y = stdlib_base_fibonaccif( x );
104103
if ( y < 0 ) {
105104
printf( "should return a nonnegative integer\n" );

lib/node_modules/@stdlib/math/base/special/fibonaccif/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/fibonaccif.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 < 37; i++ ) {
26+
for ( i = 0.0f; i < 37.0f; i++ ) {
2827
v = stdlib_base_fibonaccif( i );
29-
printf( "fibonaccif(%d) = %f\n", i, v );
28+
printf( "fibonaccif(%f) = %f\n", i, v );
3029
}
3130
}

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

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

22-
#include <stdint.h>
23-
2422
/*
2523
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2624
*/
@@ -31,7 +29,7 @@ extern "C" {
3129
/**
3230
* Computes the nth Fibonacci number as a single-precision floating-point number.
3331
*/
34-
float stdlib_base_fibonaccif( const int32_t n );
32+
float stdlib_base_fibonaccif( const float n );
3533

3634
#ifdef __cplusplus
3735
}

lib/node_modules/@stdlib/math/base/special/fibonaccif/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 MAX_FIBONACCI = require( '@stdlib/constants/float32/max-safe-nth-fibonacci' );
2626
var FIBONACCI = require( './fibonacci.json' );
2727

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40+
"@stdlib/math/base/assert/is-nonnegative-integerf",
4041
"@stdlib/constants/float32/max-safe-nth-fibonacci"
4142
]
4243
},
@@ -51,6 +52,7 @@
5152
"libraries": [],
5253
"libpath": [],
5354
"dependencies": [
55+
"@stdlib/math/base/assert/is-nonnegative-integerf",
5456
"@stdlib/constants/float32/max-safe-nth-fibonacci"
5557
]
5658
},
@@ -65,6 +67,7 @@
6567
"libraries": [],
6668
"libpath": [],
6769
"dependencies": [
70+
"@stdlib/math/base/assert/is-nonnegative-integerf",
6871
"@stdlib/constants/float32/max-safe-nth-fibonacci"
6972
]
7073
}

lib/node_modules/@stdlib/math/base/special/fibonaccif/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/fibonaccif.h"
2020
#include "stdlib/math/base/napi/unary.h"
2121

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_F( stdlib_base_fibonaccif )
22+
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_fibonaccif )

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/fibonaccif.h"
20+
#include "stdlib/math/base/assert/is_nonnegative_integerf.h"
2021
#include "stdlib/constants/float32/max_safe_nth_fibonacci.h"
22+
#include <stdint.h>
23+
#include <stdlib.h>
2124

2225
static const int32_t fibonacci_value[ 37 ] = {
2326
0,
@@ -66,12 +69,12 @@ static const int32_t fibonacci_value[ 37 ] = {
6669
* @return output value
6770
*
6871
* @example
69-
* float out = stdlib_base_fibonaccif( 1 );
72+
* float out = stdlib_base_fibonaccif( 1.0f );
7073
* // returns 1.0f
7174
*/
72-
float stdlib_base_fibonaccif( const int32_t n ) {
73-
if ( n < 0 || n > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_FIBONACCI ) {
74-
return 0.0 / 0.0; // NaN
75+
float stdlib_base_fibonaccif( const float n ) {
76+
if ( !stdlib_base_is_nonnegative_integerf( n ) || n > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_FIBONACCI ) {
77+
return 0.0f / 0.0f; // NaN
7578
}
76-
return fibonacci_value[ n ];
79+
return fibonacci_value[ (size_t)n ];
7780
}

0 commit comments

Comments
 (0)