Skip to content

Commit d109efc

Browse files
authored
refactor!: modify C implementation to accept float instead of int32 in math/base/special/lucasf
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: #7939 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent bb8477e commit d109efc

File tree

8 files changed

+30
-29
lines changed

8 files changed

+30
-29
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ logEachMap( 'lucasf(%d) = %0.1f', x, lucasf );
164164
Computes the nth [Lucas number][lucas-number] as a single-precision floating-point number.
165165

166166
```c
167-
float out = stdlib_base_lucasf( 0 );
167+
float out = stdlib_base_lucasf( 0.0f );
168168
// returns 2.0f
169169

170-
out = stdlib_base_lucasf( 1 );
170+
out = stdlib_base_lucasf( 1.0f );
171171
// returns 1.0f
172172
```
173173

174174
The function accepts the following arguments:
175175

176-
- **n**: `[in] int32_t` input value.
176+
- **n**: `[in] float` input value.
177177

178178
```c
179-
float stdlib_base_lucasf( const int32_t n );
179+
float stdlib_base_lucasf( const float n );
180180
```
181181
182182
</section>
@@ -200,15 +200,14 @@ float stdlib_base_lucasf( const int32_t n );
200200
```c
201201
#include "stdlib/math/base/special/lucasf.h"
202202
#include <stdio.h>
203-
#include <stdint.h>
204203
205204
int main( void ) {
206-
int32_t i;
205+
float i;
207206
float v;
208207
209-
for ( i = 0; i < 35; i++ ) {
208+
for ( i = 0.0f; i < 35.0f; i++ ) {
210209
v = stdlib_base_lucasf( i );
211-
printf( "lucasf(%d) = %f\n", i, v );
210+
printf( "lucasf(%f) = %f\n", i, v );
212211
}
213212
}
214213
```

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

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

100100
for ( i = 0; i < 100; i++ ) {
101-
x[ i ] = (int32_t)( 34.0f*rand_float() );
101+
x[ i ] = ( 34.0f*rand_float() );
102102
}
103103

104104
t = tic();

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

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

3634
#ifdef __cplusplus
3735
}

lib/node_modules/@stdlib/math/base/special/lucasf/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
24-
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
24+
var isNonNegativeIntegerf = require( '@stdlib/math/base/assert/is-nonnegative-integerf' );
2525
var MAX_LUCAS = require( '@stdlib/constants/float32/max-safe-nth-lucas' );
2626
var LUCAS = require( './lucas.json' );
2727

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

lib/node_modules/@stdlib/math/base/special/lucasf/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/float32/max-safe-nth-lucas"
40+
"@stdlib/constants/float32/max-safe-nth-lucas",
41+
"@stdlib/math/base/assert/is-nonnegative-integerf"
4142
]
4243
},
4344
{
@@ -51,7 +52,8 @@
5152
"libraries": [],
5253
"libpath": [],
5354
"dependencies": [
54-
"@stdlib/constants/float32/max-safe-nth-lucas"
55+
"@stdlib/constants/float32/max-safe-nth-lucas",
56+
"@stdlib/math/base/assert/is-nonnegative-integerf"
5557
]
5658
},
5759
{
@@ -65,7 +67,8 @@
6567
"libraries": [],
6668
"libpath": [],
6769
"dependencies": [
68-
"@stdlib/constants/float32/max-safe-nth-lucas"
70+
"@stdlib/constants/float32/max-safe-nth-lucas",
71+
"@stdlib/math/base/assert/is-nonnegative-integerf"
6972
]
7073
}
7174
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_F( stdlib_base_lucasf )
22+
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_lucasf )

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

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

1919
#include "stdlib/math/base/special/lucasf.h"
20+
#include "stdlib/math/base/assert/is_nonnegative_integerf.h"
2021
#include "stdlib/constants/float32/max_safe_nth_lucas.h"
22+
#include <stdint.h>
23+
#include <stdlib.h>
2124

2225
static const int32_t lucas_value[ 35 ] = {
2326
2,
@@ -64,16 +67,16 @@ static const int32_t lucas_value[ 35 ] = {
6467
* @return output value
6568
*
6669
* @example
67-
* float out = stdlib_base_lucasf( 1 );
70+
* float out = stdlib_base_lucasf( 1.0f );
6871
* // returns 1.0f
6972
*
7073
* @example
71-
* float out = stdlib_base_lucasf( -1 );
74+
* float out = stdlib_base_lucasf( -1.0f );
7275
* // returns NaN
7376
*/
74-
float stdlib_base_lucasf( const int32_t n ) {
75-
if ( n < 0 || n > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_LUCAS ) {
77+
float stdlib_base_lucasf( const float n ) {
78+
if ( !stdlib_base_is_nonnegative_integerf( n ) || n > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_LUCAS ) {
7679
return 0.0f / 0.0f; // NaN
7780
}
78-
return lucas_value[ n ];
81+
return lucas_value[ (size_t)n ];
7982
}

0 commit comments

Comments
 (0)