Skip to content

Commit 8c5f190

Browse files
gunjjoshikgryte
andauthored
refactor!: modify C implementation to accept float value instead of int32 in math/base/special/negalucasf
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: #7956 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 801494f commit 8c5f190

File tree

7 files changed

+35
-33
lines changed

7 files changed

+35
-33
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,19 @@ for ( i = 0; i > -35; i-- ) {
191191
Computes the nth [negaLucas number][lucas-number] in single-precision floating-point format.
192192

193193
```c
194-
float out = stdlib_base_negalucasf( 0 );
194+
float out = stdlib_base_negalucasf( 0.0f );
195195
// returns 0.0f
196196

197-
out = stdlib_base_negalucasf( -1 );
197+
out = stdlib_base_negalucasf( -1.0f );
198198
// returns -1.0f
199199
```
200200

201201
The function accepts the following arguments:
202202

203-
- **n**: `[in] int32_t` input value.
203+
- **n**: `[in] float` input value.
204204

205205
```c
206-
float stdlib_base_negalucasf( const int32_t n );
206+
float stdlib_base_negalucasf( const float n );
207207
```
208208
209209
</section>
@@ -227,15 +227,14 @@ float stdlib_base_negalucasf( const int32_t n );
227227
```c
228228
#include "stdlib/math/base/special/negalucasf.h"
229229
#include <stdio.h>
230-
#include <stdint.h>
231230
232231
int main( void ) {
233-
int32_t i;
232+
float i;
234233
float v;
235234
236-
for ( i = 0; i > -35; i-- ) {
235+
for ( i = 0.0f; i > -35.0f; i-- ) {
237236
v = stdlib_base_negalucasf( i );
238-
printf( "negalucasf(%d) = %f\n", i, v );
237+
printf( "negalucasf(%f) = %f\n", i, v );
239238
}
240239
}
241240
```

lib/node_modules/@stdlib/math/base/special/negalucasf/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,13 +91,13 @@ static float rand_float( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
int32_t x[ 100 ];
94+
float x[ 100 ];
9595
double t;
9696
float y;
9797
int i;
9898

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

103103
t = tic();

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

lib/node_modules/@stdlib/math/base/special/negalucasf/include/stdlib/math/base/special/negalucasf.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_NEGALUCASF_H
2020
#define STDLIB_MATH_BASE_SPECIAL_NEGALUCASF_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 negaLucas number in single-precision floating-point format.
3331
*/
34-
float stdlib_base_negalucasf( const int32_t n );
32+
float stdlib_base_negalucasf( const float n );
3533

3634
#ifdef __cplusplus
3735
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40-
"@stdlib/math/base/special/labs",
41-
"@stdlib/constants/float32/max-safe-nth-lucas"
40+
"@stdlib/math/base/special/absf",
41+
"@stdlib/constants/float32/max-safe-nth-lucas",
42+
"@stdlib/math/base/assert/is-integerf"
4243
]
4344
},
4445
{
@@ -52,8 +53,9 @@
5253
"libraries": [],
5354
"libpath": [],
5455
"dependencies": [
55-
"@stdlib/math/base/special/labs",
56-
"@stdlib/constants/float32/max-safe-nth-lucas"
56+
"@stdlib/math/base/special/absf",
57+
"@stdlib/constants/float32/max-safe-nth-lucas",
58+
"@stdlib/math/base/assert/is-integerf"
5759
]
5860
},
5961
{
@@ -67,8 +69,9 @@
6769
"libraries": [],
6870
"libpath": [],
6971
"dependencies": [
70-
"@stdlib/math/base/special/labs",
71-
"@stdlib/constants/float32/max-safe-nth-lucas"
72+
"@stdlib/math/base/special/absf",
73+
"@stdlib/constants/float32/max-safe-nth-lucas",
74+
"@stdlib/math/base/assert/is-integerf"
7275
]
7376
}
7477
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_F( stdlib_base_negalucasf )
22+
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_negalucasf )

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

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

1919
#include "stdlib/math/base/special/negalucasf.h"
20-
#include "stdlib/math/base/special/labs.h"
20+
#include "stdlib/math/base/assert/is_integerf.h"
21+
#include "stdlib/math/base/special/absf.h"
2122
#include "stdlib/constants/float32/max_safe_nth_lucas.h"
23+
#include <stdint.h>
24+
#include <stdlib.h>
2225

2326
static const int32_t negalucasf_value[ 35 ] = {
2427
2,
@@ -65,17 +68,17 @@ static const int32_t negalucasf_value[ 35 ] = {
6568
* @return output value
6669
*
6770
* @example
68-
* float out = stdlib_base_negalucasf( -1 );
69-
* // returns -1
71+
* float out = stdlib_base_negalucasf( -1.0f );
72+
* // returns -1.0f
7073
*
7174
* @example
72-
* float out = stdlib_base_negalucasf( 1 );
75+
* float out = stdlib_base_negalucasf( 1.0f );
7376
* // returns NaN
7477
*/
75-
float stdlib_base_negalucasf( const int32_t n ) {
76-
int32_t an = stdlib_base_labs( n );
77-
if ( n > 0 || an > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_LUCAS ) {
78+
float stdlib_base_negalucasf( const float n ) {
79+
float an = stdlib_base_absf( n );
80+
if ( !stdlib_base_is_integerf( n ) || n > 0.0f || an > STDLIB_CONSTANT_FLOAT32_MAX_SAFE_NTH_LUCAS ) {
7881
return 0.0f / 0.0f; // NaN
7982
}
80-
return negalucasf_value[ an ];
83+
return negalucasf_value[ (size_t)an ];
8184
}

0 commit comments

Comments
 (0)