Ejemplo n.º 1
0
static PyObject *
GMPy_Real_Is_Regular(PyObject *x, CTXT_Object *context)
{
    MPFR_Object *tempx;
    int res;

    if (MPFR_Check(x)) {
        res = mpfr_regular_p(MPFR(x));
    }
    else {
        CHECK_CONTEXT(context);
        if (!(tempx = GMPy_MPFR_From_Real(x, 1, context))) {
            return NULL;
        }
        res = mpfr_regular_p(tempx->f);
        Py_DECREF((PyObject*)tempx);
    }

    if (res) {
        Py_RETURN_TRUE;
    }
    else {
        Py_RETURN_FALSE;
    }
}
Ejemplo n.º 2
0
int
fmpr_get_mpfr(mpfr_t x, const fmpr_t y, mpfr_rnd_t rnd)
{
    int r;

    if (fmpr_is_special(y))
    {
        if (fmpr_is_zero(y)) mpfr_set_zero(x, 0);
        else if (fmpr_is_pos_inf(y)) mpfr_set_inf(x, 1);
        else if (fmpr_is_neg_inf(y)) mpfr_set_inf(x, -1);
        else mpfr_set_nan(x);
        r = 0;
    }
    else if (COEFF_IS_MPZ(*fmpr_expref(y)))
    {
        flint_printf("exception: exponent too large to convert to mpfr");
        abort();
    }
    else
    {
        if (!COEFF_IS_MPZ(*fmpr_manref(y)))
#if defined(__MINGW64__) 
            r = mpfr_set_sj_2exp(x, *fmpr_manref(y), *fmpr_expref(y), rnd);
#else
            r = mpfr_set_si_2exp(x, *fmpr_manref(y), *fmpr_expref(y), rnd);
#endif
        else
            r = mpfr_set_z_2exp(x, COEFF_TO_PTR(*fmpr_manref(y)), *fmpr_expref(y), rnd);

        if (!mpfr_regular_p(x))
        {
            flint_printf("exception: exponent too large to convert to mpfr");
            abort();
        }
    }
Ejemplo n.º 3
0
/*
 * fh : IN : file hander
 * x : IN : MPFR number to put in the file
 * return 0 if successful
 */
int
mpfr_fpif_export (FILE *fh, mpfr_t x)
{
  int status;
  unsigned char *buf;
  unsigned char *bufResult;
  size_t used_size, buf_size;

  if (fh == NULL)
    return -1;

  buf_size = MAX_VARIABLE_STORAGE(sizeof(mpfr_exp_t), mpfr_get_prec (x));
  buf = (unsigned char*) (*__gmp_allocate_func) (buf_size);
  if (buf == NULL)
    return -1;

  used_size = buf_size;
  buf = mpfr_fpif_store_precision (buf, &used_size, mpfr_get_prec (x));
  used_size > buf_size ? buf_size = used_size : 0;
  status = fwrite (buf, used_size, 1, fh);
  if (status != 1)
    {
      (*__gmp_free_func) (buf, buf_size);
      return -1;
    }
  used_size = buf_size;
  bufResult = mpfr_fpif_store_exponent (buf, &used_size, x);
  if (bufResult == NULL)
    {
      (*__gmp_free_func) (buf, buf_size);
      return -1;
    }
  buf = bufResult;
  used_size > buf_size ? buf_size = used_size : 0;
  status = fwrite (buf, used_size, 1, fh);
  if (status != 1)
    {
      (*__gmp_free_func) (buf, buf_size);
      return -1;
    }

  if (mpfr_regular_p (x))
    {
      used_size = buf_size;
      buf = mpfr_fpif_store_limbs (buf, &used_size, x);
      used_size > buf_size ? buf_size = used_size : 0;
      status = fwrite (buf, used_size, 1, fh);
      if (status != 1)
        {
          (*__gmp_free_func) (buf, buf_size);
          return -1;
        }
    }

  (*__gmp_free_func) (buf, buf_size);
  return 0;
}
Ejemplo n.º 4
0
static void
check_special (void)
{
  mpc_t z[3], res;
  mpc_ptr t[3];
  int i, inex;

  /* z[0] = (1,2), z[1] = (2,3), z[2] = (3,4) */
  for (i = 0; i < 3; i++)
    {
      mpc_init2 (z[i], 17);
      mpc_set_ui_ui (z[i], i+1, i+2, MPC_RNDNN);
      t[i] = z[i];
    }
  mpc_init2 (res, 17);
  /* dot product of empty vectors is 0 */
  inex = mpc_dot (res, t, t, 0, MPC_RNDNN);
  MPC_ASSERT (inex == 0);
  MPC_ASSERT (mpfr_zero_p (mpc_realref (res)));
  MPC_ASSERT (mpfr_zero_p (mpc_imagref (res)));
  MPC_ASSERT (mpfr_signbit (mpc_realref (res)) == 0);
  MPC_ASSERT (mpfr_signbit (mpc_imagref (res)) == 0);
  /* (1,2)*(1,2) = (-3,4) */
  inex = mpc_dot (res, t, t, 1, MPC_RNDNN);
  MPC_ASSERT (inex == 0);
  MPC_ASSERT (mpfr_regular_p (mpc_realref (res)));
  MPC_ASSERT (mpfr_regular_p (mpc_imagref (res)));
  MPC_ASSERT (mpfr_cmp_si (mpc_realref (res), -3) == 0);
  MPC_ASSERT (mpfr_cmp_ui (mpc_imagref (res), 4) == 0);
  /* (1,2)*(1,2) + (2,3)*(2,3) = (-8,16) */
  inex = mpc_dot (res, t, t, 2, MPC_RNDNN);
  MPC_ASSERT (inex == 0);
  MPC_ASSERT (mpfr_regular_p (mpc_realref (res)));
  MPC_ASSERT (mpfr_regular_p (mpc_imagref (res)));
  MPC_ASSERT (mpfr_cmp_si (mpc_realref (res), -8) == 0);
  MPC_ASSERT (mpfr_cmp_ui (mpc_imagref (res), 16) == 0);
  /* (1,2)*(1,2) + (2,3)*(2,3) + (3,4)*(3,4) = (-15,40) */
  inex = mpc_dot (res, t, t, 3, MPC_RNDNN);
  MPC_ASSERT (inex == 0);
  MPC_ASSERT (mpfr_regular_p (mpc_realref (res)));
  MPC_ASSERT (mpfr_regular_p (mpc_imagref (res)));
  MPC_ASSERT (mpfr_cmp_si (mpc_realref (res), -15) == 0);
  MPC_ASSERT (mpfr_cmp_ui (mpc_imagref (res), 40) == 0);
  for (i = 0; i < 3; i++)
    mpc_clear (z[i]);
  mpc_clear (res);
}
Ejemplo n.º 5
0
bool
Floating_is_regular (Floating* self)
{
	return mpfr_regular_p(*self->value);
}
Ejemplo n.º 6
0
int
main (void)
{
    mpfr_t  x;

    tests_start_mpfr ();

    mpfr_init (x);

    /* check +infinity gives non-zero for mpfr_inf_p only */
    mpfr_set_ui (x, 1L, MPFR_RNDZ);
    mpfr_div_ui (x, x, 0L, MPFR_RNDZ);
    if (mpfr_nan_p (x) || (mpfr_nan_p) (x) )
    {
        printf ("Error: mpfr_nan_p(+Inf) gives non-zero\n");
        exit (1);
    }
    if (mpfr_inf_p (x) == 0)
    {
        printf ("Error: mpfr_inf_p(+Inf) gives zero\n");
        exit (1);
    }
    if (mpfr_number_p (x) || (mpfr_number_p) (x) )
    {
        printf ("Error: mpfr_number_p(+Inf) gives non-zero\n");
        exit (1);
    }
    if (mpfr_zero_p (x) || (mpfr_zero_p) (x) )
    {
        printf ("Error: mpfr_zero_p(+Inf) gives non-zero\n");
        exit (1);
    }
    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) )
    {
        printf ("Error: mpfr_regular_p(+Inf) gives non-zero\n");
        exit (1);
    }

    /* same for -Inf */
    mpfr_neg (x, x, MPFR_RNDN);
    if (mpfr_nan_p (x) || (mpfr_nan_p(x)))
    {
        printf ("Error: mpfr_nan_p(-Inf) gives non-zero\n");
        exit (1);
    }
    if (mpfr_inf_p (x) == 0)
    {
        printf ("Error: mpfr_inf_p(-Inf) gives zero\n");
        exit (1);
    }
    if (mpfr_number_p (x) || (mpfr_number_p)(x) )
    {
        printf ("Error: mpfr_number_p(-Inf) gives non-zero\n");
        exit (1);
    }
    if (mpfr_zero_p (x) || (mpfr_zero_p)(x) )
    {
        printf ("Error: mpfr_zero_p(-Inf) gives non-zero\n");
        exit (1);
    }
    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) )
    {
        printf ("Error: mpfr_regular_p(-Inf) gives non-zero\n");
        exit (1);
    }

    /* same for NaN */
    mpfr_sub (x, x, x, MPFR_RNDN);
    if (mpfr_nan_p (x) == 0)
    {
        printf ("Error: mpfr_nan_p(NaN) gives zero\n");
        exit (1);
    }
    if (mpfr_inf_p (x) || (mpfr_inf_p)(x) )
    {
        printf ("Error: mpfr_inf_p(NaN) gives non-zero\n");
        exit (1);
    }
    if (mpfr_number_p (x) || (mpfr_number_p) (x) )
    {
        printf ("Error: mpfr_number_p(NaN) gives non-zero\n");
        exit (1);
    }
    if (mpfr_zero_p (x) || (mpfr_zero_p)(x) )
    {
        printf ("Error: mpfr_number_p(NaN) gives non-zero\n");
        exit (1);
    }
    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) )
    {
        printf ("Error: mpfr_regular_p(NaN) gives non-zero\n");
        exit (1);
    }

    /* same for a regular number */
    mpfr_set_ui (x, 1, MPFR_RNDN);
    if (mpfr_nan_p (x) || (mpfr_nan_p)(x))
    {
        printf ("Error: mpfr_nan_p(1) gives non-zero\n");
        exit (1);
    }
    if (mpfr_inf_p (x) || (mpfr_inf_p)(x) )
    {
        printf ("Error: mpfr_inf_p(1) gives non-zero\n");
        exit (1);
    }
    if (mpfr_number_p (x) == 0)
    {
        printf ("Error: mpfr_number_p(1) gives zero\n");
        exit (1);
    }
    if (mpfr_zero_p (x) || (mpfr_zero_p) (x) )
    {
        printf ("Error: mpfr_zero_p(1) gives non-zero\n");
        exit (1);
    }
    if (mpfr_regular_p (x) == 0 || (mpfr_regular_p) (x) == 0)
    {
        printf ("Error: mpfr_regular_p(1) gives zero\n");
        exit (1);
    }

    /* Same for +0 */
    mpfr_set_ui (x, 0, MPFR_RNDN);
    if (mpfr_nan_p (x) || (mpfr_nan_p)(x))
    {
        printf ("Error: mpfr_nan_p(+0) gives non-zero\n");
        exit (1);
    }
    if (mpfr_inf_p (x) || (mpfr_inf_p)(x) )
    {
        printf ("Error: mpfr_inf_p(+0) gives non-zero\n");
        exit (1);
    }
    if (mpfr_number_p (x) == 0)
    {
        printf ("Error: mpfr_number_p(+0) gives zero\n");
        exit (1);
    }
    if (mpfr_zero_p (x) == 0 )
    {
        printf ("Error: mpfr_zero_p(+0) gives zero\n");
        exit (1);
    }
    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) )
    {
        printf ("Error: mpfr_regular_p(+0) gives non-zero\n");
        exit (1);
    }

    /* Same for -0 */
    mpfr_set_ui (x, 0, MPFR_RNDN);
    mpfr_neg (x, x, MPFR_RNDN);
    if (mpfr_nan_p (x) || (mpfr_nan_p)(x))
    {
        printf ("Error: mpfr_nan_p(-0) gives non-zero\n");
        exit (1);
    }
    if (mpfr_inf_p (x) || (mpfr_inf_p)(x) )
    {
        printf ("Error: mpfr_inf_p(-0) gives non-zero\n");
        exit (1);
    }
    if (mpfr_number_p (x) == 0)
    {
        printf ("Error: mpfr_number_p(-0) gives zero\n");
        exit (1);
    }
    if (mpfr_zero_p (x) == 0 )
    {
        printf ("Error: mpfr_zero_p(-0) gives zero\n");
        exit (1);
    }
    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) )
    {
        printf ("Error: mpfr_regular_p(-0) gives non-zero\n");
        exit (1);
    }

    mpfr_clear (x);

    tests_end_mpfr ();
    return 0;
}
Ejemplo n.º 7
0
/* TODO
 *   exponents that use more than 16 bytes are not managed
*/
static unsigned char*
mpfr_fpif_store_exponent (unsigned char *buffer, size_t *buffer_size, mpfr_t x)
{
  unsigned char *result;
  mpfr_exp_t exponent;
  mpfr_uexp_t uexp;
  size_t exponent_size;

  exponent = mpfr_get_exp (x);
  exponent_size = 0;

  if (mpfr_regular_p (x))
    {
      if (exponent > MPFR_MAX_EMBEDDED_EXPONENT ||
          exponent < -MPFR_MAX_EMBEDDED_EXPONENT)
        {
          mpfr_exp_t copy_exponent;

          uexp = SAFE_ABS (mpfr_uexp_t, exponent)
            - MPFR_MAX_EMBEDDED_EXPONENT;

          copy_exponent = uexp << 1;
          COUNT_NB_BYTE(copy_exponent, exponent_size);

          if (exponent < 0)
            uexp |= (mpfr_uexp_t) 1 << (8 * exponent_size - 1);
        }
      else
        uexp = exponent + MPFR_MAX_EMBEDDED_EXPONENT;
    }

  result = buffer;
  ALLOC_RESULT(result, buffer_size, exponent_size + 1);

  if (mpfr_regular_p (x))
    {
      if (exponent_size == 0)
        result[0] = uexp;
      else
        {
          result[0] = MPFR_EXTERNAL_EXPONENT + exponent_size;

          putLittleEndianData (result + 1, (unsigned char *) &uexp,
                               sizeof(mpfr_exp_t), exponent_size);
        }
    }
  else if (mpfr_zero_p (x))
    result[0] = MPFR_KIND_ZERO;
  else if (mpfr_inf_p (x))
    result[0] = MPFR_KIND_INF;
  else
    {
      MPFR_ASSERTD (mpfr_nan_p (x));
      result[0] = MPFR_KIND_NAN;
    }

  if (MPFR_IS_NEG (x))
    result[0] |= 0x80;

  return result;
}