本文整理汇总了C++中MPFR_SET_NAN函数的典型用法代码示例。如果您正苦于以下问题:C++ MPFR_SET_NAN函数的具体用法?C++ MPFR_SET_NAN怎么用?C++ MPFR_SET_NAN使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MPFR_SET_NAN函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mpfr_ui_div
int
mpfr_ui_div (mpfr_ptr y, unsigned long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
{
mpfr_t uu;
mp_limb_t up[1];
unsigned long cnt;
MPFR_LOG_FUNC
(("u=%lu x[%Pu]=%.*Rg rnd=%d",
u, mpfr_get_prec(x), mpfr_log_prec, x, rnd_mode),
("y[%Pu]=%.*Rg", mpfr_get_prec(y), mpfr_log_prec, y));
if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
{
if (MPFR_IS_NAN(x))
{
MPFR_SET_NAN(y);
MPFR_RET_NAN;
}
else if (MPFR_IS_INF(x)) /* u/Inf = 0 */
{
MPFR_SET_ZERO(y);
MPFR_SET_SAME_SIGN(y,x);
MPFR_RET(0);
}
else /* u / 0 */
{
MPFR_ASSERTD(MPFR_IS_ZERO(x));
if (u)
{
/* u > 0, so y = sign(x) * Inf */
MPFR_SET_SAME_SIGN(y, x);
MPFR_SET_INF(y);
mpfr_set_divby0 ();
MPFR_RET(0);
}
else
{
/* 0 / 0 */
MPFR_SET_NAN(y);
MPFR_RET_NAN;
}
}
}
else if (MPFR_LIKELY(u != 0))
{
MPFR_TMP_INIT1(up, uu, GMP_NUMB_BITS);
MPFR_ASSERTN(u == (mp_limb_t) u);
count_leading_zeros(cnt, (mp_limb_t) u);
up[0] = (mp_limb_t) u << cnt;
MPFR_SET_EXP (uu, GMP_NUMB_BITS - cnt);
return mpfr_div (y, uu, x, rnd_mode);
}
else /* u = 0, and x != 0 */
{
MPFR_SET_ZERO(y); /* if u=0, then set y to 0 */
MPFR_SET_SAME_SIGN(y, x); /* u considered as +0: sign(+0/x) = sign(x) */
MPFR_RET(0);
}
}
示例2: mpfr_ui_div
int
mpfr_ui_div (mpfr_ptr y, unsigned long int u, mpfr_srcptr x, mp_rnd_t rnd_mode)
{
mpfr_t uu;
mp_limb_t up[1];
unsigned long cnt;
if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
{
if (MPFR_IS_NAN(x))
{
MPFR_SET_NAN(y);
MPFR_RET_NAN;
}
else if (MPFR_IS_INF(x)) /* u/Inf = 0 */
{
MPFR_SET_ZERO(y);
MPFR_SET_SAME_SIGN(y,x);
MPFR_RET(0);
}
else /* u / 0 */
{
MPFR_ASSERTD(MPFR_IS_ZERO(x));
if (u)
{
/* u > 0, so y = sign(x) * Inf */
MPFR_SET_SAME_SIGN(y, x);
MPFR_SET_INF(y);
MPFR_RET(0);
}
else
{
/* 0 / 0 */
MPFR_SET_NAN(y);
MPFR_RET_NAN;
}
}
}
else if (MPFR_LIKELY(u != 0))
{
MPFR_TMP_INIT1(up, uu, BITS_PER_MP_LIMB);
MPFR_ASSERTN(u == (mp_limb_t) u);
count_leading_zeros(cnt, (mp_limb_t) u);
up[0] = (mp_limb_t) u << cnt;
MPFR_SET_EXP (uu, BITS_PER_MP_LIMB - cnt);
return mpfr_div (y, uu, x, rnd_mode);
}
else /* u = 0, and x != 0 */
{
MPFR_SET_ZERO(y); /* if u=0, then set y to 0 */
MPFR_SET_SAME_SIGN(y, x); /* u considered as +0: sign(+0/x) = sign(x) */
MPFR_RET(0);
}
}
示例3: mpfr_root
int
mpfr_root (mpfr_ptr y, mpfr_srcptr x, unsigned long k, mpfr_rnd_t rnd_mode)
{
mpz_t m;
mpfr_exp_t e, r, sh;
mpfr_prec_t n, size_m, tmp;
int inexact, negative;
MPFR_SAVE_EXPO_DECL (expo);
MPFR_LOG_FUNC
(("x[%Pu]=%.*Rg k=%lu rnd=%d",
mpfr_get_prec (x), mpfr_log_prec, x, k, rnd_mode),
("y[%Pu]=%.*Rg inexact=%d",
mpfr_get_prec (y), mpfr_log_prec, y, inexact));
if (MPFR_UNLIKELY (k <= 1))
{
if (k < 1) /* k==0 => y=x^(1/0)=x^(+Inf) */
#if 0
/* For 0 <= x < 1 => +0.
For x = 1 => 1.
For x > 1, => +Inf.
For x < 0 => NaN.
*/
{
if (MPFR_IS_NEG (x) && !MPFR_IS_ZERO (x))
{
MPFR_SET_NAN (y);
MPFR_RET_NAN;
}
inexact = mpfr_cmp (x, __gmpfr_one);
if (inexact == 0)
return mpfr_set_ui (y, 1, rnd_mode); /* 1 may be Out of Range */
else if (inexact < 0)
return mpfr_set_ui (y, 0, rnd_mode); /* 0+ */
else
{
mpfr_set_inf (y, 1);
return 0;
}
}
#endif
{
MPFR_SET_NAN (y);
MPFR_RET_NAN;
}
else /* y =x^(1/1)=x */
return mpfr_set (y, x, rnd_mode);
}
示例4: mpfr_reldiff
/* reldiff(b, c) = abs(b-c)/b */
void
mpfr_reldiff (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
{
mpfr_t b_copy;
if (MPFR_ARE_SINGULAR (b, c))
{
if (MPFR_IS_NAN(b) || MPFR_IS_NAN(c))
{
MPFR_SET_NAN(a);
return;
}
else if (MPFR_IS_INF(b))
{
if (MPFR_IS_INF (c) && (MPFR_SIGN (c) == MPFR_SIGN (b)))
MPFR_SET_ZERO(a);
else
MPFR_SET_NAN(a);
return;
}
else if (MPFR_IS_INF(c))
{
MPFR_SET_SAME_SIGN (a, b);
MPFR_SET_INF (a);
return;
}
else if (MPFR_IS_ZERO(b)) /* reldiff = abs(c)/c = sign(c) */
{
mpfr_set_si (a, MPFR_INT_SIGN (c), rnd_mode);
return;
}
/* Fall through */
}
if (a == b)
{
mpfr_init2 (b_copy, MPFR_PREC(b));
mpfr_set (b_copy, b, MPFR_RNDN);
}
mpfr_sub (a, b, c, rnd_mode);
mpfr_abs (a, a, rnd_mode); /* for compatibility with MPF */
mpfr_div (a, a, (a == b) ? b_copy : b, rnd_mode);
if (a == b)
mpfr_clear (b_copy);
}
示例5: mpfr_frexp
int
mpfr_frexp (mpfr_exp_t *exp, mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd)
{
int inex;
if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
{
if (MPFR_IS_NAN(x))
{
MPFR_SET_NAN(y);
MPFR_RET_NAN; /* exp is unspecified */
}
else if (MPFR_IS_INF(x))
{
MPFR_SET_INF(y);
MPFR_SET_SAME_SIGN(y,x);
MPFR_RET(0); /* exp is unspecified */
}
else
{
MPFR_SET_ZERO(y);
MPFR_SET_SAME_SIGN(y,x);
*exp = 0;
MPFR_RET(0);
}
}
inex = mpfr_set (y, x, rnd);
*exp = MPFR_GET_EXP (y);
MPFR_SET_EXP (y, 0);
return mpfr_check_range (y, inex, rnd);
}
示例6: mpfr_urandomb
int
mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
{
mp_ptr rp;
mp_prec_t nbits;
mp_size_t nlimbs;
mp_size_t k; /* number of high zero limbs */
mp_exp_t exp;
int cnt;
MPFR_CLEAR_FLAGS (rop);
rp = MPFR_MANT (rop);
nbits = MPFR_PREC (rop);
nlimbs = MPFR_LIMB_SIZE (rop);
MPFR_SET_POS (rop);
/* Uniform non-normalized significand */
_gmp_rand (rp, rstate, nlimbs * BITS_PER_MP_LIMB);
/* If nbits isn't a multiple of BITS_PER_MP_LIMB, mask the low bits */
cnt = nlimbs * BITS_PER_MP_LIMB - nbits;
if (MPFR_LIKELY (cnt != 0))
rp[0] &= ~MPFR_LIMB_MASK (cnt);
/* Count the null significant limbs and remaining limbs */
exp = 0;
k = 0;
while (nlimbs != 0 && rp[nlimbs - 1] == 0)
{
k ++;
nlimbs --;
exp -= BITS_PER_MP_LIMB;
}
if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
{
count_leading_zeros (cnt, rp[nlimbs - 1]);
/* Normalization */
if (mpfr_set_exp (rop, exp - cnt))
{
/* If the exponent is not in the current exponent range, we
choose to return a NaN as this is probably a user error.
Indeed this can happen only if the exponent range has been
reduced to a very small interval and/or the precision is
huge (very unlikely). */
MPFR_SET_NAN (rop);
__gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
return 1;
}
if (cnt != 0)
mpn_lshift (rp + k, rp, nlimbs, cnt);
if (k != 0)
MPN_ZERO (rp, k);
}
else
MPFR_SET_ZERO (rop);
return 0;
}
示例7: trunc
/* compute remainder as in definition:
r = x - n * y, where n = trunc(x/y).
warning: may change flags. */
static int
slow_fmod (mpfr_ptr r, mpfr_srcptr x, mpfr_srcptr y, mpfr_rnd_t rnd)
{
mpfr_t q;
int inexact;
if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x) || MPFR_IS_SINGULAR (y)))
{
if (MPFR_IS_NAN (x) || MPFR_IS_NAN (y) || MPFR_IS_INF (x)
|| MPFR_IS_ZERO (y))
{
MPFR_SET_NAN (r);
MPFR_RET_NAN;
}
else /* either y is Inf and x is 0 or non-special,
or x is 0 and y is non-special,
in both cases the quotient is zero. */
return mpfr_set (r, x, rnd);
}
/* regular cases */
/* if 2^(ex-1) <= |x| < 2^ex, and 2^(ey-1) <= |y| < 2^ey,
then |x/y| < 2^(ex-ey+1) */
mpfr_init2 (q,
MAX (MPFR_PREC_MIN, mpfr_get_exp (x) - mpfr_get_exp (y) + 1));
mpfr_div (q, x, y, MPFR_RNDZ);
mpfr_trunc (q, q); /* may change inexact flag */
mpfr_prec_round (q, mpfr_get_prec (q) + mpfr_get_prec (y), MPFR_RNDZ);
inexact = mpfr_mul (q, q, y, MPFR_RNDZ); /* exact */
inexact = mpfr_sub (r, x, q, rnd);
mpfr_clear (q);
return inexact;
}
示例8: mpfr_max
int
mpfr_max (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y, mp_rnd_t rnd_mode)
{
if (MPFR_IS_NAN(x) && MPFR_IS_NAN(y) )
{
MPFR_SET_NAN(z);
MPFR_RET_NAN;
}
MPFR_CLEAR_NAN(z);
if (MPFR_IS_NAN(x))
return mpfr_set(z, y, rnd_mode);
if (MPFR_IS_NAN(y))
return mpfr_set(z, x, rnd_mode);
if (MPFR_IS_FP(x) && MPFR_IS_ZERO(x) && MPFR_IS_FP(y) && MPFR_IS_ZERO(y))
{
if (MPFR_SIGN(x) < 0)
return mpfr_set(z, y, rnd_mode);
else
return mpfr_set(z, x, rnd_mode);
}
if (mpfr_cmp(x,y) <= 0)
return mpfr_set(z, y, rnd_mode);
else
return mpfr_set(z, x, rnd_mode);
}
示例9: mpfr_max
int
mpfr_max (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y, mpfr_rnd_t rnd_mode)
{
if (MPFR_ARE_SINGULAR(x,y))
{
if (MPFR_IS_NAN(x) && MPFR_IS_NAN(y) )
{
MPFR_SET_NAN(z);
MPFR_RET_NAN;
}
else if (MPFR_IS_NAN(x))
return mpfr_set(z, y, rnd_mode);
else if (MPFR_IS_NAN(y))
return mpfr_set(z, x, rnd_mode);
else if (MPFR_IS_ZERO(x) && MPFR_IS_ZERO(y))
{
if (MPFR_IS_NEG(x))
return mpfr_set(z, y, rnd_mode);
else
return mpfr_set(z, x, rnd_mode);
}
}
if (mpfr_cmp(x,y) <= 0)
return mpfr_set(z, y, rnd_mode);
else
return mpfr_set(z, x, rnd_mode);
}
示例10: mpfr_set_str_binary
void
mpfr_set_str_binary (mpfr_ptr x, const char *str)
{
int has_sign;
int res;
if (*str == 'N')
{
MPFR_SET_NAN(x);
__gmpfr_flags |= MPFR_FLAGS_NAN;
return;
}
has_sign = *str == '-' || *str == '+';
if (str[has_sign] == 'I')
{
MPFR_SET_INF(x);
if (*str == '-')
MPFR_SET_NEG(x);
else
MPFR_SET_POS(x);
return;
}
res = mpfr_strtofr (x, str, 0, 2, MPFR_RNDZ);
MPFR_ASSERTN (res == 0);
}
示例11: mpfr_set_q
/* set f to the rational q */
int
mpfr_set_q (mpfr_ptr f, mpq_srcptr q, mp_rnd_t rnd)
{
mpz_srcptr num, den;
mpfr_t n, d;
int inexact;
mp_prec_t prec;
MPFR_CLEAR_FLAGS (f);
num = mpq_numref (q);
if (mpz_cmp_ui (num, 0) == 0)
{
MPFR_SET_ZERO (f);
MPFR_SET_POS (f);
MPFR_RET (0);
}
den = mpq_denref (q);
mpfr_save_emin_emax ();
prec = mpz_sizeinbase (num, 2);
if (prec < MPFR_PREC_MIN)
prec = MPFR_PREC_MIN;
mpfr_init2 (n, prec);
if (mpfr_set_z (n, num, GMP_RNDZ)) /* result is exact unless overflow */
{
mpfr_clear (n);
mpfr_restore_emin_emax ();
MPFR_SET_NAN (f);
MPFR_RET_NAN;
}
prec = mpz_sizeinbase(den, 2);
if (prec < MPFR_PREC_MIN)
prec = MPFR_PREC_MIN;
mpfr_init2 (d, prec);
if (mpfr_set_z (d, den, GMP_RNDZ)) /* result is exact unless overflow */
{
mpfr_clear (d);
mpfr_clear (n);
mpfr_restore_emin_emax ();
MPFR_SET_NAN (f);
MPFR_RET_NAN;
}
inexact = mpfr_div (f, n, d, rnd);
mpfr_clear (n);
mpfr_clear (d);
MPFR_RESTORE_RET (inexact, f, rnd);
}
示例12: mpfr_urandomb
int
mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
{
mpfr_limb_ptr rp;
mpfr_prec_t nbits;
mp_size_t nlimbs;
mp_size_t k; /* number of high zero limbs */
mpfr_exp_t exp;
int cnt;
rp = MPFR_MANT (rop);
nbits = MPFR_PREC (rop);
nlimbs = MPFR_LIMB_SIZE (rop);
MPFR_SET_POS (rop);
cnt = nlimbs * GMP_NUMB_BITS - nbits;
/* Uniform non-normalized significand */
/* generate exactly nbits so that the random generator stays in the same
state, independent of the machine word size GMP_NUMB_BITS */
mpfr_rand_raw (rp, rstate, nbits);
if (MPFR_LIKELY (cnt != 0)) /* this will put the low bits to zero */
mpn_lshift (rp, rp, nlimbs, cnt);
/* Count the null significant limbs and remaining limbs */
exp = 0;
k = 0;
while (nlimbs != 0 && rp[nlimbs - 1] == 0)
{
k ++;
nlimbs --;
exp -= GMP_NUMB_BITS;
}
if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
{
count_leading_zeros (cnt, rp[nlimbs - 1]);
/* Normalization */
if (mpfr_set_exp (rop, exp - cnt))
{
/* If the exponent is not in the current exponent range, we
choose to return a NaN as this is probably a user error.
Indeed this can happen only if the exponent range has been
reduced to a very small interval and/or the precision is
huge (very unlikely). */
MPFR_SET_NAN (rop);
__gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
return 1;
}
if (cnt != 0)
mpn_lshift (rp + k, rp, nlimbs, cnt);
if (k != 0)
MPN_ZERO (rp, k);
}
else
MPFR_SET_ZERO (rop);
return 0;
}
示例13: check_nan
static void
check_nan (void)
{
mpfr_t d, q;
mpfr_init2 (d, 100L);
mpfr_init2 (q, 100L);
/* 1/+inf == 0 */
MPFR_CLEAR_FLAGS (d);
MPFR_SET_INF (d);
MPFR_SET_POS (d);
MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_number_p (q));
MPFR_ASSERTN (mpfr_sgn (q) == 0);
/* 1/-inf == -0 */
MPFR_CLEAR_FLAGS (d);
MPFR_SET_INF (d);
MPFR_SET_NEG (d);
MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_number_p (q));
MPFR_ASSERTN (mpfr_sgn (q) == 0);
/* 1/nan == nan */
MPFR_SET_NAN (d);
MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_nan_p (q));
/* 0/0 == nan */
mpfr_set_ui (d, 0L, GMP_RNDN);
MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_nan_p (q));
/* 1/+0 = +inf */
mpfr_set_ui (d, 0L, GMP_RNDN);
MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0);
/* 1/-0 = -inf */
mpfr_set_ui (d, 0L, GMP_RNDN);
mpfr_neg (d, d, GMP_RNDN);
MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0);
/* 0/1 = +0 */
mpfr_set_ui (d, 1L, GMP_RNDN);
MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_POS (q));
/* 0/-1 = -0 */
mpfr_set_si (d, -1, GMP_RNDN);
MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */
MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_NEG (q));
mpfr_clear (d);
mpfr_clear (q);
}
示例14: check_neg_special
/* Maybe better create its own test file ? */
static void
check_neg_special (void)
{
mpfr_t x;
mpfr_init (x);
MPFR_SET_NAN (x);
mpfr_clear_nanflag ();
mpfr_neg (x, x, MPFR_RNDN);
PRINT_ERROR_IF (!mpfr_nanflag_p (),
"ERROR: neg (NaN) doesn't set Nan flag.\n");
mpfr_clear (x);
}
示例15: set_special
static void
set_special (mpfr_ptr x, unsigned int select)
{
MPFR_ASSERTN (select < SPECIAL_MAX);
switch (select)
{
case 0:
MPFR_SET_NAN (x);
break;
case 1:
MPFR_SET_INF (x);
MPFR_SET_POS (x);
break;
case 2:
MPFR_SET_INF (x);
MPFR_SET_NEG (x);
break;
case 3:
MPFR_SET_ZERO (x);
MPFR_SET_POS (x);
break;
case 4:
MPFR_SET_ZERO (x);
MPFR_SET_NEG (x);
break;
case 5:
mpfr_set_str_binary (x, "1");
break;
case 6:
mpfr_set_str_binary (x, "-1");
break;
case 7:
mpfr_set_str_binary (x, "1e-1");
break;
case 8:
mpfr_set_str_binary (x, "1e+1");
break;
case 9:
mpfr_const_pi (x, MPFR_RNDN);
break;
case 10:
mpfr_const_pi (x, MPFR_RNDN);
MPFR_SET_EXP (x, MPFR_GET_EXP (x)-1);
break;
default:
mpfr_urandomb (x, RANDS);
if (randlimb () & 1)
mpfr_neg (x, x, MPFR_RNDN);
break;
}
}