Skip to content

Commit

Permalink
Use C99-defined macros to classify a floating-point number
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Aug 27, 2021
1 parent 3711467 commit 04be8e8
Show file tree
Hide file tree
Showing 15 changed files with 11 additions and 205 deletions.
3 changes: 0 additions & 3 deletions LEGAL
Expand Up @@ -532,10 +532,7 @@ mentioned below.
[missing/acosh.c]
[missing/alloca.c]
[missing/erf.c]
[missing/finite.c]
[missing/hypot.c]
[missing/isinf.c]
[missing/isnan.c]
[missing/lgamma_r.c]
[missing/memcmp.c]
[missing/memmove.c]
Expand Down
2 changes: 1 addition & 1 deletion bignum.c
Expand Up @@ -5382,7 +5382,7 @@ rb_integer_float_eq(VALUE x, VALUE y)
double yd = RFLOAT_VALUE(y);
double yi, yf;

if (isnan(yd) || isinf(yd))
if (!isfinite(yd))
return Qfalse;
yf = modf(yd, &yi);
if (yf != 0)
Expand Down
1 change: 0 additions & 1 deletion common.mk
Expand Up @@ -963,7 +963,6 @@ crypt.$(OBJEXT): {$(VPATH)}crypt.c {$(VPATH)}crypt.h {$(VPATH)}missing/des_table
dup2.$(OBJEXT): {$(VPATH)}dup2.c
erf.$(OBJEXT): {$(VPATH)}erf.c
explicit_bzero.$(OBJEXT): {$(VPATH)}explicit_bzero.c
finite.$(OBJEXT): {$(VPATH)}finite.c
flock.$(OBJEXT): {$(VPATH)}flock.c
memcmp.$(OBJEXT): {$(VPATH)}memcmp.c
memmove.$(OBJEXT): {$(VPATH)}memmove.c
Expand Down
6 changes: 2 additions & 4 deletions configure.ac
Expand Up @@ -799,7 +799,7 @@ AS_IF([test "$GCC" = yes], [
])
],
[cygwin*|msys*|darwin*|netbsd*], [
# need lgamma_r(), finite()
# need lgamma_r()
])
# ANSI (no XCFLAGS because this is C only)
Expand Down Expand Up @@ -1906,9 +1906,7 @@ AC_REPLACE_FUNCS(strlcpy)
AC_REPLACE_FUNCS(strstr)
AC_REPLACE_FUNCS(tgamma)

RUBY_REPLACE_FUNC([finite], [@%:@include <math.h>])
RUBY_REPLACE_FUNC([isinf], [@%:@include <math.h>])
RUBY_REPLACE_FUNC([isnan], [@%:@include <math.h>])
AC_DEFINE(HAVE_ISFINITE) # C99; backward compatibility

# for missing/setproctitle.c
AS_CASE(["$target_os"],
Expand Down
36 changes: 3 additions & 33 deletions include/ruby/missing.h
Expand Up @@ -92,10 +92,6 @@ RUBY_EXTERN int eaccess(const char*, int);
RUBY_EXTERN double round(double); /* numeric.c */
#endif

#ifndef HAVE_FINITE
RUBY_EXTERN int finite(double);
#endif

#ifndef HAVE_FLOCK
RUBY_EXTERN int flock(int, int);
#endif
Expand Down Expand Up @@ -152,35 +148,9 @@ RUBY_EXTERN const union bytesequence4_or_float rb_nan;
# define HUGE_VAL ((double)INFINITY)
#endif

#if defined(isinf)
# /* Take that. */
#elif defined(HAVE_ISINF)
# /* Take that. */
#elif defined(HAVE_FINITE) && defined(HAVE_ISNAN)
# define isinf(x) (!finite(x) && !isnan(x))
#elif defined(__cplusplus) && __cplusplus >= 201103L
# // <cmath> must include constexpr bool isinf(double);
#else
RUBY_EXTERN int isinf(double);
#endif

#if defined(isnan)
# /* Take that. */
#elif defined(HAVE_ISNAN)
# /* Take that. */
#elif defined(__cplusplus) && __cplusplus >= 201103L
# // <cmath> must include constexpr bool isnan(double);
#else
RUBY_EXTERN int isnan(double);
#endif

#if defined(isfinite)
# /* Take that. */
#elif defined(HAVE_ISFINITE)
# /* Take that. */
#else
# define HAVE_ISFINITE 1
# define isfinite(x) finite(x)
#ifndef HAVE_FINITE
# define HAVE_FINITE 1
# define finite(x) isfinite(x)
#endif

#ifndef HAVE_NAN
Expand Down
10 changes: 0 additions & 10 deletions include/ruby/win32.h
Expand Up @@ -343,14 +343,6 @@ rb_infinity_float(void)
#endif

#if !defined __MINGW32__ || defined __NO_ISOCEXT
#ifndef isnan
#define isnan(x) _isnan(x)
#endif
static inline int
finite(double x)
{
return _finite(x);
}
#ifndef copysign
#define copysign(a, b) _copysign(a, b)
#endif
Expand All @@ -359,8 +351,6 @@ scalb(double a, long b)
{
return _scalb(a, b);
}
#else
__declspec(dllimport) extern int finite(double);
#endif

#if !defined S_IFIFO && defined _S_IFIFO
Expand Down
15 changes: 0 additions & 15 deletions missing/erf.c
Expand Up @@ -7,21 +7,6 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
#include <stdio.h>
#include <math.h>

#ifdef _WIN32
# include <float.h>
# if !defined __MINGW32__ || defined __NO_ISOCEXT
# ifndef isnan
# define isnan(x) _isnan(x)
# endif
# ifndef isinf
# define isinf(x) (!_finite(x) && !_isnan(x))
# endif
# ifndef finite
# define finite(x) _finite(x)
# endif
# endif
#endif

static double q_gamma(double, double, double);

/* Incomplete gamma function
Expand Down
9 changes: 0 additions & 9 deletions missing/finite.c

This file was deleted.

69 changes: 0 additions & 69 deletions missing/isinf.c

This file was deleted.

32 changes: 0 additions & 32 deletions missing/isnan.c

This file was deleted.

15 changes: 0 additions & 15 deletions missing/tgamma.c
Expand Up @@ -14,21 +14,6 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
#include <math.h>
#include <errno.h>

#ifdef _WIN32
# include <float.h>
# if !defined __MINGW32__ || defined __NO_ISOCEXT
# ifndef isnan
# define isnan(x) _isnan(x)
# endif
# ifndef isinf
# define isinf(x) (!_finite(x) && !_isnan(x))
# endif
# ifndef finite
# define finite(x) _finite(x)
# endif
# endif
#endif

#ifndef HAVE_LGAMMA_R

#include <errno.h>
Expand Down
10 changes: 1 addition & 9 deletions numeric.c
Expand Up @@ -1699,15 +1699,7 @@ rb_flo_is_finite_p(VALUE num)
{
double value = RFLOAT_VALUE(num);

#ifdef HAVE_ISFINITE
if (!isfinite(value))
return Qfalse;
#else
if (isinf(value) || isnan(value))
return Qfalse;
#endif

return Qtrue;
return RBOOL(isfinite(value));
}

static VALUE
Expand Down
2 changes: 1 addition & 1 deletion random.c
Expand Up @@ -1348,7 +1348,7 @@ static inline double
float_value(VALUE v)
{
double x = RFLOAT_VALUE(v);
if (isinf(x) || isnan(x)) {
if (!isfinite(x)) {
domain_error();
}
return x;
Expand Down
4 changes: 2 additions & 2 deletions rational.c
Expand Up @@ -2096,7 +2096,7 @@ rb_float_numerator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
if (isinf(d) || isnan(d))
if (!isfinite(d))
return self;
r = float_to_r(self);
return nurat_numerator(r);
Expand All @@ -2116,7 +2116,7 @@ rb_float_denominator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
if (isinf(d) || isnan(d))
if (!isfinite(d))
return INT2FIX(1);
r = float_to_r(self);
return nurat_denominator(r);
Expand Down
2 changes: 1 addition & 1 deletion sprintf.c
Expand Up @@ -875,7 +875,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
double fval;

fval = RFLOAT_VALUE(rb_Float(val));
if (isnan(fval) || isinf(fval)) {
if (!isfinite(fval)) {
const char *expr;
int need;
int elen;
Expand Down

0 comments on commit 04be8e8

Please sign in to comment.