github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpf/out_str.c (about)

     1  /* mpf_out_str (stream, base, n_digits, op) -- Print N_DIGITS digits from
     2     the float OP to STREAM in base BASE.  Return the number of characters
     3     written, or 0 if an error occurred.
     4  
     5  Copyright 1996, 1997, 2001, 2002, 2005, 2011 Free Software Foundation, Inc.
     6  
     7  This file is part of the GNU MP Library.
     8  
     9  The GNU MP Library is free software; you can redistribute it and/or modify
    10  it under the terms of either:
    11  
    12    * the GNU Lesser General Public License as published by the Free
    13      Software Foundation; either version 3 of the License, or (at your
    14      option) any later version.
    15  
    16  or
    17  
    18    * the GNU General Public License as published by the Free Software
    19      Foundation; either version 2 of the License, or (at your option) any
    20      later version.
    21  
    22  or both in parallel, as here.
    23  
    24  The GNU MP Library is distributed in the hope that it will be useful, but
    25  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    26  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    27  for more details.
    28  
    29  You should have received copies of the GNU General Public License and the
    30  GNU Lesser General Public License along with the GNU MP Library.  If not,
    31  see https://www.gnu.org/licenses/.  */
    32  
    33  #define _GNU_SOURCE    /* for DECIMAL_POINT in langinfo.h */
    34  
    35  #include "config.h"
    36  
    37  #include <stdio.h>
    38  #include <string.h>
    39  
    40  #if HAVE_LANGINFO_H
    41  #include <langinfo.h>  /* for nl_langinfo */
    42  #endif
    43  
    44  #if HAVE_LOCALE_H
    45  #include <locale.h>    /* for localeconv */
    46  #endif
    47  
    48  #include "gmp.h"
    49  #include "gmp-impl.h"
    50  #include "longlong.h"
    51  
    52  
    53  size_t
    54  mpf_out_str (FILE *stream, int base, size_t n_digits, mpf_srcptr op)
    55  {
    56    char *str;
    57    mp_exp_t exp;
    58    size_t written;
    59    TMP_DECL;
    60  
    61    TMP_MARK;
    62  
    63    if (base == 0)
    64      base = 10;
    65    if (n_digits == 0)
    66      MPF_SIGNIFICANT_DIGITS (n_digits, base, op->_mp_prec);
    67  
    68    if (stream == 0)
    69      stream = stdout;
    70  
    71    /* Consider these changes:
    72       * Don't allocate memory here for huge n_digits; pass NULL to mpf_get_str.
    73       * Make mpf_get_str allocate extra space when passed NULL, to avoid
    74         allocating two huge string buffers.
    75       * Implement more/other allocation reductions tricks.  */
    76  
    77    str = (char *) TMP_ALLOC (n_digits + 2); /* extra for minus sign and \0 */
    78  
    79    mpf_get_str (str, &exp, base, n_digits, op);
    80    n_digits = strlen (str);
    81  
    82    written = 0;
    83  
    84    /* Write sign */
    85    if (str[0] == '-')
    86      {
    87        str++;
    88        fputc ('-', stream);
    89        written = 1;
    90        n_digits--;
    91      }
    92  
    93    {
    94      const char  *point = GMP_DECIMAL_POINT;
    95      size_t      pointlen = strlen (point);
    96      putc ('0', stream);
    97      fwrite (point, 1, pointlen, stream);
    98      written += pointlen + 1;
    99    }
   100  
   101    /* Write mantissa */
   102    {
   103      size_t fwret;
   104      fwret = fwrite (str, 1, n_digits, stream);
   105      written += fwret;
   106    }
   107  
   108    /* Write exponent */
   109    {
   110      int fpret;
   111      fpret = fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), exp);
   112      written += fpret;
   113    }
   114  
   115    TMP_FREE;
   116    return ferror (stream) ? 0 : written;
   117  }