summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2003-09-24 12:02:45 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2003-09-24 12:02:45 +0000
commit58cef36c22f5f9d05f526f39ec55d26f71101022 (patch)
tree713cb96eb314667d33b26fe62aa162c81be32592
parent7eb7a9c19210d83f71fd20df04fe2a1754e3a129 (diff)
downloadmpfr-58cef36c22f5f9d05f526f39ec55d26f71101022.tar.gz
Accept (case-insensitive) NaN and Inf in input for bases <= 16 for
backward compatibility in these bases (for bases > 16, we cannot guaranty backward compatibility with the current wanted behavior). Added tests. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@2443 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--TODO6
-rw-r--r--set_str.c6
-rw-r--r--tests/tset_str.c83
3 files changed, 65 insertions, 30 deletions
diff --git a/TODO b/TODO
index 7a0491a3c..b1d766ba6 100644
--- a/TODO
+++ b/TODO
@@ -78,10 +78,8 @@ Changes in existing functions:
The decimal point character, or string, expected is taken from the
current locale on systems providing `localeconv'. [end of suggestion]
-- in mpfr_set_str, make string comparisons case insensitive and use @Inf@
- and @NaN@ instead of the possibly ambiguous Inf and NaN in bases > 10.
- Modify the other functions to make them consistent with this choice.
- Possibly accept other strings, like those accepted by strtod.
+- in mpfr_set_str, possibly accept other strings, like those accepted
+ by strtod.
New functions to implement:
diff --git a/set_str.c b/set_str.c
index f463abc8c..ca02a7ae6 100644
--- a/set_str.c
+++ b/set_str.c
@@ -96,7 +96,8 @@ mpfr_set_str (mpfr_t x, const char *str, int base, mp_rnd_t rnd)
if (base < 2 || base > 36)
return -1;
- if (strncasecmp (str, "@NaN@", 5) == 0)
+ if (strncasecmp (str, "@NaN@", 5) == 0 ||
+ (base <= 16 && strcasecmp (str, "NaN") == 0))
{
MPFR_SET_NAN(x);
/* MPFR_RET_NAN not used as the return value isn't a ternary value */
@@ -111,7 +112,8 @@ mpfr_set_str (mpfr_t x, const char *str, int base, mp_rnd_t rnd)
/* be careful that 'inf' is a valid number in base >= 24,
since i=18, n=23, f=15 */
- if (strncasecmp (str, "@Inf@", 5) == 0)
+ if (strncasecmp (str, "@Inf@", 5) == 0 ||
+ (base <= 16 && strcasecmp (str, "Inf") == 0))
{
MPFR_CLEAR_NAN (x);
MPFR_SET_INF (x);
diff --git a/tests/tset_str.c b/tests/tset_str.c
index 9ed50ba04..4d1b59a9e 100644
--- a/tests/tset_str.c
+++ b/tests/tset_str.c
@@ -182,31 +182,66 @@ main (int argc, char *argv[])
(*__gmp_free_func) (str, strlen (str) + 1);
}
- if (mpfr_set_str (x, "@NaN@garbage", 10, GMP_RNDN) != 0 || !mpfr_nan_p(x))
+ for (i = 2; i <= 36; i++)
{
- fprintf (stderr, "mpfr_set_str failed on NaN\n");
- exit (1);
- }
-
- if (mpfr_set_str (x, "@Inf@garbage", 10, GMP_RNDN) != 0 || !mpfr_inf_p(x) ||
- MPFR_SIGN(x) < 0)
- {
- fprintf (stderr, "mpfr_set_str failed on Inf\n");
- exit (1);
- }
-
- if (mpfr_set_str (x, "-@Inf@garbage", 10, GMP_RNDN) != 0 || !mpfr_inf_p(x) ||
- MPFR_SIGN(x) > 0)
- {
- fprintf (stderr, "mpfr_set_str failed on -Inf\n");
- exit (1);
- }
-
- if (mpfr_set_str (x, "+@Inf@garbage", 10, GMP_RNDN) != 0 || !mpfr_inf_p(x) ||
- MPFR_SIGN(x) < 0)
- {
- fprintf (stderr, "mpfr_set_str failed on +Inf\n");
- exit (1);
+ if (mpfr_set_str (x, "@NaN@garbage", i, GMP_RNDN) != 0 ||
+ !mpfr_nan_p(x))
+ {
+ fprintf (stderr, "mpfr_set_str failed on @NaN@garbage\n");
+ exit (1);
+ }
+
+ if (mpfr_set_str (x, "@Inf@garbage", i, GMP_RNDN) != 0 ||
+ !mpfr_inf_p(x) || MPFR_SIGN(x) < 0)
+ {
+ fprintf (stderr, "mpfr_set_str failed on @Inf@garbage\n");
+ exit (1);
+ }
+
+ if (mpfr_set_str (x, "-@Inf@garbage", i, GMP_RNDN) != 0 ||
+ !mpfr_inf_p(x) || MPFR_SIGN(x) > 0)
+ {
+ fprintf (stderr, "mpfr_set_str failed on -@Inf@garbage\n");
+ exit (1);
+ }
+
+ if (mpfr_set_str (x, "+@Inf@garbage", i, GMP_RNDN) != 0 ||
+ !mpfr_inf_p(x) || MPFR_SIGN(x) < 0)
+ {
+ fprintf (stderr, "mpfr_set_str failed on +@Inf@garbage\n");
+ exit (1);
+ }
+
+ if (i > 16)
+ continue;
+
+ if (mpfr_set_str (x, "NaN", i, GMP_RNDN) != 0 ||
+ !mpfr_nan_p(x))
+ {
+ fprintf (stderr, "mpfr_set_str failed on NaN\n");
+ exit (1);
+ }
+
+ if (mpfr_set_str (x, "Inf", i, GMP_RNDN) != 0 ||
+ !mpfr_inf_p(x) || MPFR_SIGN(x) < 0)
+ {
+ fprintf (stderr, "mpfr_set_str failed on Inf\n");
+ exit (1);
+ }
+
+ if (mpfr_set_str (x, "-Inf", i, GMP_RNDN) != 0 ||
+ !mpfr_inf_p(x) || MPFR_SIGN(x) > 0)
+ {
+ fprintf (stderr, "mpfr_set_str failed on -Inf\n");
+ exit (1);
+ }
+
+ if (mpfr_set_str (x, "+Inf", i, GMP_RNDN) != 0 ||
+ !mpfr_inf_p(x) || MPFR_SIGN(x) < 0)
+ {
+ fprintf (stderr, "mpfr_set_str failed on +Inf\n");
+ exit (1);
+ }
}
/* check that mpfr_set_str works for uppercase letters too */