Skip to content

Commit 634baa5

Browse files
anchaoxiaoxiang781216
authored andcommitted
libs: workaround for Visual Studio(MSVC) Compiler Error C2124
D:\archer\code\nuttx\libs\libc\stdlib\lib_strtod.c: error C2124: divide or mod by zero Windows MSVC restrictions, MSVC doesn't allow division through a zero literal, but allows it through const variable set to zero Reference: https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2124?view=msvc-170 Signed-off-by: chao an <[email protected]>
1 parent 86aed87 commit 634baa5

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

include/nuttx/lib/math.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,19 @@
7474

7575
/* General Constants ********************************************************/
7676

77-
#define INFINITY (1.0/0.0)
78-
#define NAN (0.0/0.0)
79-
#define HUGE_VAL INFINITY
77+
#ifndef _HUGE_ENUF
78+
# define _HUGE_ENUF (1e+300) /* _HUGE_ENUF*_HUGE_ENUF must overflow */
79+
#endif
80+
81+
#define INFINITY ((double)(_HUGE_ENUF * _HUGE_ENUF))
82+
#define NAN ((double)(INFINITY * 0.0F))
83+
#define HUGE_VAL INFINITY
8084

81-
#define INFINITY_F (1.0F/0.0F)
82-
#define NAN_F (0.0F/0.0F)
85+
#define INFINITY_F ((float)INFINITY)
86+
#define NAN_F ((float)(INFINITY * 0.0F))
8387

84-
#define INFINITY_L (1.0L/0.0L)
85-
#define NAN_L (0.0L/0.0L)
88+
#define INFINITY_L ((long double)INFINITY)
89+
#define NAN_L ((long double)(INFINITY * 0.0F))
8690

8791
#define isnan(x) ((x) != (x))
8892
#define isnanf(x) ((x) != (x))

libs/libc/math/lib_gamma.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@
3232
*
3333
****************************************************************************/
3434

35-
/* "A Precision Approximation of the Gamma Function" - Cornelius Lanczos (1964)
36-
* "Lanczos Implementation of the Gamma Function" - Paul Godfrey (2001)
37-
* "An Analysis of the Lanczos Gamma Approximation" - Glendon Ralph Pugh (2004)
35+
/* "A Precision Approximation of the Gamma Function"
36+
* - Cornelius Lanczos (1964)
37+
* "Lanczos Implementation of the Gamma Function"
38+
* - Paul Godfrey (2001)
39+
* "An Analysis of the Lanczos Gamma Approximation"
40+
* - Glendon Ralph Pugh (2004)
3841
*
3942
* Approximation method:
4043
*
@@ -133,9 +136,10 @@ static const double g_sden[N + 1] =
133136
static const double g_fact[] =
134137
{
135138
1, 1, 2, 6, 24, 120, 720, 5040.0, 40320.0, 362880.0, 3628800.0, 39916800.0,
136-
479001600.0, 6227020800.0, 87178291200.0, 1307674368000.0, 20922789888000.0,
137-
355687428096000.0, 6402373705728000.0, 121645100408832000.0,
138-
2432902008176640000.0, 51090942171709440000.0, 1124000727777607680000.0,
139+
479001600.0, 6227020800.0, 87178291200.0, 1307674368000.0,
140+
20922789888000.0, 355687428096000.0, 6402373705728000.0,
141+
121645100408832000.0, 2432902008176640000.0, 51090942171709440000.0,
142+
1124000727777607680000.0,
139143
};
140144

141145
/* S(x) rational function for positive x */
@@ -151,6 +155,7 @@ static double sinpi(double x)
151155
int n;
152156

153157
/* argument reduction: x = |x| mod 2 */
158+
154159
/* spurious inexact when x is odd int */
155160

156161
x = x * 0.5;
@@ -205,7 +210,7 @@ static double s(double x)
205210
}
206211
}
207212

208-
return num/den;
213+
return num / den;
209214
}
210215

211216
/****************************************************************************
@@ -219,6 +224,7 @@ double tgamma(double x)
219224
double f;
220225
uint64_t i;
221226
} u;
227+
222228
u.f = x;
223229

224230
double absx;
@@ -241,17 +247,19 @@ double tgamma(double x)
241247
if (ix < (0x3ff - 54) << 20)
242248
{
243249
/* |x| < 2^-54: tgamma(x) ~ 1/x, +-0 raises div-by-zero */
250+
244251
return 1 / x;
245252
}
246253

247254
/* integer arguments */
255+
248256
/* raise inexact when non-integer */
249257

250258
if (x == floor(x))
251259
{
252260
if (sign)
253261
{
254-
return 0 / 0.0;
262+
return NAN;
255263
}
256264

257265
if (x <= sizeof g_fact / sizeof *g_fact)
@@ -261,6 +269,7 @@ double tgamma(double x)
261269
}
262270

263271
/* x >= 172: tgamma(x)=inf with overflow */
272+
264273
/* x =< -184: tgamma(x)=+-0 with underflow */
265274

266275
if (ix >= 0x40670000)
@@ -269,11 +278,13 @@ double tgamma(double x)
269278

270279
if (sign)
271280
{
272-
FORCE_EVAL((float)(0x1p-126 / x));
281+
FORCE_EVAL((float)(ldexp(1.0, -126) / x));
282+
273283
if (floor(x) * 0.5 == floor(x * 0.5))
274284
{
275285
return 0;
276286
}
287+
277288
return -0.0;
278289
}
279290

@@ -302,6 +313,7 @@ double tgamma(double x)
302313
if (x < 0)
303314
{
304315
/* reflection formula for negative x */
316+
305317
/* sinpi(absx) is not 0, integers are already handled */
306318

307319
r = -pi / (sinpi(absx) * absx * r);

0 commit comments

Comments
 (0)