Ejemplo n.º 1
0
double AB_Value_GetValueAsDouble(const AB_VALUE *v) {
  assert(v);
  if (mpz_fits_slong_p(mpq_numref(v->value)) && mpz_fits_slong_p(mpq_denref(v->value))) {
    return (double) (mpz_get_d(mpq_numref(v->value)) / mpz_get_d(mpq_denref(v->value)));
  }
  else {
    return mpq_get_d(v->value);
  }
}
Ejemplo n.º 2
0
Term
Yap_MkBigIntTerm(MP_INT *big)
{
  CACHE_REGS
  Int nlimbs;
  MP_INT *dst = (MP_INT *)(H+2);
  CELL *ret = H;
  Int bytes;

  if (mpz_fits_slong_p(big)) {
    long int out = mpz_get_si(big);
    return MkIntegerTerm((Int)out);
  }
  //  bytes = big->_mp_alloc * sizeof(mp_limb_t);
  //  nlimbs = ALIGN_YAPTYPE(bytes,CELL)/CellSize;
  // this works, but it shouldn't need to do this...
  nlimbs = big->_mp_alloc;
  bytes = nlimbs*sizeof(CELL);
  if (nlimbs > (ASP-ret)-1024) {
    return TermNil;
  }
  H[0] = (CELL)FunctorBigInt;
  H[1] = BIG_INT;

  dst->_mp_size = big->_mp_size;
  dst->_mp_alloc = nlimbs*(CellSize/sizeof(mp_limb_t));
  memmove((void *)(dst+1), (const void *)(big->_mp_d), bytes);
  H = (CELL *)(dst+1)+nlimbs;
  H[0] = EndSpecials;
  H++;
  return AbsAppl(ret);
}
Ejemplo n.º 3
0
int qsieve_getsize(mpz_t x)
{
    int t;
    t = mpz_get_si(x);
    if(mpz_fits_slong_p(x)) return t;
    return (t<0 ? -0x7FFFFFFF : +0x7FFFFFFF);
}
Ejemplo n.º 4
0
ecl_long_long_t
ecl_to_long_long(cl_object x)
{
	if (ECL_FIXNUMP(x)) {
		return (ecl_long_long_t)ecl_fixnum(x);
	} else if (!ECL_BIGNUMP(x)) {
		(void)0;
	} else if (mpz_fits_slong_p(x->big.big_num)) {
		return (ecl_long_long_t)mpz_get_si(x->big.big_num);
	} else {
		cl_object copy = _ecl_big_register0();
		int i = ECL_LONG_LONG_BITS - FIXNUM_BITS;
		mpz_fdiv_q_2exp(copy->bit.big_num, x->big.big_num, i);
		if (mpz_fits_ulong_p(copy->big.big_num)) {
			volatile ecl_long_long_t output;
			output = mpz_get_si(copy->big.big_num);
			for (i -= FIXNUM_BITS; i; i-= FIXNUM_BITS) {
				output = (output << FIXNUM_BITS);
				output += mpz_get_ui(x->big.big_num);
			}
			return output;
		}
	}
	FEwrong_type_argument(cl_list(3,ECL_SYM("INTEGER",437),
				      ecl_negate(ecl_ash(ecl_make_fixnum(1), ECL_LONG_LONG_BITS-1)),
				      ecl_one_minus(ecl_ash(ecl_make_fixnum(1), ECL_LONG_LONG_BITS-1))),
			      x);
}
Ejemplo n.º 5
0
/* This function is used by Miller */
int step3(int j, mpz_t m, mpz_t x, mpz_t a, mpz_t p, mpz_t p1){
    int newm;
    int somethi = (int) pow(2.0,(double)j);
    printf("something = %d\n", somethi);
    if(mpz_fits_slong_p(m) == 0){
        printf("this is problematic cause m is too huge for gmp to handle");
        return 0;
    }
    else{
        newm = mpz_get_si(m);

    }
    somethi = somethi*newm;
    printf("something (after new M) = %d\n", somethi);
    gmp_printf("x (before mod): %Zd\t & p1: %Zd\n", x, p1);
    mpz_powm_ui(x, a, somethi, p);
    gmp_printf("x (step 3) = %Zd\n", x);
    if(mpz_cmp_ui(x, 1) == 0){
        printf("entered if\n");
        return 0;
    }
    else if(mpz_cmp(x, p1) == 0){
        printf("enter else if\n");
        return 1;
    }
    else{
        printf("enter else, let's see j's value, %d\n", j);
        return ++j;
    }
}
Ejemplo n.º 6
0
Archivo: ZZ.cpp Proyecto: DanGrayson/M2
std::pair<bool, int> RingZZ::get_si(mpz_t n)
{
  if (not mpz_fits_slong_p(n)) return std::make_pair<bool, int>(false, 0);
  long a = mpz_get_si(n);
  int b = static_cast<int>(a);
  if (a == b) return std::make_pair<bool, int>(true, std::move(b));
  return std::make_pair<bool, int>(false, 0);
}
Ejemplo n.º 7
0
/* mpz -> pyint/pylong conversion; if the value fits in a python int, it
returns a python int (optimised for that pathway), otherwise returns
a python long */
PyObject *
mpz_get_pyintlong(mpz_srcptr z)
{
  if (mpz_fits_slong_p(z))
     return PyInt_FromLong(mpz_get_si(z));

  return mpz_get_pylong(z);
}
Ejemplo n.º 8
0
object
make_integer(__mpz_struct *u)
{
  if ((u)->_mp_size == 0) return small_fixnum(0);
  if (mpz_fits_slong_p(u)) {
    return make_fixnum(mpz_get_si(u));
      }
  return make_bignum(u);
}
Ejemplo n.º 9
0
/*
void camlidl_mpq_ml2c(value val, __mpq_struct* mpq)
{
  *mpq = *((mpq_ptr)(Data_custom_val(val)));
}
*/
int mpz_fits_int_p (mpz_t OP)
{
    if (mpz_fits_slong_p(OP)) {
        long v = mpz_get_si(OP);
        return (Min_long <= v && v<=Max_long);
    }
    else {
        return 0;
    }
}
Ejemplo n.º 10
0
static CBIGINT *_pow(CBIGINT *a, CBIGINT *b, bool invert)
{
	if (!mpz_fits_slong_p(b->n))
	{
		GB.Error(GB_ERR_OVERFLOW);
		return NULL;
	}

	return BIGINT_make_int(a, mpz_get_si(b->n), mpz_pow_ui);
}
Ejemplo n.º 11
0
int fmpz_fits_si(const fmpz_t f)
{
    if (!COEFF_IS_MPZ(*f))
    {
        return 1;
    }
    else
    {
        return mpz_fits_slong_p(COEFF_TO_PTR(*f));
    }
}
Ejemplo n.º 12
0
Archivo: big.o.c Proyecto: hoobaa/mecl
cl_fixnum
fixint(cl_object x)
{
        if (ECL_FIXNUMP(x))
                return ecl_fixnum(x);
        if (ECL_BIGNUMP(x)) {
                if (mpz_fits_slong_p(x->big.big_num)) {
                        return mpz_get_si(x->big.big_num);
                }
        }
	FEwrong_type_argument(ecl_make_fixnum(/*FIXNUM*/372), x);
}
Ejemplo n.º 13
0
 void power_mpz(ElementType &result, const ElementType& a, mpz_ptr n) const
 {
   if (mpz_fits_slong_p(n)) 
     {
       int n1 = static_cast<int>(mpz_get_si(n));
       power(result,a,n1);
     } 
   else  
     {
       ERROR("exponent too large");
       set_from_long(result, 1);
     }
 }
Ejemplo n.º 14
0
extern "C" Py_ssize_t PyLong_AsSsize_t(PyObject* vv) noexcept {
    RELEASE_ASSERT(PyLong_Check(vv), "");

    if (PyLong_Check(vv)) {
        BoxedLong* l = static_cast<BoxedLong*>(vv);
        if (mpz_fits_slong_p(l->n)) {
            return mpz_get_si(l->n);
        } else {
            PyErr_SetString(PyExc_OverflowError, "long int too large to convert to int");
            return -1;
        }
    }
    Py_FatalError("unimplemented");
}
Ejemplo n.º 15
0
ecl_int64_t
ecl_to_int64_t(cl_object x) {
	if (ECL_FIXNUMP(x)) {
		return (ecl_int64_t)ecl_fixnum(x);
	} else if (!ECL_BIGNUMP(x)) {
		(void)0;
	} else if (mpz_fits_slong_p(x->big.big_num)) {
		return (ecl_int64_t)mpz_get_si(x->big.big_num);
	} else {
		cl_object copy = _ecl_big_register0();
		mpz_fdiv_q_2exp(copy->big.big_num, x->big.big_num, 32);
		if (mpz_fits_slong_p(copy->big.big_num)) {
			ecl_int64_t output;
			output = (ecl_int64_t)mpz_get_si(copy->big.big_num);
			mpz_fdiv_r_2exp(copy->big.big_num, x->big.big_num, 32);
			return (output << 32) + mpz_get_ui(copy->big.big_num);
		}
	}
	FEwrong_type_argument(cl_list(3,ECL_SYM("INTEGER",437),
				      ecl_negate(ecl_ash(ecl_make_fixnum(1), 63)),
				      ecl_one_minus(ecl_ash(ecl_make_fixnum(1), 63))),
			      x);
}
Ejemplo n.º 16
0
int fmpz_fits_si(const fmpz_t f)
{
    if (!COEFF_IS_MPZ(*f))
    {
        return 1;
    }
    else
    {
#if defined(_WIN64)
       return flint_mpz_fits_si_p(COEFF_TO_PTR(*f));
#else
       return mpz_fits_slong_p(COEFF_TO_PTR(*f));
#endif
    }
}
/* Converts a GMP integer into a backend tree node.  */
tree
gfc_conv_mpz_to_tree (mpz_t i, int kind)
{
  HOST_WIDE_INT high;
  unsigned HOST_WIDE_INT low;

  if (mpz_fits_slong_p (i))
    {
      /* Note that HOST_WIDE_INT is never smaller than long.  */
      low = mpz_get_si (i);
      high = mpz_sgn (i) < 0 ? -1 : 0;
    }
  else
    {
      unsigned HOST_WIDE_INT words[2];
      size_t count;

      /* Since we know that the value is not zero (mpz_fits_slong_p),
	 we know that at one word will be written, but we don't know
	 about the second.  It's quicker to zero the second word before
	 that conditionally clear it later.  */
      words[1] = 0;

      /* Extract the absolute value into words.  */
      mpz_export (words, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, i);

      /* We assume that all numbers are in range for its type, and that
	 we never create a type larger than 2*HWI, which is the largest
	 that the middle-end can handle.  */
      gcc_assert (count == 1 || count == 2);

      low = words[0];
      high = words[1];

      /* Negate if necessary.  */
      if (mpz_sgn (i) < 0)
	{
	  if (low == 0)
	    high = -high;
	  else
	    low = -low, high = ~high;
	}
    }

  return build_int_cst_wide (gfc_get_int_type (kind), low, high);
}
Ejemplo n.º 18
0
static CRATIONAL *_powo(CRATIONAL *a, void *o, bool invert)
{
    CBIGINT *b;

    if (invert || !GB.Is(o, CLASS_BigInt))
        return NULL;

    b = (CBIGINT *)o;

    if (!mpz_fits_slong_p(b->n))
    {
        GB.Error(GB_ERR_OVERFLOW);
        return NULL;
    }

    return _powf(a, (double)mpz_get_si(b->n), invert);
}
Ejemplo n.º 19
0
static CBIGINT *_powf(CBIGINT *a, double f, bool invert)
{
	if (invert)
	{
		mpz_t b;

		if (!mpz_fits_slong_p(a->n))
			return NULL;

		mpz_init_set_si(b, (long)f);
		mpz_pow_ui(b, b, mpz_get_si(a->n));

		return BIGINT_create(b);
	}
	else
		return BIGINT_make_int(a, f, mpz_pow_ui);
}
Ejemplo n.º 20
0
int
check_si (mpz_t sz, mpz_t oz, long si, long oi, int c)
{
  mpz_t t;
  int fail;

  if (mpz_cmp_si (sz, oi) != c)
    {
      printf ("mpz_cmp_si (sz, %ld) != %i.\n", oi, c);
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      abort ();
    }

  if ((si < oi ? -1 : si > oi) != c)
    return 1;

  mpz_init_set_si (t, si);

  if ((fail = mpz_cmp_si (sz, si)) != 0)
    printf ("mpz_cmp_si (sz, %ld) != 0.\n", si);
  if (mpz_cmp_si (oz, si) != -c)
    printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, -c), fail = 1;
  if (! mpz_fits_slong_p (sz))
    printf ("mpz_fits_slong_p (sz) != 1.\n"), fail = 1;
  if (mpz_get_si (sz) != si)
    printf ("mpz_get_si (sz) != %ld.\n", si), fail = 1;
  if (mpz_cmp (t, sz) != 0)
    {
      printf ("mpz_init_set_si (%ld) failed.\n", si);
      printf (" got="); mpz_out_str (stdout, 10, t); printf ("\n");
      fail = 1;
    }

  mpz_clear (t);

  if (fail)
    {
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
      printf (" si=%ld\n", si);
      abort ();
    }

  return 0;
}
Ejemplo n.º 21
0
signed long vanilla::int_object_to_signed_long(object::ptr const& obj)
{
    if(obj->type_id() != OBJECT_ID_INT)
    {
        BOOST_THROW_EXCEPTION(error::bad_cast_error()
            << error::first_operand(obj)
            << error::cast_target_name("int"));
    }
    
    mpz_t& mpz = static_cast<int_object const*>(obj.get())->value().mpz();
    if(!mpz_fits_slong_p(mpz))
    {
        BOOST_THROW_EXCEPTION(error::integer_conversion_overflow_error()
            << error::first_operand(obj)
            << error::integer_conversion_target_type("signed long"));
    }
    return mpz_get_si(mpz);
}
Ejemplo n.º 22
0
extern "C" long PyLong_AsLongAndOverflow(Box* vv, int* overflow) noexcept {
    // Ported from CPython; original comment:
    /* This version by Tim Peters */

    *overflow = 0;
    if (vv == NULL) {
        PyErr_BadInternalCall();
        return -1;
    }

    if (PyInt_Check(vv))
        return PyInt_AsLong(vv);

    if (!PyLong_Check(vv)) {
        PyNumberMethods* nb;
        nb = vv->cls->tp_as_number;
        if (nb == NULL || nb->nb_int == NULL) {
            PyErr_SetString(PyExc_TypeError, "an integer is required");
            return -1;
        }

        vv = (*nb->nb_int)(vv);
        if (vv == NULL)
            return -1;

        if (PyInt_Check(vv))
            return PyInt_AsLong(vv);

        if (!PyLong_Check(vv)) {
            PyErr_SetString(PyExc_TypeError, "nb_int should return int object");
            return -1;
        }
        // fallthrough: this has to be a long
    }

    BoxedLong* l = static_cast<BoxedLong*>(vv);
    if (mpz_fits_slong_p(l->n)) {
        return mpz_get_si(l->n);
    } else {
        *overflow = mpz_sgn(l->n);
        return -1;
    }
}
Ejemplo n.º 23
0
Value Bignum::add(long n) const
{
  mpz_t result;
  mpz_init_set(result, _z);
  if (n >= 0)
    mpz_add_ui(result, result, (unsigned long) n);
  else
    mpz_sub_ui(result, result, (unsigned long) -n);
  if (mpz_fits_slong_p(result))
    {
      long n = mpz_get_si(result);
      if (n >= MOST_NEGATIVE_FIXNUM && n <= MOST_POSITIVE_FIXNUM)
        {
          MPZ_CLEAR(result);
          return make_fixnum(n);
        }
    }
  return make_value(new Bignum(result, false));
}
Ejemplo n.º 24
0
APLRAT PrimFnMonQuoteDotRisR
    (APLRAT     aplRatRht,
     LPPRIMSPEC lpPrimSpec)

{
    APLRAT mpqRes = {0};
    UINT   uRht;

    // Check for indeterminates:  !N for integer N < 0
    if (mpq_integer_p (&aplRatRht)
     && mpq_cmp_ui (&aplRatRht, 0, 1) < 0)
        return *mpq_QuadICValue (&aplRatRht,        // No left arg
                                  ICNDX_QDOTn,
                                 &aplRatRht,
                                 &mpqRes,
                                  FALSE);
    // Check for PosInfinity
    if (IsMpqPosInfinity (&aplRatRht))
        return mpqPosInfinity;

    // If the denominator is 1,
    //   and the numerator fts in a UINT, ...
    if (mpq_integer_p (&aplRatRht)
     && mpz_fits_slong_p (mpq_numref (&aplRatRht)) NE 0)
    {
        // Initialize the result to 0/1
        mpq_init (&mpqRes);

        // Extract the numerator
        uRht = (UINT) mpz_get_si (mpq_numref (&aplRatRht));

        // Compute the factorial
        mpz_fac_ui (mpq_numref (&mpqRes), uRht);
    } else
        RaiseException (EXCEPTION_RESULT_VFP, 0, 0, NULL);

    return mpqRes;
} // End PrimFnMonQuoteDotRisR
Ejemplo n.º 25
0
void
try_op_si (int c)
{
  long  si, oi;
  mpz_t sz, oz;

  si = c;
  mpz_init_set_si (sz, si);

  oi = si;
  mpz_init_set (oz, sz);

  do {
    si *= 2; /* c * 2^k */
    mpz_mul_2exp (sz, sz, 1);

    if (check_si (sz, oz, si, oi, c))
      {
	mpz_set (oz, sz);
	break;
      }

    oi = si + c; /* c * (2^k + 1) */
    if (c == -1)
      mpz_sub_ui (oz, sz, 1);
    else
      mpz_add_ui (oz, sz, 1);

    if (check_si (oz, sz, oi, si, c))
      break;

    oi = (si - c) * 2 + c; /* c * (2^K - 1) */
    mpz_mul_si (oz, sz, 2*c);
    if (c == -1)
      mpz_ui_sub (oz, 1, oz); /* oz = sz * 2 + 1 */
    else
      mpz_sub_ui (oz, oz, 1); /* oz = sz * 2 - 1 */
  } while (check_si (oz, sz, oi, si, c) == 0);

  mpz_clear (sz);

  if (mpz_fits_slong_p (oz))
    {
      printf ("Should not fit a signed long any more.\n");
      printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
      abort ();
    }

  if (mpz_cmp_si (oz, -c) != c)
      {
	printf ("mpz_cmp_si (oz, %i) != %i.\n", c, c);
	printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
	abort ();
      }

  mpz_mul_2exp (oz, oz, 1);
  if (mpz_cmp_si (oz, -c) != c)
      {
	printf ("mpz_cmp_si (oz, %i) != %i.\n", c, c);
	printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
	abort ();
      }

  mpz_clear (oz);
}
Ejemplo n.º 26
0
int gmpint_fits_int(mpz_t *op) {
    return mpz_fits_slong_p(*op);
}
Ejemplo n.º 27
0
int main(int argc, char *argv[])
{
  Vstr_base *s1 = ex_init(NULL);
  mpz_t bignum;
  const char *loc_num_name = NULL;
  Vstr_ref *ref = NULL;
  const unsigned int num_flags = (VSTR_FLAG_PARSE_NUM_SEP |
                                  VSTR_FLAG_PARSE_NUM_SPACE);
  
  if (argc < 2)
    errx(EXIT_FAILURE, "No count specified");

  vstr_cntl_conf(s1->conf, VSTR_CNTL_CONF_SET_FMT_CHAR_ESC, '$');
  vstr_fmt_add(s1->conf, "<dMPZ:%p>", ex_usr_dmpz_cb,
               VSTR_TYPE_FMT_PTR_VOID, VSTR_TYPE_FMT_END);
  vstr_fmt_add(s1->conf, "<oMPZ:%p>", ex_usr_ompz_cb,
               VSTR_TYPE_FMT_PTR_VOID, VSTR_TYPE_FMT_END);
  vstr_fmt_add(s1->conf, "<xMPZ:%p>", ex_usr_xmpz_cb,
               VSTR_TYPE_FMT_PTR_VOID, VSTR_TYPE_FMT_END);
  vstr_fmt_add(s1->conf, "<bMPZ:%p>", ex_usr_bmpz_cb,
               VSTR_TYPE_FMT_PTR_VOID, VSTR_TYPE_FMT_END);

  setlocale(LC_ALL, "");
  loc_num_name = setlocale(LC_NUMERIC, NULL);
  
  if (!vstr_cntl_conf(s1->conf, VSTR_CNTL_CONF_SET_LOC_CSTR_AUTO_NAME_NUMERIC,
                      loc_num_name))
    errx(EXIT_FAILURE, "Couldn't change numeric locale info");

  mpz_init(bignum);
  
  if (!vstr_add_cstr_ptr(s1, 0, argv[1]) ||
      !vstr_parse_num(s1, 1, s1->len, num_flags, NULL, NULL, vmpz_num, bignum))
    errno = ENOMEM, err(EXIT_FAILURE, "Couldn't init number");

  vstr_del(s1, 1, s1->len);
    
#if (EX_GMP_NUMS_USE_TST)
  {
    mpz_abs(bignum, bignum);
    mpz_neg(bignum, bignum);
  do
  {
    mpz_neg(bignum, bignum);

    vstr_add_fmt(s1, s1->len, "%%'20.40d = <$'20.40<dMPZ:%p>>\n",
                 (void *)bignum);
    if (mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%'20.40d = <%'20.40ld>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%'40.20d = <$'40.20<dMPZ:%p>>\n",
                 (void *)bignum);
    if (mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%'40.20d = <%'40.20ld>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%'20.40o = <$'20.40<oMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%'20.40o = <%'20.40lo>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%'40.20o = <$'40.20<oMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%'40.20o = <%'40.20lo>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%'20.40x = <$'20.40<xMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%'20.40x = <%'20.40lx>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%'40.20x = <$'40.20<xMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%'40.20x = <%'40.20lx>\n",
                 mpz_get_si(bignum));

    vstr_add_fmt(s1, s1->len, "%%#'40d   = <$#'40<dMPZ:%p>>\n",
                 (void *)bignum);
    if (mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%#'40d   = <%#'40ld>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%#'-40d  = <$#'-40<dMPZ:%p>>\n",
                 (void *)bignum);
    if (mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%#'-40d  = <%#'-40ld>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%#'40o   = <$#'40<oMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%#'40o   = <%#'40lo>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%#'-40o  = <$#'-40<oMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%#'-40o  = <%#'-40lo>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%#'40x   = <$#'40<xMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%#'40x   = <%#'40lx>\n",
                 mpz_get_si(bignum));
    vstr_add_fmt(s1, s1->len, "%%#'-40x  = <$#'-40<xMPZ:%p>>\n",
                 (void *)bignum);
    if ((mpz_sgn(bignum) != -1) && mpz_fits_slong_p(bignum))
    vstr_add_fmt(s1, s1->len, "%%#'-40x  = <%#'-40lx>\n",
                 mpz_get_si(bignum));

    vstr_add_fmt(s1, s1->len, "%%#'b  = <$#'<bMPZ:%p>>\n",
                 (void *)bignum);
    
    vstr_add_rep_chr(s1, s1->len, '-', 79);
    vstr_add_rep_chr(s1, s1->len, '\n', 1);
    
  } while (mpz_sgn(bignum) > 0);
    mpz_abs(bignum, bignum);
  }
#endif
  
  if (!(ref = vstr_ref_make_strdup("_")))
    errno = ENOMEM, err(EXIT_FAILURE, "Ref seperator");

  if (!vstr_cntl_conf(NULL, VSTR_CNTL_CONF_SET_LOC_REF_THOU_SEP,  2, ref, 1) ||
      !vstr_cntl_conf(NULL, VSTR_CNTL_CONF_SET_LOC_REF_THOU_SEP,  8, ref, 1) ||
      !vstr_cntl_conf(NULL, VSTR_CNTL_CONF_SET_LOC_REF_THOU_SEP, 16, ref, 1) ||
      FALSE)
    errno = ENOMEM, err(EXIT_FAILURE, "Add seperator");
  vstr_ref_del(ref);
  
  if (!(ref = vstr_ref_make_strdup("\4")))
    errno = ENOMEM, err(EXIT_FAILURE, "Ref grouping");

  if (!vstr_cntl_conf(NULL, VSTR_CNTL_CONF_SET_LOC_REF_THOU_GRP,  2, ref) ||
      !vstr_cntl_conf(NULL, VSTR_CNTL_CONF_SET_LOC_REF_THOU_GRP, 16, ref) ||
      FALSE)
    errno = ENOMEM, err(EXIT_FAILURE, "Add grouping");
  vstr_ref_del(ref);
  
  vstr_add_fmt(s1, s1->len, " Input: %s\n", argv[1]);
  vstr_add_fmt(s1, s1->len, "    %%#'x = $#'<xMPZ:%p>\n",
               (void *)bignum);
  vstr_add_fmt(s1, s1->len, "    %%#'d = $#'<dMPZ:%p>\n",
               (void *)bignum);
  vstr_add_fmt(s1, s1->len, "    %%#'o = $#'<oMPZ:%p>\n",
               (void *)bignum);
  vstr_add_fmt(s1, s1->len, "    %%#'b = $#'<bMPZ:%p>\n",
               (void *)bignum);
  
  if (s1->conf->malloc_bad)
    errno = ENOMEM, err(EXIT_FAILURE, "Add string data");
  
  io_put_all(s1, STDOUT_FILENO);

  exit (ex_exit(s1, NULL));
}
Ejemplo n.º 28
0
 void power_mpz(ElementType& result,const  ElementType& a,const mpz_ptr n) const {
     if (mpz_fits_slong_p(n))
         power(result, a, mpz_get_ui(n));
     else
         throw exc::engine_error("attempted to take a power of an integer to too large of a power");
 }
Ejemplo n.º 29
0
void
check_data (void)
{
  static const struct {
    int         a_base;
    const char  *a;
    const char  *b;
    int         want;
  } data[] = {
    { 10, "0",  "1", -1 },
    { 10, "0",  "0",  0 },
    { 10, "0", "-1",  1 },

    { 10, "1",  "1", 0 },
    { 10, "1",  "0", 1 },
    { 10, "1", "-1", 1 },

    { 10, "-1",  "1", -1 },
    { 10, "-1",  "0", -1 },
    { 10, "-1", "-1", 0 },

    { 16,         "0", "-0x80000000",  1 },
    { 16,  "80000000", "-0x80000000",  1 },
    { 16,  "80000001", "-0x80000000",  1 },
    { 16, "-80000000", "-0x80000000",  0 },
    { 16, "-80000001", "-0x80000000", -1 },
    { 16, "-FF0080000001", "-0x80000000", -1 },

    { 16,                 "0", "-0x8000000000000000",  1 },
    { 16,  "8000000000000000", "-0x8000000000000000",  1 },
    { 16,  "8000000000000001", "-0x8000000000000000",  1 },
    { 16, "-8000000000000000", "-0x8000000000000000",  0 },
    { 16, "-8000000000000001", "-0x8000000000000000", -1 },
    { 16, "-FF008000000000000001", "-0x8000000000000000", -1 },
  };

  mpf_t  a;
  mpz_t  bz;
  long   b;
  int    got;
  int    i;

  mpf_init (a);
  mpz_init (bz);
  for (i = 0; i < numberof (data); i++)
    {
      mpf_set_str_or_abort (a, data[i].a, data[i].a_base);
      mpz_set_str_or_abort (bz, data[i].b, 0);

      if (mpz_fits_slong_p (bz))
        {
          b = mpz_get_si (bz);
          got = mpf_cmp_si (a, b);
          if (SGN (got) != data[i].want)
            {
              printf ("mpf_cmp_si wrong on data[%d]\n", i);
              printf ("  a="); mpf_out_str (stdout, 10, 0, a);
              printf (" (%s)\n", data[i].a);
              printf ("  b=%ld (%s)\n", b, data[i].b);
              printf ("  got=%d\n", got);
              printf ("  want=%d\n", data[i].want);
              abort();
            }
        }
    }

  mpf_clear (a);
  mpz_clear (bz);
}
Ejemplo n.º 30
0
bool RingZZ::get_si(int &result, mpz_t n)
{
  result = static_cast<int>(mpz_get_si(n));
  return mpz_fits_slong_p(n);
}