Skip to content

Commit 3d340e2

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

File tree

8 files changed

+31
-30
lines changed

8 files changed

+31
-30
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,19 @@ for ( i = 0; i < 77; i++ ) {
174174
Computes the nth [Lucas number][lucas-number].
175175

176176
```c
177-
double out = stdlib_base_lucas( 0 );
178-
// returns 2
177+
double out = stdlib_base_lucas( 0.0 );
178+
// returns 2.0
179179

180-
out = stdlib_base_lucas( 1 );
181-
// returns 1
180+
out = stdlib_base_lucas( 1.0 );
181+
// returns 1.0
182182
```
183183

184184
The function accepts the following arguments:
185185

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

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* Prints the TAP version.
3232
*/
33-
void print_version() {
33+
static void print_version( void ) {
3434
printf( "TAP version 13\n" );
3535
}
3636

@@ -91,14 +91,14 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
int32_t x;
94+
double x;
9595
double t;
9696
double y;
9797
int i;
9898

9999
t = tic();
100100
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = (int32_t)( 76.0*rand_double() );
101+
x = ( 76.0*rand_double() );
102102
y = stdlib_base_lucas( x );
103103
if ( y != y ) {
104104
printf( "should not return NaN\n" );

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

lib/node_modules/@stdlib/math/base/special/lucas/include/stdlib/math/base/special/lucas.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_LUCAS_H
2020
#define STDLIB_MATH_BASE_SPECIAL_LUCAS_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 Lucas number.
3331
*/
34-
double stdlib_base_lucas( const int32_t n );
32+
double stdlib_base_lucas( const double n );
3533

3634
#ifdef __cplusplus
3735
}

lib/node_modules/@stdlib/math/base/special/lucas/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_LUCAS = require( '@stdlib/constants/float64/max-safe-nth-lucas' );
2626
var LUCAS = require( './lucas.json' );
2727

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

lib/node_modules/@stdlib/math/base/special/lucas/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-integer",
4041
"@stdlib/constants/float64/max-safe-nth-lucas"
4142
]
4243
},
@@ -51,6 +52,7 @@
5152
"libraries": [],
5253
"libpath": [],
5354
"dependencies": [
55+
"@stdlib/math/base/assert/is-nonnegative-integer",
5456
"@stdlib/constants/float64/max-safe-nth-lucas"
5557
]
5658
},
@@ -65,6 +67,7 @@
6567
"libraries": [],
6668
"libpath": [],
6769
"dependencies": [
70+
"@stdlib/math/base/assert/is-nonnegative-integer",
6871
"@stdlib/constants/float64/max-safe-nth-lucas"
6972
]
7073
}

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_D( stdlib_base_lucas )
22+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_lucas )

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

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

1919
#include "stdlib/math/base/special/lucas.h"
20+
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
2021
#include "stdlib/constants/float64/max_safe_nth_lucas.h"
22+
#include <stdint.h>
23+
#include <stdlib.h>
2124

2225
static const int64_t lucas_value[ 77 ] = {
2326
2,
@@ -106,16 +109,16 @@ static const int64_t lucas_value[ 77 ] = {
106109
* @return output value
107110
*
108111
* @example
109-
* double out = stdlib_base_lucas( 1 );
110-
* // returns 1
112+
* double out = stdlib_base_lucas( 1.0 );
113+
* // returns 1.0
111114
*
112115
* @example
113-
* double out = stdlib_base_lucas( -1 );
116+
* double out = stdlib_base_lucas( -1.0 );
114117
* // returns NaN
115118
*/
116-
double stdlib_base_lucas( const int32_t n ) {
117-
if ( n < 0 || n > STDLIB_CONSTANT_FLOAT64_MAX_SAFE_NTH_LUCAS ) {
119+
double stdlib_base_lucas( const double n ) {
120+
if ( !stdlib_base_is_nonnegative_integer( n ) || n > STDLIB_CONSTANT_FLOAT64_MAX_SAFE_NTH_LUCAS ) {
118121
return 0.0 / 0.0; // NaN
119122
}
120-
return lucas_value[ n ];
123+
return lucas_value[ (size_t)n ];
121124
}

0 commit comments

Comments
 (0)