Skip to content

Commit d7b5f34

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

File tree

7 files changed

+37
-28
lines changed

7 files changed

+37
-28
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,19 @@ for ( i = 1; i < 100; i++ ) {
151151
Computes the nth non-Fibonacci single-precision floating-point number.
152152

153153
```c
154-
float out = stdlib_base_nonfibonaccif( 1 );
154+
float out = stdlib_base_nonfibonaccif( 1.0f );
155155
// returns 4.0f
156156

157-
out = stdlib_base_nonfibonaccif( 2 );
157+
out = stdlib_base_nonfibonaccif( 2.0f );
158158
// returns 6.0f
159159
```
160160

161161
The function accepts the following arguments:
162162

163-
- **x**: `[in] int32_t` input value.
163+
- **x**: `[in] float` input value.
164164

165165
```c
166-
float stdlib_base_nonfibonaccif( const int32_t x );
166+
float stdlib_base_nonfibonaccif( const float x );
167167
```
168168
169169
</section>
@@ -189,10 +189,12 @@ float stdlib_base_nonfibonaccif( const int32_t x );
189189
#include <stdio.h>
190190
191191
int main( void ) {
192-
int i;
192+
float i;
193+
float v;
193194
194-
for ( i = 1; i < 12; i++ ) {
195-
printf( "x: %i => result: %f\n", i, stdlib_base_nonfibonaccif( i ) );
195+
for ( i = 1.0f; i < 12.0f; i++ ) {
196+
v = stdlib_base_nonfibonaccif( i );
197+
printf( "nonfibonaccif(%f) = %f\n", i, v );
196198
}
197199
}
198200
```

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ static float rand_float( void ) {
9090
*/
9191
static double benchmark( void ) {
9292
double elapsed;
93-
int x[ 100 ];
93+
float x[ 100 ];
9494
double t;
9595
float y;
9696
int i;
9797

9898
for ( i = 0; i < 100; i++ ) {
99-
x[ i ] = (int)floorf( ( 100.0f * rand_float() ) + 1.0f );
99+
x[ i ] = floorf( ( 100.0f * rand_float() ) + 1.0f );
100100
}
101101

102102
t = tic();

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#include <stdio.h>
2121

2222
int main( void ) {
23-
int i;
23+
float i;
24+
float v;
2425

25-
for ( i = 1; i < 12; i++ ) {
26-
printf( "x: %i => result: %f\n", i, stdlib_base_nonfibonaccif( i ) );
26+
for ( i = 1.0f; i < 12.0f; i++ ) {
27+
v = stdlib_base_nonfibonaccif( i );
28+
printf( "nonfibonaccif(%f) = %f\n", i, v );
2729
}
2830
}

lib/node_modules/@stdlib/math/base/special/nonfibonaccif/include/stdlib/math/base/special/nonfibonaccif.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_NONFIBONACCIF_H
2020
#define STDLIB_MATH_BASE_SPECIAL_NONFIBONACCIF_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
*/
@@ -32,7 +30,7 @@ extern "C"
3230
/**
3331
* Computes the nth non-Fibonacci single-precision floating-point number.
3432
*/
35-
float stdlib_base_nonfibonaccif( const int32_t n );
33+
float stdlib_base_nonfibonaccif( const float n );
3634

3735
#ifdef __cplusplus
3836
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
"@stdlib/math/base/napi/unary",
4040
"@stdlib/constants/float32/nan",
4141
"@stdlib/math/base/special/floorf",
42-
"@stdlib/math/base/special/lnf"
42+
"@stdlib/math/base/special/lnf",
43+
"@stdlib/math/base/assert/is-integerf",
44+
"@stdlib/constants/float32/pinf"
4345
]
4446
},
4547
{
@@ -55,7 +57,9 @@
5557
"dependencies": [
5658
"@stdlib/constants/float32/nan",
5759
"@stdlib/math/base/special/floorf",
58-
"@stdlib/math/base/special/lnf"
60+
"@stdlib/math/base/special/lnf",
61+
"@stdlib/math/base/assert/is-integerf",
62+
"@stdlib/constants/float32/pinf"
5963
]
6064
},
6165
{
@@ -71,7 +75,9 @@
7175
"dependencies": [
7276
"@stdlib/constants/float32/nan",
7377
"@stdlib/math/base/special/floorf",
74-
"@stdlib/math/base/special/lnf"
78+
"@stdlib/math/base/special/lnf",
79+
"@stdlib/math/base/assert/is-integerf",
80+
"@stdlib/constants/float32/pinf"
7581
]
7682
}
7783
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_F( stdlib_base_nonfibonaccif )
22+
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_nonfibonaccif )

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

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

1919
#include "stdlib/math/base/special/nonfibonaccif.h"
20+
#include "stdlib/math/base/assert/is_integerf.h"
2021
#include "stdlib/constants/float32/nan.h"
22+
#include "stdlib/constants/float32/pinf.h"
2123
#include "stdlib/math/base/special/floorf.h"
2224
#include "stdlib/math/base/special/lnf.h"
23-
#include <stdint.h>
2425

2526
static const float SQRT5 = 2.23606797749979f;
2627
static const float LN_PHI = 0.48121182506f;
@@ -32,30 +33,30 @@ static const float LN_PHI = 0.48121182506f;
3233
* @return output value
3334
*
3435
* @example
35-
* float y = stdlib_base_nonfibonaccif( 2 );
36+
* float y = stdlib_base_nonfibonaccif( 2.0f );
3637
* // returns 6.0f
3738
*
3839
* @example
39-
* float y = stdlib_base_nonfibonaccif( 1 );
40+
* float y = stdlib_base_nonfibonaccif( 1.0f );
4041
* // returns 4.0f
4142
*
4243
* @example
43-
* float y = stdlib_base_nonfibonaccif( 3 );
44+
* float y = stdlib_base_nonfibonaccif( 3.0f );
4445
* // returns 7.0f
4546
*
4647
* @example
47-
* float y = stdlib_base_nonfibonaccif( -1 );
48+
* float y = stdlib_base_nonfibonaccif( -1.0f );
4849
* // returns NaN
4950
*/
50-
float stdlib_base_nonfibonaccif( const int32_t n ) {
51-
int32_t m;
51+
float stdlib_base_nonfibonaccif( const float n ) {
52+
float m;
5253
float a;
5354
float b;
5455

55-
if ( n < 1 ) {
56+
if ( !stdlib_base_is_integerf( n ) || n == STDLIB_CONSTANT_FLOAT32_PINF || n < 1.0f ) {
5657
return STDLIB_CONSTANT_FLOAT32_NAN;
5758
}
58-
m = n + 1;
59+
m = n + 1.0f;
5960
a = stdlib_base_lnf( m * SQRT5 ) / LN_PHI;
6061
b = stdlib_base_lnf( ( SQRT5 * ( m + a ) ) - 5.0f + ( 3.0f / m ) ) / LN_PHI;
6162
return stdlib_base_floorf( m + b - 2.0f );

0 commit comments

Comments
 (0)