summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Bodrato <bodrato@mail.dm.unipi.it>2018-05-04 21:43:34 +0200
committerMarco Bodrato <bodrato@mail.dm.unipi.it>2018-05-04 21:43:34 +0200
commit071f5e609c5601165a07d68fb077f88140592c7f (patch)
tree537792b5431c315ceecb6892aea2ea948a443f12
parent0bbd79a595602e3f72bb7234342ff69fd0adc2d3 (diff)
downloadgmp-071f5e609c5601165a07d68fb077f88140592c7f.tar.gz
mpq_*_str: document and support the correct base range
-rw-r--r--ChangeLog5
-rw-r--r--doc/gmp.texi26
-rw-r--r--mpq/get_str.c13
3 files changed, 33 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index 6d027e6b4..86885c20a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2018-05-04 Marco Bodrato <bodrato@mail.dm.unipi.it>
+
+ * doc/gmp.texi (mpq_*_str): Document the full base allowed range.
+ * mpq/get_str.c: Make all bases either work or return an error.
+
2018-04-27 Niels Möller <nisse@lysator.liu.se>
* mpn/generic/div_q.c (mpn_div_q): Replace dead code with ASSERT.
diff --git a/doc/gmp.texi b/doc/gmp.texi
index 9ebf2b082..15ce9ad85 100644
--- a/doc/gmp.texi
+++ b/doc/gmp.texi
@@ -14,7 +14,7 @@
This manual describes how to install and use the GNU multiple precision
arithmetic library, version @value{VERSION}.
-Copyright 1991, 1993-2016 Free Software Foundation, Inc.
+Copyright 1991, 1993-2016, 2018 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document under
the terms of the GNU Free Documentation License, Version 1.3 or any later
@@ -4365,9 +4365,14 @@ is exact.
@end deftypefun
@deftypefun {char *} mpq_get_str (char *@var{str}, int @var{base}, const mpq_t @var{op})
-Convert @var{op} to a string of digits in base @var{base}. The base may vary
-from 2 to 36. The string will be of the form @samp{num/den}, or if the
-denominator is 1 then just @samp{num}.
+Convert @var{op} to a string of digits in base @var{base}. The base argument
+may vary from 2 to 62 or from @minus{}2 to @minus{}36. The string will be of
+the form @samp{num/den}, or if the denominator is 1 then just @samp{num}.
+
+For @var{base} in the range 2..36, digits and lower-case letters are used; for
+@minus{}2..@minus{}36, digits and upper-case letters are used; for 37..62,
+digits, upper-case letters, and lower-case letters (in that significance order)
+are used.
If @var{str} is @code{NULL}, the result string is allocated using the current
allocation function (@pxref{Custom Allocation}). The block will be
@@ -4535,9 +4540,15 @@ See also @ref{Formatted Output} and @ref{Formatted Input}.
@deftypefun size_t mpq_out_str (FILE *@var{stream}, int @var{base}, const mpq_t @var{op})
Output @var{op} on stdio stream @var{stream}, as a string of digits in base
-@var{base}. The base may vary from 2 to 36. Output is in the form
+@var{base}. The base argument may vary from 2 to 62 or from @minus{}2 to
+@minus{}36. Output is in the form
@samp{num/den} or if the denominator is 1 then just @samp{num}.
+For @var{base} in the range 2..36, digits and lower-case letters are used; for
+@minus{}2..@minus{}36, digits and upper-case letters are used; for 37..62,
+digits, upper-case letters, and lower-case letters (in that significance order)
+are used.
+
Return the number of bytes written, or if an error occurred, return 0.
@end deftypefun
@@ -4553,9 +4564,10 @@ space is not permitted within the string. If the input might not be in
canonical form, then @code{mpq_canonicalize} must be called (@pxref{Rational
Number Functions}).
-The @var{base} can be between 2 and 36, or can be 0 in which case the leading
+The @var{base} can be between 2 and 62, or can be 0 in which case the leading
characters of the string determine the base, @samp{0x} or @samp{0X} for
-hexadecimal, @samp{0} for octal, or decimal otherwise. The leading characters
+hexadecimal, @code{0b} and @code{0B} for binary, @samp{0} for octal, or
+decimal otherwise. The leading characters
are examined separately for the numerator and denominator of a fraction, so
for instance @samp{0x10/11} is @math{16/11}, whereas @samp{0x10/0x11} is
@math{16/17}.
diff --git a/mpq/get_str.c b/mpq/get_str.c
index 881340fb9..d0cfb58dd 100644
--- a/mpq/get_str.c
+++ b/mpq/get_str.c
@@ -1,6 +1,6 @@
/* mpq_get_str -- mpq to string conversion.
-Copyright 2001, 2002, 2006, 2011 Free Software Foundation, Inc.
+Copyright 2001, 2002, 2006, 2011, 2018 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -47,6 +47,8 @@ mpq_get_str (char *str, int base, mpq_srcptr q)
/* This is an overestimate since we don't bother checking how much of
the high limbs of num and den are used. +2 for rounding up the
chars per bit of num and den. +3 for sign, slash and '\0'. */
+ if (ABS(base) < 2)
+ base = 10;
DIGITS_IN_BASE_PER_LIMB (str_alloc, ABSIZ(NUM(q)) + SIZ(DEN(q)), ABS(base));
str_alloc += 6;
@@ -64,9 +66,12 @@ mpq_get_str (char *str, int base, mpq_srcptr q)
ASSERT (len == strlen(str));
ASSERT (str_alloc == 0 || len+1 <= str_alloc);
- ASSERT (len+1 <= /* size recommended to applications */
- mpz_sizeinbase (mpq_numref(q), ABS(base)) +
- mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3);
+ ASSERT (len+1 <= 3 + /* size recommended to applications */
+ (ABS(base) < 2 ?
+ mpz_sizeinbase (mpq_numref(q), 10) +
+ mpz_sizeinbase (mpq_denref(q), 10)
+ : mpz_sizeinbase (mpq_numref(q), ABS(base)) +
+ mpz_sizeinbase (mpq_denref(q), ABS(base))));
if (str_alloc != 0)
__GMP_REALLOCATE_FUNC_MAYBE_TYPE (str, str_alloc, len+1, char);