add arf_get_str; rewrite arf_fprintd

This commit is contained in:
fredrik 2022-01-14 14:22:16 +01:00
parent a00859dcd9
commit 915022dc31
4 changed files with 109 additions and 20 deletions

2
arf.h
View file

@ -775,6 +775,8 @@ void arf_ceil(arf_t z, const arf_t x);
void arf_debug(const arf_t x); void arf_debug(const arf_t x);
char * arf_get_str(const arf_t x, slong d);
void arf_fprint(FILE * file, const arf_t x); void arf_fprint(FILE * file, const arf_t x);
void arf_fprintd(FILE * file, const arf_t y, slong d); void arf_fprintd(FILE * file, const arf_t y, slong d);

View file

@ -10,7 +10,35 @@
(at your option) any later version. See <http://www.gnu.org/licenses/>. (at your option) any later version. See <http://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include "arf.h" #include "arf.h"
#include "arb.h"
char * arf_get_str(const arf_t x, slong d)
{
if (arf_is_special(x))
{
char * s = flint_malloc(5);
if (arf_is_zero(x))
strcpy(s, "0");
else if (arf_is_pos_inf(x))
strcpy(s, "+inf");
else if (arf_is_neg_inf(x))
strcpy(s, "-inf");
else
strcpy(s, "nan");
return s;
}
else
{
arb_t t;
*arb_midref(t) = *x;
mag_init(arb_radref(t)); /* no need to free */
return arb_get_str(t, FLINT_MAX(d, 1), ARB_STR_NO_RADIUS);
}
}
void void
arf_fprint(FILE * file, const arf_t x) arf_fprint(FILE * file, const arf_t x)
@ -45,20 +73,8 @@ arf_fprint(FILE * file, const arf_t x)
void void
arf_fprintd(FILE * file, const arf_t x, slong d) arf_fprintd(FILE * file, const arf_t x, slong d)
{ {
if (arf_is_finite(x) && (ARF_EXP(x) <= MPFR_EMIN_MIN + 1 || char * s = arf_get_str(x, d);
ARF_EXP(x) >= MPFR_EMAX_MAX - 1))
{
arf_fprint(file, x);
}
else
{
mpfr_t t;
mpfr_init2(t, d * 3.33 + 10);
mpfr_set_emin(MPFR_EMIN_MIN);
mpfr_set_emax(MPFR_EMAX_MAX);
arf_get_mpfr(t, x, MPFR_RNDN);
mpfr_fprintf(file, "%.*Rg", FLINT_MAX(d, 1), t);
mpfr_clear(t);
}
}
fprintf(file, "%s", s);
flint_free(s);
}

67
arf/test/t-get_str.c Normal file
View file

@ -0,0 +1,67 @@
/*
Copyright (C) 2021 Fredrik Johansson
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "arf.h"
#include "arb.h"
int main()
{
slong iter;
flint_rand_t state;
flint_printf("get_str....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++)
{
arf_t x;
arb_t y;
char *s1, * s2;
slong d;
arf_init(x);
arb_init(y);
arf_randtest_special(x, state, 2 + n_randint(state, 200), 2 + n_randint(state, 100));
arb_set_arf(y, x);
d = 1 + n_randint(state, 200);
s1 = arf_get_str(x, d);
s2 = arb_get_str(y, d, ARB_STR_NO_RADIUS);
if ((arf_is_pos_inf(x) && strcmp(s1, "+inf")) ||
(arf_is_neg_inf(x) && strcmp(s1, "-inf")) ||
(arf_is_nan(x) && strcmp(s1, "nan")) ||
(arf_is_finite(x) && strcmp(s1, s2)))
{
flint_printf("FAIL\n");
arf_print(x); flint_printf("\n");
arb_print(y); flint_printf("\n");
flint_printf("%s\n", s1);
flint_printf("%s\n", s2);
flint_abort();
}
flint_free(s1);
flint_free(s2);
arf_clear(x);
arb_clear(y);
}
flint_randclear(state);
flint_cleanup();
flint_printf("PASS\n");
return EXIT_SUCCESS;
}

View file

@ -502,8 +502,12 @@ Input and output
.. function:: void arf_printd(const arf_t x, slong d) .. function:: void arf_printd(const arf_t x, slong d)
Prints *x* as a decimal floating-point number, rounding to *d* digits. Prints *x* as a decimal floating-point number, rounding to *d* digits.
This function is currently implemented using MPFR, Rounding is faithful (at most 1 ulp error).
and does not support large exponents.
.. function:: char * arf_get_str(const arf_t x, slong d)
Returns *x* as a decimal floating-point number, rounding to *d* digits.
Rounding is faithful (at most 1 ulp error).
.. function:: void arf_fprint(FILE * file, const arf_t x) .. function:: void arf_fprint(FILE * file, const arf_t x)
@ -512,8 +516,8 @@ Input and output
.. function:: void arf_fprintd(FILE * file, const arf_t y, slong d) .. function:: void arf_fprintd(FILE * file, const arf_t y, slong d)
Prints *x* as a decimal floating-point number to the stream *file*, Prints *x* as a decimal floating-point number to the stream *file*,
rounding to *d* digits. This function is currently implemented using MPFR, rounding to *d* digits.
and does not support large exponents. Rounding is faithful (at most 1 ulp error).
.. function:: char * arf_dump_str(const arf_t x) .. function:: char * arf_dump_str(const arf_t x)