/* mpfr_get_str -- output a floating-point number to a string
Copyright 1999-2022 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.
This file is part of the GNU MPFR Library.
The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of 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.
The GNU MPFR 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#define MPFR_NEED_LONGLONG_H
#define MPFR_NEED_INTMAX_H
#include "mpfr-impl.h"
static int mpfr_get_str_aux (char *const, mpfr_exp_t *const, mp_limb_t *const,
mp_size_t, mpfr_exp_t, long, int, size_t, mpfr_rnd_t);
/* The implicit \0 is useless, but we do not write num_to_text[62] otherwise
g++ complains. */
static const char num_to_text36[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static const char num_to_text62[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
/* copy most important limbs of {op, n2} in {rp, n1} */
/* if n1 > n2 put 0 in low limbs of {rp, n1} */
#define MPN_COPY2(rp, n1, op, n2) \
if ((n1) <= (n2)) \
{ \
MPN_COPY ((rp), (op) + (n2) - (n1), (n1)); \
} \
else \
{ \
MPN_COPY ((rp) + (n1) - (n2), (op), (n2)); \
MPN_ZERO ((rp), (n1) - (n2)); \
}
#define MPFR_ROUND_FAILED 3
/* Input: an approximation r*2^f to a real Y, with |r*2^f - Y| <= 2^(e+f).
If rounding is possible, returns:
- in s: a string representing the significand corresponding to
the integer nearest to Y, within the direction rnd;
- in exp: the exponent.
n is the number of limbs of r.
e represents the maximal error in the approximation to Y (see above),
(e < 0 means that the approximation is known to be exact, i.e.,
r*2^f = Y).
b is the wanted base (2 <= b <= 62 or -36 <= b <= -2).
m is the number of wanted digits in the significand.
rnd is the rounding mode.
It is assumed that b^(m-1) <= Y < b^(m+1), thus the returned value
satisfies b^(m-1) <= rnd(Y) < b^(m+1).
Rounding may fail for two reasons:
- the error is too large to determine the integer N nearest to Y
- either the number of digits of N in base b is too large (m+1),
N=2*N1+(b/2) and the rounding mode is to nearest. This can
only happen when b is even.
Return value:
- the direction of rounding (-1, 0, 1) if rounding is possible
- -MPFR_ROUND_FAILED if rounding not possible because m+1 digits
- MPFR_ROUND_FAILED otherwise (too large error)
*/
static int
mpfr_get_str_aux (char *const str, mpfr_exp_t *const exp, mp_limb_t *const r,
mp_size_t n, mpfr_exp_t f, long e, int b, size_t m,
mpfr_rnd_t rnd)
{
const char *num_to_text;
int b0 = b; /* initial base (might be negative) */
int dir; /* direction of the rounded result */
mp_limb_t ret = 0; /* possible carry in addition */
mp_size_t i0, j0; /* number of limbs and bits of Y */
unsigned char *str1; /* string of m+2 characters */
size_t size_s1; /* length of str1 */
mpfr_rnd_t rnd1;
size_t i;
int exact = (e < 0);
MPFR_TMP_DECL(marker);
/* if f > 0, then the maximal error 2^(e+f) is larger than 2 so we can't
determine the integer Y */
MPFR_ASSERTN(f <= 0);
/* if f is too small, then r*2^f is smaller than 1 */
MPFR_ASSERTN(f > (-n * GMP_NUMB_BITS));
MPFR_TMP_MARK(marker);
num_to_text = (2 <= b0 && b0 <= 36) ? num_to_text36 : num_to_text62;
b = (b0 > 0) ? b0 : -b0;
/* R = 2^f sum r[i]K^(i)
r[i] = (r_(i,k-1)...r_(i,0))_2
R = sum r(i,j)2^(j+ki+f)
the bits from R are referenced by pairs (i,j) */
/* check if is possible to round r with rnd mode
where |r*2^f - Y| <= 2^(e+f)
the exponent of R is: f + n*GMP_NUMB_BITS
we must have e + f == f + n*GMP_NUMB_BITS - err
err = n*GMP_NUMB_BITS - e
R contains exactly -f bits after the integer point:
to determine the nearest integer, we thus need a precision of
n * GMP_NUMB_BITS + f */
if (exact || mpfr_round_p (r, n, n * GMP_NUMB_BITS - e,
n * GMP_NUMB_BITS + f + (rnd == MPFR_RNDN)))
{
/* compute the nearest integer to R */
/* bit of weight 0 in R has position j0 in limb r[i0] */
i0 = (-f) / GMP_NUMB_BITS;
j0 = (-f) % GMP_NUMB_BITS;
ret = mpfr_round_raw (r + i0, r, n * GMP_NUMB_BITS, 0,
n * GMP_NUMB_BITS + f, rnd, &dir);
MPFR_ASSERTD(dir != MPFR_ROUND_FAILED);
if (ret) /* Y is a power of 2 */
{
if (j0)
r[n - 1] = MPFR_LIMB_HIGHBIT >> (j0 - 1);
else /* j0=0, necessarily i0 >= 1 otherwise f=0 and r is exact */
{
r[n - 1] = ret;
r[--i0] = 0; /* set to zero the new low limb */
}
}
else /* shift r to the right by (-f) bits (i0 already done) */
{
if (j0)
mpn_rshift (r + i0, r + i0, n - i0, j0);
}
/* now the rounded value Y is in {r+i0, n-i0} */
/* convert r+i0 into base b: we use b0 which might be in -36..-2 */
str1 = (unsigned char*) MPFR_TMP_ALLOC (m + 3); /* need one extra character for mpn_get_str */
size_s1 = mpn_get_str (str1, b, r + i0, n - i0);
/* round str1 */
MPFR_ASSERTN(size_s1 >= m);
*exp = size_s1 - m; /* number of superfluous characters */
/* if size_s1 = m + 2, necessarily we have b^(m+1) as result,
and the result will not change */
/* so we have to double-round only when size_s1 = m + 1 and
(i) the result is inexact
(ii) or the last digit is non-zero */
if ((size_s1 == m + 1) && ((dir != 0) || (str1[size_s1 - 1] != 0)))
{
/* rounding mode */
rnd1 = rnd;
/* round to nearest case */
if (rnd == MPFR_RNDN)
{
if (2 * str1[size_s1 - 1] == b)
{
if (dir == 0 && exact) /* exact: even rounding */
{
rnd1 = ((str1[size_s1 - 2] & 1) == 0)
? MPFR_RNDD : MPFR_RNDU;
}
else
{
/* otherwise we cannot round correctly: for example
if b=10, we might have a mantissa of
xxxxxxx5.00000000 which can be rounded to nearest
to 8 digits but not to 7 */
dir = -MPFR_ROUND_FAILED;
MPFR_ASSERTD(dir != MPFR_EVEN_INEX);
goto free_and_return;
}
}
else if (2 * str1[size_s1 - 1] < b)
rnd1 = MPFR_RNDD;
else
rnd1 = MPFR_RNDU;
}
/* now rnd1 is either
MPFR_RNDD or MPFR_RNDZ -> truncate, or
MPFR_RNDU or MPFR_RNDA -> round toward infinity */
/* round away from zero */
if (rnd1 == MPFR_RNDU || rnd1 == MPFR_RNDA)
{
if (str1[size_s1 - 1] != 0)
{
/* the carry cannot propagate to the whole string, since
Y = x*b^(m-g) < 2*b^m <= b^(m+1)-b
where x is the input float */
MPFR_ASSERTN(size_s1 >= 2);
i = size_s
没有合适的资源?快使用搜索试试~ 我知道了~
mpfr-4.1.1.tar.gz
需积分: 1 0 下载量 96 浏览量
2024-01-29
23:15:14
上传
评论
收藏 2.03MB GZ 举报
温馨提示
共541个文件
c:446个
h:30个
m4:8个
MPFR(Multiple Precision Floating-Point Reliable)以下是对这些资源的简介: MPFR 库简介:MPFR 是一个用于高精度浮点数计算的自由库,它提供了遵循 IEEE 754 标准的浮点数运算功能。这个库尤其适用于科学计算、加密算法和任何需要极高数值精度的场景。 重要性:MPFR 库因其高精度和可靠性在科学和工程计算领域中非常重要。它提供了一种标准和一致的方法来处理复杂的数值运算,确保了计算结果的准确性。
资源推荐
资源详情
资源评论
收起资源包目录
mpfr-4.1.1.tar.gz (541个子文件)
configure.ac 33KB
Makefile.am 8KB
Makefile.am 7KB
Makefile.am 4KB
Makefile.am 2KB
Makefile.am 1019B
Makefile.am 910B
ar-lib 6KB
AUTHORS 1KB
BUGS 3KB
get_str.c 120KB
vasprintf.c 80KB
sub1sp.c 68KB
tstrtofr.c 58KB
tdiv.c 55KB
tsprintf.c 54KB
sum.c 53KB
tpow.c 53KB
tadd.c 52KB
tget_str.c 52KB
tsub.c 51KB
div.c 50KB
tmul.c 44KB
tfma.c 43KB
tgmpop.c 40KB
tuneup.c 39KB
tsum.c 39KB
mul.c 38KB
tests.c 37KB
tsub1sp.c 36KB
texp.c 35KB
strtofr.c 35KB
add1sp.c 35KB
tsqrt.c 33KB
tgamma.c 33KB
lngamma.c 33KB
tgeneric.c 31KB
sub1.c 27KB
tset_str.c 26KB
tpow_all.c 25KB
pow.c 25KB
trint.c 25KB
zeta.c 25KB
tfmma.c 24KB
tset_si.c 24KB
rec_sqrt.c 23KB
sin_cos.c 22KB
sqrt.c 22KB
ai.c 22KB
reuse.c 21KB
tsin_cos.c 21KB
round_prec.c 21KB
sqr.c 21KB
li2.c 21KB
fpif.c 20KB
get_d64.c 20KB
turandom.c 19KB
tfms.c 19KB
atan.c 19KB
gamma.c 19KB
tset_ld.c 18KB
gamma_inc.c 18KB
tatan.c 18KB
tdiv_ui.c 18KB
terf.c 18KB
tget_set_d128.c 18KB
tget_set_d64.c 17KB
tprintf.c 17KB
set_d64.c 17KB
texceptions.c 17KB
troot.c 17KB
add1.c 17KB
set_d128.c 16KB
tzeta.c 16KB
tcan_round.c 16KB
gmp_op.c 16KB
exp_2.c 15KB
sub1sp1_extracted.c 15KB
tadd1sp.c 15KB
tset.c 15KB
tversion.c 15KB
tremquo.c 15KB
digamma.c 14KB
yn.c 14KB
rint.c 14KB
bidimensional_sample.c 14KB
taway.c 14KB
tlgamma.c 14KB
tstckintc.c 14KB
eint.c 14KB
tlog.c 14KB
tget_flt.c 13KB
random_deviate.c 13KB
tmul_2exp.c 13KB
pow_z.c 13KB
tfmod.c 13KB
set_ld.c 12KB
tfpif.c 12KB
jn.c 12KB
tsin.c 12KB
共 541 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
程序员Chino的日记
- 粉丝: 3036
- 资源: 4万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功