Blame mpf/out_str.c

Packit 5c3484
/* mpf_out_str (stream, base, n_digits, op) -- Print N_DIGITS digits from
Packit 5c3484
   the float OP to STREAM in base BASE.  Return the number of characters
Packit 5c3484
   written, or 0 if an error occurred.
Packit 5c3484
Packit 5c3484
Copyright 1996, 1997, 2001, 2002, 2005, 2011 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#define _GNU_SOURCE    /* for DECIMAL_POINT in langinfo.h */
Packit 5c3484
Packit 5c3484
#include "config.h"
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <string.h>
Packit 5c3484
Packit 5c3484
#if HAVE_LANGINFO_H
Packit 5c3484
#include <langinfo.h>  /* for nl_langinfo */
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#if HAVE_LOCALE_H
Packit 5c3484
#include <locale.h>    /* for localeconv */
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "longlong.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
size_t
Packit 5c3484
mpf_out_str (FILE *stream, int base, size_t n_digits, mpf_srcptr op)
Packit 5c3484
{
Packit 5c3484
  char *str;
Packit 5c3484
  mp_exp_t exp;
Packit 5c3484
  size_t written;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
Packit 5c3484
  TMP_MARK;
Packit 5c3484
Packit 5c3484
  if (base == 0)
Packit 5c3484
    base = 10;
Packit 5c3484
  if (n_digits == 0)
Packit 5c3484
    MPF_SIGNIFICANT_DIGITS (n_digits, base, op->_mp_prec);
Packit 5c3484
Packit 5c3484
  if (stream == 0)
Packit 5c3484
    stream = stdout;
Packit 5c3484
Packit 5c3484
  /* Consider these changes:
Packit 5c3484
     * Don't allocate memory here for huge n_digits; pass NULL to mpf_get_str.
Packit 5c3484
     * Make mpf_get_str allocate extra space when passed NULL, to avoid
Packit 5c3484
       allocating two huge string buffers.
Packit 5c3484
     * Implement more/other allocation reductions tricks.  */
Packit 5c3484
Packit 5c3484
  str = (char *) TMP_ALLOC (n_digits + 2); /* extra for minus sign and \0 */
Packit 5c3484
Packit 5c3484
  mpf_get_str (str, &exp, base, n_digits, op);
Packit 5c3484
  n_digits = strlen (str);
Packit 5c3484
Packit 5c3484
  written = 0;
Packit 5c3484
Packit 5c3484
  /* Write sign */
Packit 5c3484
  if (str[0] == '-')
Packit 5c3484
    {
Packit 5c3484
      str++;
Packit 5c3484
      fputc ('-', stream);
Packit 5c3484
      written = 1;
Packit 5c3484
      n_digits--;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  {
Packit 5c3484
    const char  *point = GMP_DECIMAL_POINT;
Packit 5c3484
    size_t      pointlen = strlen (point);
Packit 5c3484
    putc ('0', stream);
Packit 5c3484
    fwrite (point, 1, pointlen, stream);
Packit 5c3484
    written += pointlen + 1;
Packit 5c3484
  }
Packit 5c3484
Packit 5c3484
  /* Write mantissa */
Packit 5c3484
  {
Packit 5c3484
    size_t fwret;
Packit 5c3484
    fwret = fwrite (str, 1, n_digits, stream);
Packit 5c3484
    written += fwret;
Packit 5c3484
  }
Packit 5c3484
Packit 5c3484
  /* Write exponent */
Packit 5c3484
  {
Packit 5c3484
    int fpret;
Packit 5c3484
    fpret = fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), exp);
Packit 5c3484
    written += fpret;
Packit 5c3484
  }
Packit 5c3484
Packit 5c3484
  TMP_FREE;
Packit 5c3484
  return ferror (stream) ? 0 : written;
Packit 5c3484
}