<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- This manual describes how to install and use the GNU multiple precision arithmetic library, version 6.1.0. Copyright 1991, 1993-2015 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 version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being "A GNU Manual", and with the Back-Cover Texts being "You have freedom to copy and modify this GNU Manual, like GNU software". A copy of the license is included in GNU Free Documentation License. --> <!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ --> <head> <title>Rational Conversions (GNU MP 6.1.0)</title> <meta name="description" content="How to install and use the GNU multiple precision arithmetic library, version 6.1.0."> <meta name="keywords" content="Rational Conversions (GNU MP 6.1.0)"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="Rational-Number-Functions.html#Rational-Number-Functions" rel="up" title="Rational Number Functions"> <link href="Rational-Arithmetic.html#Rational-Arithmetic" rel="next" title="Rational Arithmetic"> <link href="Initializing-Rationals.html#Initializing-Rationals" rel="prev" title="Initializing Rationals"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> </head> <body lang="en"> <a name="Rational-Conversions"></a> <div class="header"> <p> Next: <a href="Rational-Arithmetic.html#Rational-Arithmetic" accesskey="n" rel="next">Rational Arithmetic</a>, Previous: <a href="Initializing-Rationals.html#Initializing-Rationals" accesskey="p" rel="prev">Initializing Rationals</a>, Up: <a href="Rational-Number-Functions.html#Rational-Number-Functions" accesskey="u" rel="up">Rational Number Functions</a> [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Conversion-Functions-1"></a> <h3 class="section">6.2 Conversion Functions</h3> <a name="index-Rational-conversion-functions"></a> <a name="index-Conversion-functions-1"></a> <dl> <dt><a name="index-mpq_005fget_005fd"></a>Function: <em>double</em> <strong>mpq_get_d</strong> <em>(const mpq_t <var>op</var>)</em></dt> <dd><p>Convert <var>op</var> to a <code>double</code>, truncating if necessary (i.e. rounding towards zero). </p> <p>If the exponent from the conversion is too big or too small to fit a <code>double</code> then the result is system dependent. For too big an infinity is returned when available. For too small <em>0.0</em> is normally returned. Hardware overflow, underflow and denorm traps may or may not occur. </p></dd></dl> <dl> <dt><a name="index-mpq_005fset_005fd"></a>Function: <em>void</em> <strong>mpq_set_d</strong> <em>(mpq_t <var>rop</var>, double <var>op</var>)</em></dt> <dt><a name="index-mpq_005fset_005ff"></a>Function: <em>void</em> <strong>mpq_set_f</strong> <em>(mpq_t <var>rop</var>, const mpf_t <var>op</var>)</em></dt> <dd><p>Set <var>rop</var> to the value of <var>op</var>. There is no rounding, this conversion is exact. </p></dd></dl> <dl> <dt><a name="index-mpq_005fget_005fstr"></a>Function: <em>char *</em> <strong>mpq_get_str</strong> <em>(char *<var>str</var>, int <var>base</var>, const mpq_t <var>op</var>)</em></dt> <dd><p>Convert <var>op</var> to a string of digits in base <var>base</var>. The base may vary from 2 to 36. The string will be of the form ‘<samp>num/den</samp>’, or if the denominator is 1 then just ‘<samp>num</samp>’. </p> <p>If <var>str</var> is <code>NULL</code>, the result string is allocated using the current allocation function (see <a href="Custom-Allocation.html#Custom-Allocation">Custom Allocation</a>). The block will be <code>strlen(str)+1</code> bytes, that being exactly enough for the string and null-terminator. </p> <p>If <var>str</var> is not <code>NULL</code>, it should point to a block of storage large enough for the result, that being </p> <div class="example"> <pre class="example">mpz_sizeinbase (mpq_numref(<var>op</var>), <var>base</var>) + mpz_sizeinbase (mpq_denref(<var>op</var>), <var>base</var>) + 3 </pre></div> <p>The three extra bytes are for a possible minus sign, possible slash, and the null-terminator. </p> <p>A pointer to the result string is returned, being either the allocated block, or the given <var>str</var>. </p></dd></dl> </body> </html>