Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f18e41

Browse files
gunjjoshikgryte
andauthored
refactor!: modify C implementation to accept float instead of int32 in math/base/special/factorial2f
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: #8030 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 862befd commit 9f18e41

File tree

8 files changed

+37
-41
lines changed

8 files changed

+37
-41
lines changed

‎lib/node_modules/@stdlib/math/base/special/factorial2f/README.md‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ logEachMap( 'factorial2f(%d) = %0.1f', x, factorial2f );
136136
Evaluates the [double factorial][double-factorial] of `n` as a single-precision floating-point number.
137137

138138
```c
139-
float out = stdlib_base_factorial2f( 3 );
139+
float out = stdlib_base_factorial2f( 3.0f );
140140
// returns 3.0f
141141
```
142142

143143
The function accepts the following arguments:
144144

145-
- **n**: `[in] int32_t` input value.
145+
- **n**: `[in] float` input value.
146146

147147
```c
148-
float stdlib_base_factorial2f( const int32_t n );
148+
float stdlib_base_factorial2f( const float n );
149149
```
150150
151151
</section>
@@ -169,16 +169,15 @@ float stdlib_base_factorial2f( const int32_t n );
169169
```c
170170
#include "stdlib/math/base/special/factorial2f.h"
171171
#include <stdio.h>
172-
#include <stdint.h>
173172
174173
int main( void ) {
175-
const int32_t x[] = { 1, 10, 50, 56, 57 };
174+
const float x[] = { 1.0f, 10.0f, 50.0f, 56.0f, 57.0f };
176175
177176
float b;
178177
int i;
179178
for ( i = 0; i < 5; i++ ) {
180179
b = stdlib_base_factorial2f( x[ i ] );
181-
printf ( "factorial2f(%d) = %f\n", x[ i ], b );
180+
printf ( "factorial2f(%f) = %f\n", x[ i ], b );
182181
}
183182
}
184183
```

‎lib/node_modules/@stdlib/math/base/special/factorial2f/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)( 56*rand_float() );
101+
x[ i ] = roundf( 56.0f*rand_float() );
102102
}
103103

104104
t = tic();

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818

1919
#include "stdlib/math/base/special/factorial2f.h"
2020
#include <stdio.h>
21-
#include <stdint.h>
2221

2322
int main( void ) {
24-
const int32_t x[] = { 1, 10, 50, 56, 57 };
23+
const float x[] = { 1.0f, 10.0f, 50.0f, 56.0f, 57.0f };
2524

2625
float b;
2726
int i;
2827
for ( i = 0; i < 5; i++ ) {
2928
b = stdlib_base_factorial2f( x[ i ] );
30-
printf ( "factorial2f(%d) = %f\n", x[ i ], b );
29+
printf ( "factorial2f(%f) = %f\n", x[ i ], b );
3130
}
3231
}

‎lib/node_modules/@stdlib/math/base/special/factorial2f/include/stdlib/math/base/special/factorial2f.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_FACTORIAL2F_H
2020
#define STDLIB_MATH_BASE_SPECIAL_FACTORIAL2F_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
* Evaluates the double factorial of `n` as a single-precision floating-point number.
3331
*/
34-
float stdlib_base_factorial2f( const int32_t n );
32+
float stdlib_base_factorial2f( const float n );
3533

3634
#ifdef __cplusplus
3735
}

‎lib/node_modules/@stdlib/math/base/special/factorial2f/lib/main.js‎

Lines changed: 2 additions & 5 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 isEvenf = require( '@stdlib/math/base/assert/is-evenf' );
2626
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
2727
var PINF = require( '@stdlib/constants/float32/pinf' );
@@ -62,15 +62,12 @@ function factorial2f( n ) {
6262
var v;
6363
var i;
6464

65-
if ( isnanf( n ) ) {
65+
if ( isnanf( n ) ||!isNonnegativeIntegerf(n)) {
6666
return NaN;
6767
}
6868
if ( n > FLOAT32_MAX_NTH_DOUBLE_FACTORIAL ) {
6969
return PINF;
7070
}
71-
if ( n < 0 || isIntegerf( n ) === false ) {
72-
return NaN;
73-
}
7471
v = n|0; // asm type annotation
7572
if ( v === 0|0 || v === 1|0 ) {
7673
return 1|0; // asm type annotation

‎lib/node_modules/@stdlib/math/base/special/factorial2f/manifest.json‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40-
"@stdlib/math/base/assert/int32-is-even",
40+
"@stdlib/math/base/assert/is-evenf",
4141
"@stdlib/constants/float32/pinf",
42-
"@stdlib/constants/float32/max-nth-double-factorial"
42+
"@stdlib/constants/float32/max-nth-double-factorial",
43+
"@stdlib/math/base/assert/is-nonnegative-integerf"
4344
]
4445
},
4546
{
@@ -53,9 +54,10 @@
5354
"libraries": [],
5455
"libpath": [],
5556
"dependencies": [
56-
"@stdlib/math/base/assert/int32-is-even",
57+
"@stdlib/math/base/assert/is-evenf",
5758
"@stdlib/constants/float32/pinf",
58-
"@stdlib/constants/float32/max-nth-double-factorial"
59+
"@stdlib/constants/float32/max-nth-double-factorial",
60+
"@stdlib/math/base/assert/is-nonnegative-integerf"
5961
]
6062
},
6163
{
@@ -69,9 +71,10 @@
6971
"libraries": [],
7072
"libpath": [],
7173
"dependencies": [
72-
"@stdlib/math/base/assert/int32-is-even",
74+
"@stdlib/math/base/assert/is-evenf",
7375
"@stdlib/constants/float32/pinf",
74-
"@stdlib/constants/float32/max-nth-double-factorial"
76+
"@stdlib/constants/float32/max-nth-double-factorial",
77+
"@stdlib/math/base/assert/is-nonnegative-integerf"
7578
]
7679
}
7780
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_F( stdlib_base_factorial2f )
22+
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_factorial2f )

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

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

1919
#include "stdlib/math/base/special/factorial2f.h"
20-
#include "stdlib/math/base/assert/int32_is_even.h"
20+
#include "stdlib/math/base/assert/is_evenf.h"
2121
#include "stdlib/constants/float32/pinf.h"
2222
#include "stdlib/constants/float32/max_nth_double_factorial.h"
23-
#include <stdint.h>
23+
#include "stdlib/math/base/assert/is_nonnegative_integerf.h"
2424

2525
/**
2626
* Evaluates the double factorial of `n` as a single-precision floating-point number.
2727
*
28-
* @param x input value
28+
* @param n input value
2929
* @return double factorial
3030
*
3131
* @example
32-
* float v = stdlib_base_factorial2f( 3 );
32+
* float v = stdlib_base_factorial2f( 3.0f );
3333
* // returns 3.0f
3434
*/
35-
float stdlib_base_factorial2f( const int32_t n ) {
36-
int32_t last;
37-
int32_t i;
35+
float stdlib_base_factorial2f( const float n ) {
36+
float last;
3837
float out;
38+
float i;
3939

40+
if ( !stdlib_base_is_nonnegative_integerf( n ) ) {
41+
return 0.0f / 0.0f; // NaN
42+
}
4043
if ( n > STDLIB_CONSTANT_FLOAT32_MAX_NTH_DOUBLE_FACTORIAL ) {
4144
return STDLIB_CONSTANT_FLOAT32_PINF;
4245
}
43-
if ( n < 0 ) {
44-
return 0.0f / 0.0f; // NaN
45-
}
46-
if ( n == 0 || n == 1 ) {
46+
if ( n == 0.0f || n == 1.0f ) {
4747
return 1.0f;
4848
}
49-
if ( stdlib_base_int32_is_even( n ) ) {
50-
last = 2;
49+
if ( stdlib_base_is_evenf( n ) ) {
50+
last = 2.0f;
5151
} else {
52-
last = 3;
52+
last = 3.0f;
5353
}
5454
out = 1.0f;
55-
for ( i = n; i >= last; i -= 2 ) {
55+
for ( i = n; i >= last; i -= 2.0f ) {
5656
out *= i;
5757
}
5858
return out;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /