Use C99-defined macros to classify a floating-point number

This commit is contained in:
Nobuyoshi Nakada 2021-08-27 10:52:02 +09:00
parent 3711467362
commit 04be8e84db
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6
Notes: git 2021-08-27 15:41:07 +09:00
15 changed files with 11 additions and 205 deletions

3
LEGAL
View File

@ -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]

View File

@ -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)

View File

@ -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

View File

@ -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)
@ -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"],

View File

@ -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
@ -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

View File

@ -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
@ -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

View File

@ -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

View File

@ -1,9 +0,0 @@
/* public domain rewrite of finite(3) */
#include "ruby/missing.h"
int
finite(double n)
{
return !isnan(n) && !isinf(n);
}

View File

@ -1,69 +0,0 @@
/* public domain rewrite of isinf(3) */
#ifdef __osf__
#define _IEEE 1
#include <nan.h>
int
isinf(double n)
{
if (IsNANorINF(n) && IsINF(n)) {
return 1;
}
else {
return 0;
}
}
#else
#include "ruby/internal/config.h"
#if defined(HAVE_FINITE) && defined(HAVE_ISNAN)
#include <math.h>
#ifdef HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
/*
* isinf may be provided only as a macro.
* ex. HP-UX, Solaris 10
* http://www.gnu.org/software/automake/manual/autoconf/Function-Portability.html
*/
#ifndef isinf
int
isinf(double n)
{
return (!finite(n) && !isnan(n));
}
#endif
#else
#ifdef HAVE_STRING_H
# include <string.h>
#else
# include <strings.h>
#endif
static double zero(void) { return 0.0; }
static double one (void) { return 1.0; }
static double inf (void) { return one() / zero(); }
int
isinf(double n)
{
static double pinf = 0.0;
static double ninf = 0.0;
if (pinf == 0.0) {
pinf = inf();
ninf = -pinf;
}
return memcmp(&n, &pinf, sizeof n) == 0
|| memcmp(&n, &ninf, sizeof n) == 0;
}
#endif
#endif

View File

@ -1,32 +0,0 @@
/* public domain rewrite of isnan(3) */
#include "ruby/missing.h"
/*
* isnan() may be a macro, a function or both.
* (The C99 standard defines that isnan() is a macro, though.)
* http://www.gnu.org/software/automake/manual/autoconf/Function-Portability.html
*
* macro only: uClibc
* both: GNU libc
*
* This file is compile if no isnan() function is available.
* (autoconf AC_REPLACE_FUNCS detects only the function.)
* The macro is detected by following #ifndef.
*/
#ifndef isnan
static int double_ne(double n1, double n2);
int
isnan(double n)
{
return double_ne(n, n);
}
static int
double_ne(double n1, double n2)
{
return n1 != n2;
}
#endif

View File

@ -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>

View File

@ -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

View File

@ -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;

View File

@ -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);
@ -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);

View File

@ -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;