/* mpfr_get_str -- output a floating-point number to a string
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
Contributed by the Arenaire and Cacao projects, INRIA.
Contributed by Alain Delplanque and Paul Zimmermann.
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 2.1 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.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include <string.h> /* For strlen */
#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"
static int mpfr_get_str_aux (char *const, mp_exp_t *const, mp_limb_t *const,
mp_size_t, mp_exp_t, long, int, size_t, mp_rnd_t);
/* The implicit \0 is useless, but we do not write num_to_text[36]
otherwise g++ complains. */
static const char num_to_text[] = "0123456789abcdefghijklmnopqrstuvwxyz";
/* 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 of an real Y, with |r*2^f-Y| <= 2^(e+f).
Returns if possible in the string s the mantissa corresponding to
the integer nearest to Y, within the direction rnd, and returns the
exponent in exp.
n is the number of limbs of r.
e represents the maximal error in the approximation of Y
(e < 0 iff the approximation is exact, i.e. r*2^f = Y).
b is the wanted base (2 <= b <= 36).
m is the number of wanted digits in the mantissa.
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, mp_exp_t *const exp, mp_limb_t *const r,
mp_size_t n, mp_exp_t f, long e, int b, size_t m,
mp_rnd_t rnd)
{
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 */
mp_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 * BITS_PER_MP_LIMB));
MPFR_TMP_MARK(marker);
/* 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*BITS_PER_MP_LIMB
we must have e + f == f + n*BITS_PER_MP_LIMB - err
err = n*BITS_PER_MP_LIMB - e
R contains exactly -f bits after the integer point:
to determine the nearest integer, we thus need a precision of
n * BITS_PER_MP_LIMB + f */
if (exact || mpfr_can_round_raw (r, n, (mp_size_t) 1,
n * BITS_PER_MP_LIMB - e, GMP_RNDN, rnd, n * BITS_PER_MP_LIMB + f))
{
/* compute the nearest integer to R */
/* bit of weight 0 in R has position j0 in limb r[i0] */
i0 = (-f) / BITS_PER_MP_LIMB;
j0 = (-f) % BITS_PER_MP_LIMB;
ret = mpfr_round_raw (r + i0, r, n * BITS_PER_MP_LIMB, 0,
n * BITS_PER_MP_LIMB + f, rnd, &dir);
MPFR_ASSERTD(dir != MPFR_ROUND_FAILED);
/* warning: mpfr_round_raw_generic returns MPFR_EVEN_INEX (2) or
-MPFR_EVEN_INEX (-2) in case of even rounding */
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 */
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 == GMP_RNDN)
{
if (2 * str1[size_s1 - 1] == b)
{
if (dir == 0 && exact) /* exact: even rounding */
{
rnd1 = ((str1[size_s1-2] & 1) == 0)
? GMP_RNDD : GMP_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 = GMP_RNDD;
else
rnd1 = GMP_RNDU;
}
/* now rnd1 is either GMP_RNDD or GMP_RNDZ -> truncate
or GMP_RDNU -> round towards infinity */
/* round away from zero */
if (rnd1 == GMP_RNDU)
{
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_s1 - 2;
while (str1[i] == b - 1)
{
MPFR_ASSERTD(i > 0);
str1[i--] = 0;
}
str1[i]++;
}
dir = 1;
没有合适的资源?快使用搜索试试~ 我知道了~
mpfr-2.4.1.zip
共417个文件
c:364个
h:11个
m4:8个
需积分: 1 0 下载量 56 浏览量
2024-01-29
23:14:51
上传
评论
收藏 1.64MB ZIP 举报
温馨提示
MPFR(Multiple Precision Floating-Point Reliable)以下是对这些资源的简介: MPFR 库简介:MPFR 是一个用于高精度浮点数计算的自由库,它提供了遵循 IEEE 754 标准的浮点数运算功能。这个库尤其适用于科学计算、加密算法和任何需要极高数值精度的场景。 重要性:MPFR 库因其高精度和可靠性在科学和工程计算领域中非常重要。它提供了一种标准和一致的方法来处理复杂的数值运算,确保了计算结果的准确性。
资源推荐
资源详情
资源评论
收起资源包目录
mpfr-2.4.1.zip (417个子文件)
ansi2knr.1 1KB
Makefile.am 4KB
Makefile.am 2KB
AUTHORS 789B
BUGS 3KB
get_str.c 68KB
vasprintf.c 65KB
tadd.c 44KB
tstrtofr.c 42KB
tget_str.c 41KB
tpow.c 37KB
texp.c 33KB
tdiv.c 28KB
sub1sp.c 28KB
strtofr.c 27KB
tsprintf.c 27KB
tset_str.c 26KB
tpow_all.c 25KB
lngamma.c 25KB
tests.c 23KB
div.c 23KB
pow.c 22KB
li2.c 22KB
rec_sqrt.c 21KB
tsqrt.c 21KB
tmul.c 21KB
ansi2knr.c 20KB
tgmpop.c 19KB
tfms.c 19KB
tfma.c 19KB
reuse.c 18KB
mul.c 17KB
tuneup.c 17KB
tsub1sp.c 17KB
sub1.c 17KB
gamma.c 15KB
terf.c 15KB
add1.c 15KB
tsub.c 15KB
zeta.c 15KB
get_d64.c 14KB
tgeneric.c 14KB
yn.c 14KB
exp_2.c 14KB
tlog.c 13KB
tatan.c 13KB
rint.c 13KB
atan.c 13KB
pow_z.c 13KB
tzeta.c 13KB
add1sp.c 13KB
tgamma.c 12KB
tlgamma.c 12KB
mpfr-gmp.c 12KB
trint.c 12KB
tset_si.c 11KB
exp3.c 11KB
tfprintf.c 11KB
fms.c 11KB
eint.c 11KB
fma.c 11KB
jyn_asympt.c 11KB
texceptions.c 10KB
tcmp2.c 10KB
tprintf.c 10KB
tpow_z.c 10KB
tcos.c 10KB
pow_si.c 10KB
tsin_cos.c 10KB
texp2.c 10KB
tsin.c 10KB
erfc.c 10KB
atan2.c 10KB
set_ld.c 9KB
troot.c 9KB
tjn.c 9KB
set_d64.c 9KB
sum.c 9KB
round_near_x.c 9KB
cos.c 9KB
thypot.c 9KB
sqrt.c 8KB
gmp_op.c 8KB
erf.c 8KB
texp10.c 8KB
tui_sub.c 8KB
tremquo.c 8KB
round_raw_generic.c 8KB
get_d.c 8KB
tfactorial.c 8KB
jn.c 8KB
tmul_ui.c 8KB
div_ui.c 8KB
rem1.c 7KB
sin_cos.c 7KB
tmul_2exp.c 7KB
tsum.c 7KB
thyperbolic.c 7KB
trandom.c 7KB
exceptions.c 7KB
共 417 条
- 1
- 2
- 3
- 4
- 5
资源评论
程序员Chino的日记
- 粉丝: 3671
- 资源: 5万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功