Skip to content

Commit 348acc3

Browse files
authored
refactor!: modify C implementation to accept float value instead of int32 in math/base/special/negafibonaccif
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: #7954 Reviewed-by: Athan Reines <[email protected]>
1 parent be54164 commit 348acc3

File tree

7 files changed

+27
-28
lines changed

7 files changed

+27
-28
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,19 @@ logEachMap( 'negafibonaccif(%d) = %0.4f', v, negafibonaccif );
174174
Computes the nth [negaFibonacci number][fibonacci-number] as a [single-precision floating-point number][ieee754].
175175

176176
```c
177-
float out = stdlib_base_negafibonaccif( 0 );
177+
float out = stdlib_base_negafibonaccif( 0.0f );
178178
// returns 0.0f
179179

180-
out = stdlib_base_negafibonaccif( -1 );
180+
out = stdlib_base_negafibonaccif( -1.0f );
181181
// returns 1.0f
182182
```
183183

184184
The function accepts the following arguments:
185185

186-
- **n**: `[in] int32_t` input value.
186+
- **n**: `[in] float` input value.
187187

188188
```c
189-
float stdlib_base_negafibonaccif( const int32_t n );
189+
float stdlib_base_negafibonaccif( const float n );
190190
```
191191
192192
</section>
@@ -210,15 +210,14 @@ float stdlib_base_negafibonaccif( const int32_t n );
210210
```c
211211
#include "stdlib/math/base/special/negafibonaccif.h"
212212
#include <stdio.h>
213-
#include <stdint.h>
214213
215214
int main( void ) {
216-
int32_t i;
215+
float i;
217216
float v;
218217
219-
for ( i = 0; i > -37; i-- ) {
218+
for ( i = 0.0f; i > -37.0f; i-- ) {
220219
v = stdlib_base_negafibonaccif( i );
221-
printf( "negafibonaccif(%d) = %f\n", i, v );
220+
printf( "negafibonaccif(%f) = %f\n", i, v );
222221
}
223222
}
224223
```

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/negafibonaccif.h"
20-
#include <stdint.h>
2120
#include <stdlib.h>
2221
#include <stdio.h>
2322
#include <math.h>
@@ -91,14 +90,14 @@ static float rand_float( void ) {
9190
* @return elapsed time in seconds
9291
*/
9392
static double benchmark( void ) {
94-
int32_t x[ 100 ];
93+
float x[ 100 ];
9594
double elapsed;
9695
double t;
9796
float y;
9897
int i;
9998

10099
for ( i = 0; i < 100; i++ ) {
101-
x[ i ] = (int32_t)floorf( 36.0f*rand_float());
100+
x[ i ] = floorf( 36.0f*rand_float());
102101
}
103102

104103
t = tic();

lib/node_modules/@stdlib/math/base/special/negafibonaccif/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/negafibonaccif.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_negafibonaccif( i );
29-
printf( "negafibonaccif(%d) = %f\n", i, v );
28+
printf( "negafibonaccif(%f) = %f\n", i, v );
3029
}
3130
}

lib/node_modules/@stdlib/math/base/special/negafibonaccif/include/stdlib/math/base/special/negafibonaccif.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_NEGAFIBONACCIF_H
2020
#define STDLIB_MATH_BASE_SPECIAL_NEGAFIBONACCIF_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 negaFibonacci number as a single-precision floating-point number.
3331
*/
34-
float stdlib_base_negafibonaccif( const int32_t n );
32+
float stdlib_base_negafibonaccif( const float n );
3533

3634
#ifdef __cplusplus
3735
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
4040
"@stdlib/constants/float32/max-safe-nth-fibonacci",
41-
"@stdlib/math/base/special/absf"
41+
"@stdlib/math/base/special/absf",
42+
"@stdlib/math/base/assert/is-integerf"
4243
]
4344
},
4445
{
@@ -53,7 +54,8 @@
5354
"libpath": [],
5455
"dependencies": [
5556
"@stdlib/constants/float32/max-safe-nth-fibonacci",
56-
"@stdlib/math/base/special/absf"
57+
"@stdlib/math/base/special/absf",
58+
"@stdlib/math/base/assert/is-integerf"
5759
]
5860
},
5961
{
@@ -68,7 +70,8 @@
6870
"libpath": [],
6971
"dependencies": [
7072
"@stdlib/constants/float32/max-safe-nth-fibonacci",
71-
"@stdlib/math/base/special/absf"
73+
"@stdlib/math/base/special/absf",
74+
"@stdlib/math/base/assert/is-integerf"
7275
]
7376
}
7477
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_F( stdlib_base_negafibonaccif )
22+
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_negafibonaccif )

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

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

1919
#include "stdlib/math/base/special/negafibonaccif.h"
20+
#include "stdlib/math/base/assert/is_integerf.h"
2021
#include "stdlib/math/base/special/absf.h"
2122
#include "stdlib/constants/float32/max_safe_nth_fibonacci.h"
22-
#include <stdint.h>
23+
#include <stdlib.h>
2324

2425
static const float negafibonaccif_value[ 37 ] = {
2526
0.0f,
@@ -68,17 +69,17 @@ static const float negafibonaccif_value[ 37 ] = {
6869
* @return output value
6970
*
7071
* @example
71-
* float out = stdlib_base_negafibonaccif( -1 );
72+
* float out = stdlib_base_negafibonaccif( -1.0f );
7273
* // returns 1.0f
7374
*/
74-
float stdlib_base_negafibonaccif( const int32_t n ) {
75-
int32_t an;
76-
if ( n > 0 ) {
75+
float stdlib_base_negafibonaccif( const float n ) {
76+
float an;
77+
if ( !stdlib_base_is_integerf( n ) || n > 0.0f ) {
7778
return 0.0f / 0.0f; // NaN
7879
}
7980
an = stdlib_base_absf( n );
8081
if ( an > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_FIBONACCI ) {
8182
return 0.0f / 0.0f; // NaN
8283
}
83-
return negafibonaccif_value[ an ];
84+
return negafibonaccif_value[ (size_t)an ];
8485
}

0 commit comments

Comments
 (0)