From b80689141673b93e8d12968c3196ec6a2331da45 Mon Sep 17 00:00:00 2001

From: Nobuyoshi Nakada <nobu@ruby-lang.org>
Date: Mon, 16 Aug 2010 18:55:11 +0900
Subject: [PATCH 2/2] 	* util.c (ruby_dtoa, ruby_hdtoa): use same representations for
 	  Infinity and NaN.  a part of a patch from Peter Weldon at
 	  [ruby-core:31725].

---
 util.c |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/util.c b/util.c
index 065b2f1..76ba457 100644
--- a/util.c
+++ b/util.c
@@ -3145,6 +3145,10 @@ freedtoa(char *s)
 }
 #endif

+static const char INFSTR[] = "Infinity";
+static const char NANSTR[] = "NaN";
+static const char ZEROSTR[] = "0";
+
 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  *
  * Inspired by "How to Print Floating-Point Numbers Accurately" by
@@ -3263,9 +3267,9 @@ ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
         *decpt = 9999;
 #ifdef IEEE_Arith
         if (!word1(d) && !(word0(d) & 0xfffff))
-            return rv_strdup("Infinity", rve);
+            return rv_strdup(INFSTR, rve);
 #endif
-        return rv_strdup("NaN", rve);
+        return rv_strdup(NANSTR, rve);
     }
 #endif
 #ifdef IBM
@@ -3273,7 +3277,7 @@ ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
 #endif
     if (!dval(d)) {
         *decpt = 1;
-        return rv_strdup("0", rve);
+        return rv_strdup(ZEROSTR, rve);
     }

 #ifdef SET_INEXACT
@@ -3897,8 +3901,6 @@ ruby_each_words(const char *str, void (*func)(const char*, int, void*), void *ar

 #define	DBL_MANH_SIZE	20
 #define	DBL_MANL_SIZE	32
-#define	INFSTR	"Infinity"
-#define	NANSTR	"NaN"
 #define	DBL_ADJ	(DBL_MAX_EXP - 2)
 #define	SIGFIGS	((DBL_MANT_DIG + 3) / 4 + 1)
 #define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)
@@ -3959,7 +3961,7 @@ ruby_hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
 	}
 	else if (d == 0.0) { /* FP_ZERO */
 	    *decpt = 1;
-	    return rv_strdup("0", rve);
+	    return rv_strdup(ZEROSTR, rve);
 	}
 	else if (dexp_get(u)) { /* FP_NORMAL */
 	    *decpt = dexp_get(u) - DBL_ADJ;
--
1.7.0.4

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29011 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2010-08-16 10:23:22 +00:00
Родитель d17ff1a6a5
Коммит a043fb8642
2 изменённых файлов: 13 добавлений и 7 удалений

Просмотреть файл

@ -1,4 +1,8 @@
Mon Aug 16 19:22:31 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> Mon Aug 16 19:23:19 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* util.c (ruby_dtoa, ruby_hdtoa): use same representations for
Infinity and NaN. a part of a patch from Peter Weldon at
[ruby-core:31725].
* util.c (ruby_hdtoa): fixed buffer overrun. based on a patch * util.c (ruby_hdtoa): fixed buffer overrun. based on a patch
from Peter Weldon at [ruby-core:31725]. from Peter Weldon at [ruby-core:31725].

14
util.c
Просмотреть файл

@ -3145,6 +3145,10 @@ freedtoa(char *s)
} }
#endif #endif
static const char INFSTR[] = "Infinity";
static const char NANSTR[] = "NaN";
static const char ZEROSTR[] = "0";
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
* *
* Inspired by "How to Print Floating-Point Numbers Accurately" by * Inspired by "How to Print Floating-Point Numbers Accurately" by
@ -3263,9 +3267,9 @@ ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
*decpt = 9999; *decpt = 9999;
#ifdef IEEE_Arith #ifdef IEEE_Arith
if (!word1(d) && !(word0(d) & 0xfffff)) if (!word1(d) && !(word0(d) & 0xfffff))
return rv_strdup("Infinity", rve); return rv_strdup(INFSTR, rve);
#endif #endif
return rv_strdup("NaN", rve); return rv_strdup(NANSTR, rve);
} }
#endif #endif
#ifdef IBM #ifdef IBM
@ -3273,7 +3277,7 @@ ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
#endif #endif
if (!dval(d)) { if (!dval(d)) {
*decpt = 1; *decpt = 1;
return rv_strdup("0", rve); return rv_strdup(ZEROSTR, rve);
} }
#ifdef SET_INEXACT #ifdef SET_INEXACT
@ -3897,8 +3901,6 @@ ruby_each_words(const char *str, void (*func)(const char*, int, void*), void *ar
#define DBL_MANH_SIZE 20 #define DBL_MANH_SIZE 20
#define DBL_MANL_SIZE 32 #define DBL_MANL_SIZE 32
#define INFSTR "Infinity"
#define NANSTR "NaN"
#define DBL_ADJ (DBL_MAX_EXP - 2) #define DBL_ADJ (DBL_MAX_EXP - 2)
#define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1) #define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1)
#define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1) #define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)
@ -3959,7 +3961,7 @@ ruby_hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
} }
else if (d == 0.0) { /* FP_ZERO */ else if (d == 0.0) { /* FP_ZERO */
*decpt = 1; *decpt = 1;
return rv_strdup("0", rve); return rv_strdup(ZEROSTR, rve);
} }
else if (dexp_get(u)) { /* FP_NORMAL */ else if (dexp_get(u)) { /* FP_NORMAL */
*decpt = dexp_get(u) - DBL_ADJ; *decpt = dexp_get(u) - DBL_ADJ;