[Gmp-commit] /var/hg/gmp: mpq_*_str: document and support the correct base range
mercurial at gmplib.org
mercurial at gmplib.org
Fri May 4 19:49:38 UTC 2018
details: /var/hg/gmp/rev/85eda8a9f6e2
changeset: 17618:85eda8a9f6e2
user: Marco Bodrato <bodrato at mail.dm.unipi.it>
date: Fri May 04 21:43:34 2018 +0200
description:
mpq_*_str: document and support the correct base range
diffstat:
ChangeLog | 5 +++++
doc/gmp.texi | 26 +++++++++++++++++++-------
mpq/get_str.c | 13 +++++++++----
3 files changed, 33 insertions(+), 11 deletions(-)
diffs (108 lines):
diff -r 2bf7fa45600d -r 85eda8a9f6e2 ChangeLog
--- a/ChangeLog Mon Apr 30 01:48:28 2018 +0200
+++ b/ChangeLog Fri May 04 21:43:34 2018 +0200
@@ -1,3 +1,8 @@
+2018-05-04 Marco Bodrato <bodrato at 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 at lysator.liu.se>
* mpn/generic/div_q.c (mpn_div_q): Replace dead code with ASSERT.
diff -r 2bf7fa45600d -r 85eda8a9f6e2 doc/gmp.texi
--- a/doc/gmp.texi Mon Apr 30 01:48:28 2018 +0200
+++ b/doc/gmp.texi Fri May 04 21:43:34 2018 +0200
@@ -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 @@
@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
+ at minus{}2.. at 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 @@
@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
- at var{base}. The base may vary from 2 to 36. Output is in the form
+ at var{base}. The base argument may vary from 2 to 62 or from @minus{}2 to
+ at 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
+ at minus{}2.. at 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 @@
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 -r 2bf7fa45600d -r 85eda8a9f6e2 mpq/get_str.c
--- a/mpq/get_str.c Mon Apr 30 01:48:28 2018 +0200
+++ b/mpq/get_str.c Fri May 04 21:43:34 2018 +0200
@@ -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 @@
/* 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 @@
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);
More information about the gmp-commit
mailing list