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 862befd

Browse files
refactor!: modify C implementation to accept double instead of int32 in math/base/special/factorial2
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: #8029 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 5967ff0 commit 862befd

File tree

8 files changed

+37
-41
lines changed

8 files changed

+37
-41
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ for ( i = 0; i < values.length; i++ ) {
124124
Evaluates the [double factorial][double-factorial] of `n`.
125125

126126
```c
127-
double out = stdlib_base_factorial2( 3 );
128-
// returns 3
127+
double out = stdlib_base_factorial2( 3.0 );
128+
// returns 3.0
129129
```
130130

131131
The function accepts the following arguments:
132132

133-
- **n**: `[in] int32_t` input value.
133+
- **n**: `[in] double` input value.
134134

135135
```c
136-
double stdlib_base_factorial2( const int32_t n );
136+
double stdlib_base_factorial2( const double n );
137137
```
138138
139139
</section>
@@ -157,16 +157,15 @@ double stdlib_base_factorial2( const int32_t n );
157157
```c
158158
#include "stdlib/math/base/special/factorial2.h"
159159
#include <stdio.h>
160-
#include <stdint.h>
161160
162161
int main( void ) {
163-
const int32_t x[] = { 1, 10, 100, 301, 302 };
162+
const double x[] = { 1.0, 10.0, 100.0, 301.0, 302.0 };
164163
165164
double b;
166165
int i;
167166
for ( i = 0; i < 5; i++ ){
168167
b = stdlib_base_factorial2( x[ i ] );
169-
printf ( "factorial2(%d) = %lf\n", x[ i ], b );
168+
printf ( "factorial2(%lf) = %lf\n", x[ i ], b );
170169
}
171170
}
172171
```

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ static double rand_double( void ) {
9292
*/
9393
static double benchmark( void ) {
9494
double elapsed;
95-
int32_t x;
95+
double x;
9696
double y;
9797
double t;
9898
int i;
9999

100100
t = tic();
101101
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = (int32_t)(rand_double() * 301);
102+
x = round( rand_double() * 301.0);
103103
y = stdlib_base_factorial2( x );
104104
if ( y != y ) {
105105
printf( "should not return NaN\n" );

‎lib/node_modules/@stdlib/math/base/special/factorial2/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/factorial2.h"
2020
#include <stdio.h>
21-
#include <stdint.h>
2221

2322
int main( void ) {
24-
const int32_t x[] = { 1, 10, 100, 301, 302 };
23+
const double x[] = { 1.0, 10.0, 100.0, 301.0, 302.0 };
2524

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

‎lib/node_modules/@stdlib/math/base/special/factorial2/include/stdlib/math/base/special/factorial2.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_FACTORIAL2_H
2020
#define STDLIB_MATH_BASE_SPECIAL_FACTORIAL2_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`.
3331
*/
34-
double stdlib_base_factorial2( const int32_t n );
32+
double stdlib_base_factorial2( const double n );
3533

3634
#ifdef __cplusplus
3735
}

‎lib/node_modules/@stdlib/math/base/special/factorial2/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 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 isEven = require( '@stdlib/math/base/assert/is-even' );
2626
var PINF = require( '@stdlib/constants/float64/pinf' );
2727
var FLOAT64_MAX_NTH_DOUBLE_FACTORIAL = require( '@stdlib/constants/float64/max-nth-double-factorial' ); // eslint-disable-line id-length
@@ -56,15 +56,12 @@ function factorial2( n ) {
5656
var out;
5757
var v;
5858
var i;
59-
if ( isnan( n ) ) {
59+
if ( isnan( n ) ||!isNonnegativeInteger(n)) {
6060
return NaN;
6161
}
6262
if ( n > FLOAT64_MAX_NTH_DOUBLE_FACTORIAL ) {
6363
return PINF;
6464
}
65-
if ( n < 0 || isInteger( n ) === false ) {
66-
return NaN;
67-
}
6865
v = n|0; // asm type annotation
6966
if ( v === 0|0 || v === 1|0 ) {
7067
return 1|0; // asm type annotation

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"@stdlib/math/base/napi/unary",
4040
"@stdlib/math/base/assert/is-even",
4141
"@stdlib/constants/float64/pinf",
42-
"@stdlib/constants/float64/max-nth-double-factorial"
42+
"@stdlib/constants/float64/max-nth-double-factorial",
43+
"@stdlib/math/base/assert/is-nonnegative-integer"
4344
]
4445
},
4546
{
@@ -55,7 +56,8 @@
5556
"dependencies": [
5657
"@stdlib/math/base/assert/is-even",
5758
"@stdlib/constants/float64/pinf",
58-
"@stdlib/constants/float64/max-nth-double-factorial"
59+
"@stdlib/constants/float64/max-nth-double-factorial",
60+
"@stdlib/math/base/assert/is-nonnegative-integer"
5961
]
6062
},
6163
{
@@ -71,7 +73,8 @@
7173
"dependencies": [
7274
"@stdlib/math/base/assert/is-even",
7375
"@stdlib/constants/float64/pinf",
74-
"@stdlib/constants/float64/max-nth-double-factorial"
76+
"@stdlib/constants/float64/max-nth-double-factorial",
77+
"@stdlib/math/base/assert/is-nonnegative-integer"
7578
]
7679
}
7780
]

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_D( stdlib_base_factorial2 )
22+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_factorial2 )

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,42 @@
1818

1919
#include "stdlib/math/base/special/factorial2.h"
2020
#include "stdlib/math/base/assert/is_even.h"
21+
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
2122
#include "stdlib/constants/float64/pinf.h"
2223
#include "stdlib/constants/float64/max_nth_double_factorial.h"
23-
#include <stdint.h>
2424

2525
/**
2626
* Evaluates the double factorial of `n`.
2727
*
28-
* @param x input value
28+
* @param n input value
2929
* @return double factorial
3030
*
3131
* @example
32-
* double v = stdlib_base_factorial2( 3 );
33-
* // returns 3
32+
* double v = stdlib_base_factorial2( 3.0 );
33+
* // returns 3.0
3434
*/
35-
double stdlib_base_factorial2( const int32_t n ) {
36-
int32_t last;
35+
double stdlib_base_factorial2( const double n ) {
36+
double last;
3737
double out;
38-
int32_t v;
39-
int32_t i;
38+
double v;
39+
double i;
40+
if ( !stdlib_base_is_nonnegative_integer( n ) ) {
41+
return 0.0 / 0.0; // NaN
42+
}
4043
if ( n > STDLIB_CONSTANT_FLOAT64_MAX_NTH_DOUBLE_FACTORIAL ) {
4144
return STDLIB_CONSTANT_FLOAT64_PINF;
4245
}
43-
if ( n < 0 ) {
44-
return 0.0/0.0;
45-
}
4646
v = n;
47-
if ( v == 0 || v == 1 ) {
48-
return 1;
47+
if ( v == 0.0 || v == 1.0 ) {
48+
return 1.0;
4949
}
5050
if ( stdlib_base_is_even( v ) ) {
51-
last = 2;
51+
last = 2.0;
5252
} else {
53-
last = 3;
53+
last = 3.0;
5454
}
55-
out = 1;
56-
for ( i = v; i >= last; i -= 2 ) {
55+
out = 1.0;
56+
for ( i = v; i >= last; i -= 2.0 ) {
5757
out *= i;
5858
}
5959
return out;

0 commit comments

Comments
(0)

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