Skip to content

Commit df33cdf

Browse files
authored
refactor!: modify C implementation to accept double instead of int32 in math/base/special/fibonacci
This commit updates the signature of the C API to accept a double 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 doubles 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: #7923 Reviewed-by: Athan Reines <[email protected]>
1 parent e6bfc53 commit df33cdf

File tree

8 files changed

+28
-27
lines changed

8 files changed

+28
-27
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ out = stdlib_base_fibonacci( 1 );
185185

186186
The function accepts the following arguments:
187187

188-
- **n**: `[in] int32_t` input value.
188+
- **n**: `[in] double` input value.
189189

190190
```c
191-
double stdlib_base_fibonacci( const int32_t n );
191+
double stdlib_base_fibonacci( const double n );
192192
```
193193
194194
</section>
@@ -212,15 +212,14 @@ double stdlib_base_fibonacci( const int32_t n );
212212
```c
213213
#include "stdlib/math/base/special/fibonacci.h"
214214
#include <stdio.h>
215-
#include <stdint.h>
216215
217216
int main( void ) {
218-
int32_t i;
217+
double i;
219218
double v;
220219
221-
for ( i = 0; i < 79; i++ ) {
220+
for ( i = 0.0; i < 79.0; i++ ) {
222221
v = stdlib_base_fibonacci( i );
223-
printf( "fibonacci(%d) = %lf\n", i, v );
222+
printf( "fibonacci(%lf) = %lf\n", i, v );
224223
}
225224
}
226225
```

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

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

9999
for ( i = 0; i < 100; i++ ) {
100-
x[ i ] = (int32_t)floor( 40.0*rand_double() );
100+
x[ i ] = floor( 40.0*rand_double() );
101101
}
102102

103103
t = tic();

lib/node_modules/@stdlib/math/base/special/fibonacci/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/fibonacci.h"
2020
#include <stdio.h>
21-
#include <stdint.h>
2221

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

27-
for ( i = 0; i < 79; i++ ) {
26+
for ( i = 0.0; i < 79.0; i++ ) {
2827
v = stdlib_base_fibonacci( i );
29-
printf( "fibonacci(%d) = %lf\n", i, v );
28+
printf( "fibonacci(%lf) = %lf\n", i, v );
3029
}
3130
}

lib/node_modules/@stdlib/math/base/special/fibonacci/include/stdlib/math/base/special/fibonacci.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_FIBONACCI_H
2020
#define STDLIB_MATH_BASE_SPECIAL_FIBONACCI_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.
3331
*/
34-
double stdlib_base_fibonacci( const int32_t n );
32+
double stdlib_base_fibonacci( const double n );
3533

3634
#ifdef __cplusplus
3735
}

lib/node_modules/@stdlib/math/base/special/fibonacci/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
24-
var isInteger = require( '@stdlib/math/base/assert/is-integer' );
24+
var isNonnegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' );
2525
var MAX_FIBONACCI = require( '@stdlib/constants/float64/max-safe-nth-fibonacci' );
2626
var FIBONACCI = require( './fibonacci.json' );
2727

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

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40-
"@stdlib/constants/float64/max-safe-nth-fibonacci"
40+
"@stdlib/constants/float64/max-safe-nth-fibonacci",
41+
"@stdlib/math/base/assert/is-nonnegative-integer"
4142
]
4243
},
4344
{
@@ -51,7 +52,8 @@
5152
"libraries": [],
5253
"libpath": [],
5354
"dependencies": [
54-
"@stdlib/constants/float64/max-safe-nth-fibonacci"
55+
"@stdlib/constants/float64/max-safe-nth-fibonacci",
56+
"@stdlib/math/base/assert/is-nonnegative-integer"
5557
]
5658
},
5759
{
@@ -65,7 +67,8 @@
6567
"libraries": [],
6668
"libpath": [],
6769
"dependencies": [
68-
"@stdlib/constants/float64/max-safe-nth-fibonacci"
70+
"@stdlib/constants/float64/max-safe-nth-fibonacci",
71+
"@stdlib/math/base/assert/is-nonnegative-integer"
6972
]
7073
}
7174
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_D( stdlib_base_fibonacci )
22+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_fibonacci )

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
#include "stdlib/math/base/special/fibonacci.h"
2020
#include "stdlib/constants/float64/max_safe_nth_fibonacci.h"
21+
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
22+
#include <stdint.h>
23+
#include <stdlib.h>
2124

2225
static const int64_t fibonacci_value[ 79 ] = {
2326
0,
@@ -108,12 +111,12 @@ static const int64_t fibonacci_value[ 79 ] = {
108111
* @return output value
109112
*
110113
* @example
111-
* double out = stdlib_base_fibonacci( 1 );
112-
* // returns 1
114+
* double out = stdlib_base_fibonacci( 1.0 );
115+
* // returns 1.0
113116
*/
114-
double stdlib_base_fibonacci( const int32_t n ) {
115-
if ( n < 0 || n > STDLIB_CONSTANT_FLOAT64_MAX_SAFE_NTH_FIBONACCI ) {
117+
double stdlib_base_fibonacci( const double n ) {
118+
if ( !stdlib_base_is_nonnegative_integer( n ) || n > STDLIB_CONSTANT_FLOAT64_MAX_SAFE_NTH_FIBONACCI ) {
116119
return 0.0 / 0.0; // NaN
117120
}
118-
return fibonacci_value[ n ];
121+
return fibonacci_value[ (size_t)n ];
119122
}

0 commit comments

Comments
 (0)