aboutsummaryrefslogtreecommitdiff
path: root/gmp-6.3.0/mpq/get_str.c
diff options
context:
space:
mode:
authorDuncan Wilkie <antigravityd@gmail.com>2023-11-18 06:11:09 -0600
committerDuncan Wilkie <antigravityd@gmail.com>2023-11-18 06:11:09 -0600
commit11da511c784eca003deb90c23570f0873954e0de (patch)
treee14fdd3d5d6345956d67e79ae771d0633d28362b /gmp-6.3.0/mpq/get_str.c
Initial commit.
Diffstat (limited to 'gmp-6.3.0/mpq/get_str.c')
-rw-r--r--gmp-6.3.0/mpq/get_str.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/gmp-6.3.0/mpq/get_str.c b/gmp-6.3.0/mpq/get_str.c
new file mode 100644
index 0000000..d0cfb58
--- /dev/null
+++ b/gmp-6.3.0/mpq/get_str.c
@@ -0,0 +1,80 @@
+/* mpq_get_str -- mpq to string conversion.
+
+Copyright 2001, 2002, 2006, 2011, 2018 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+The GNU MP Library is free software; you can redistribute it and/or modify
+it under the terms of either:
+
+ * the GNU Lesser General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at your
+ option) any later version.
+
+or
+
+ * the GNU General Public License as published by the Free Software
+ Foundation; either version 2 of the License, or (at your option) any
+ later version.
+
+or both in parallel, as here.
+
+The GNU MP Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received copies of the GNU General Public License and the
+GNU Lesser General Public License along with the GNU MP Library. If not,
+see https://www.gnu.org/licenses/. */
+
+#include <stdio.h>
+#include <string.h>
+#include "gmp-impl.h"
+#include "longlong.h"
+
+char *
+mpq_get_str (char *str, int base, mpq_srcptr q)
+{
+ size_t str_alloc, len;
+
+ if (base > 62 || base < -36)
+ return NULL;
+
+ str_alloc = 0;
+ if (str == NULL)
+ {
+ /* 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;
+
+ str = __GMP_ALLOCATE_FUNC_TYPE (str_alloc, char);
+ }
+
+ mpz_get_str (str, base, mpq_numref(q));
+ len = strlen (str);
+ if (! MPZ_EQUAL_1_P (mpq_denref (q)))
+ {
+ str[len++] = '/';
+ mpz_get_str (str+len, base, mpq_denref(q));
+ len += strlen (str+len);
+ }
+
+ ASSERT (len == strlen(str));
+ ASSERT (str_alloc == 0 || len+1 <= str_alloc);
+ 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);
+
+ return str;
+}