Skip to content

Commit 63cf5ea

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

File tree

7 files changed

+43
-33
lines changed

7 files changed

+43
-33
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,19 @@ for ( i = 1; i < 100; i++ ) {
154154
Computes the nth non-Fibonacci number.
155155

156156
```c
157-
double out = stdlib_base_nonfibonacci( 1 );
158-
// returns 4
157+
double out = stdlib_base_nonfibonacci( 1.0 );
158+
// returns 4.0
159159

160-
out = stdlib_base_nonfibonacci( 2 );
161-
// returns 6
160+
out = stdlib_base_nonfibonacci( 2.0 );
161+
// returns 6.0
162162
```
163163

164164
The function accepts the following arguments:
165165

166-
- **x**: `[in] int32_t` input value.
166+
- **x**: `[in] double` input value.
167167

168168
```c
169-
double stdlib_base_nonfibonacci( const int32_t x );
169+
double stdlib_base_nonfibonacci( const double x );
170170
```
171171
172172
</section>
@@ -190,12 +190,14 @@ double stdlib_base_nonfibonacci( const int32_t x );
190190
```c
191191
#include "stdlib/math/base/special/nonfibonacci.h"
192192
#include <stdio.h>
193-
#include <stdlib.h>
193+
194194
int main( void ) {
195-
int i;
196-
for ( i = 1; i < 12; i++ ) {
197-
double result = stdlib_base_nonfibonacci( i );
198-
printf( "x: %i => result: %lf", i, result );
195+
double i;
196+
double v;
197+
198+
for ( i = 1.0; i < 12.0; i++ ) {
199+
v = stdlib_base_nonfibonacci( i );
200+
printf( "nonfibonacci(%lf) = %lf\n", i, v );
199201
}
200202
}
201203
```

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

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

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

102102
t = tic();

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

Lines changed: 6 additions & 4 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;
24-
for ( i = 1; i < 12; i++ ) {
25-
double result = stdlib_base_nonfibonacci( i );
26-
printf( "x: %i => result: %lf\n", i, result );
23+
double i;
24+
double v;
25+
26+
for ( i = 1.0; i < 12.0; i++ ) {
27+
v = stdlib_base_nonfibonacci( i );
28+
printf( "nonfibonacci(%lf) = %lf\n", i, v );
2729
}
2830
}

lib/node_modules/@stdlib/math/base/special/nonfibonacci/include/stdlib/math/base/special/nonfibonacci.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_NONFIBONACCI_H
2020
#define STDLIB_MATH_BASE_SPECIAL_NONFIBONACCI_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 non-Fibonacci number.
3331
*/
34-
double stdlib_base_nonfibonacci( const int32_t n );
32+
double stdlib_base_nonfibonacci( const double n );
3533

3634
#ifdef __cplusplus
3735
}

lib/node_modules/@stdlib/math/base/special/nonfibonacci/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/float64/nan",
4141
"@stdlib/math/base/special/floor",
42-
"@stdlib/math/base/special/ln"
42+
"@stdlib/math/base/special/ln",
43+
"@stdlib/math/base/assert/is-integer",
44+
"@stdlib/constants/float64/pinf"
4345
]
4446
},
4547
{
@@ -55,7 +57,9 @@
5557
"dependencies": [
5658
"@stdlib/constants/float64/nan",
5759
"@stdlib/math/base/special/floor",
58-
"@stdlib/math/base/special/ln"
60+
"@stdlib/math/base/special/ln",
61+
"@stdlib/math/base/assert/is-integer",
62+
"@stdlib/constants/float64/pinf"
5963
]
6064
},
6165
{
@@ -71,7 +75,9 @@
7175
"dependencies": [
7276
"@stdlib/constants/float64/nan",
7377
"@stdlib/math/base/special/floor",
74-
"@stdlib/math/base/special/ln"
78+
"@stdlib/math/base/special/ln",
79+
"@stdlib/math/base/assert/is-integer",
80+
"@stdlib/constants/float64/pinf"
7581
]
7682
}
7783
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_D( stdlib_base_nonfibonacci )
22+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_nonfibonacci )

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

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

1919
#include "stdlib/math/base/special/nonfibonacci.h"
20+
#include "stdlib/math/base/assert/is_integer.h"
2021
#include "stdlib/constants/float64/nan.h"
22+
#include "stdlib/constants/float64/pinf.h"
2123
#include "stdlib/math/base/special/floor.h"
2224
#include "stdlib/math/base/special/ln.h"
23-
#include <stdint.h>
2425

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

55-
if ( n < 1 ) {
56+
mut_n = n;
57+
if ( !stdlib_base_is_integer( n ) || n == STDLIB_CONSTANT_FLOAT64_PINF || n < 1.0 ) {
5658
return STDLIB_CONSTANT_FLOAT64_NAN;
5759
}
5860

59-
mut_n += 1;
61+
mut_n += 1.0;
6062
a = stdlib_base_ln( mut_n * SQRT5 ) / LN_PHI;
6163
b = stdlib_base_ln( (SQRT5 * (mut_n + a)) - 5.0 + (3.0 / mut_n) ) / LN_PHI;
6264

0 commit comments

Comments
 (0)