Esempio n. 1
0
File: hilbert.c Progetto: blynn/pbc
static void precision_clear(void) {
  mpf_clear(eulere);
  mpf_clear(recipeulere);
  mpf_clear(pi);
  mpf_clear(epsilon);
  mpf_clear(negepsilon);
}
Esempio n. 2
0
File: hilbert.c Progetto: blynn/pbc
static void mpf_exp(mpf_t res, mpf_t pwr) {
  mpf_t a;
  mpf_t f0;
  int i;

  mpf_init(a); mpf_set(a, pwr);

  mpf_init(f0);

  mpf_set(f0, a);
  mpf_add_ui(res, a, 1);

  for (i=2;;i++) {
    mpf_mul(f0, f0, a);
    mpf_div_ui(f0, f0, i);
    if (mpf_sgn(f0) > 0) {
      if (mpf_cmp(f0, epsilon) < 0) break;
    } else {
      if (mpf_cmp(f0, negepsilon) > 0) break;
    }
    mpf_add(res, res, f0);
  }

  mpf_clear(f0);
  mpf_clear(a);
}
Esempio n. 3
0
void
check_various (void)
{
  mpf_t got, u, v;

  mpf_init (got);
  mpf_init (u);
  mpf_init (v);

  /* 100/4 == 25 */
  mpf_set_prec (got, 20L);
  mpf_set_ui (u, 100L);
  mpf_set_ui (v, 4L);
  mpf_div (got, u, v);
  MPF_CHECK_FORMAT (got);
  ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0);

  /* 1/(2^n+1), a case where truncating the divisor would be wrong */
  mpf_set_prec (got, 500L);
  mpf_set_prec (v, 900L);
  mpf_set_ui (v, 1L);
  mpf_mul_2exp (v, v, 800L);
  mpf_add_ui (v, v, 1L);
  mpf_div (got, u, v);
  check_one ("1/2^n+1, separate", got, u, v);

  mpf_clear (got);
  mpf_clear (u);
  mpf_clear (v);
}
Esempio n. 4
0
/**
 * rasqal_xsd_decimal_round:
 * @result: result variable
 * @a: argment decimal
 * 
 * Return the number with no fractional part closes to argument for an XSD Decimal
 *
 * Return value: non-0 on failure
 **/
int
rasqal_xsd_decimal_round(rasqal_xsd_decimal* result, rasqal_xsd_decimal* a)
{
  int rc = 0;
#ifdef RASQAL_DECIMAL_GMP
  mpf_t b;
  mpf_t c;
#endif
  
  rasqal_xsd_decimal_clear_string(result);
  
#if defined(RASQAL_DECIMAL_C99) || defined(RASQAL_DECIMAL_NONE)
  result->raw = round(a->raw);
#endif
#ifdef RASQAL_DECIMAL_MPFR
  mpfr_round(result->raw, a->raw);
#endif
#ifdef RASQAL_DECIMAL_GMP
  /* GMP has no mpf_round so use result := floor(a + 0.5) */
  mpf_init2(b, a->precision_bits);
  mpf_init2(c, a->precision_bits);

  mpf_set_d(b, 0.5);
  mpf_add(c, a->raw, b);
  mpf_floor(result->raw, c);

  mpf_clear(b);
  mpf_clear(c);
#endif

  return rc;
}
void extrapolate(index_t num_samples, mpf_t *samples, mpf_t ans) {
    // The Richardson extrapolation recursive formula is
    //
    // A_n+1(x) = (2^n A_n(2x) - A_n(x)) / (2^n - 1)

    mpf_t mult;  // mult = 2^n
    mpf_init_set_d(mult, 1);

    mpf_t denom;  // denom = 1 / (mult - 1)
    mp_bitcnt_t precision = mpf_get_prec(ans);
    mpf_init2(denom, precision);

    mpf_t *end = samples + num_samples;
    for (mpf_t *lim = samples; lim < end; lim++) {  // lim - samples = n
        mpf_mul_ui(mult, mult, 2);
        mpf_set(denom, mult);
        mpf_sub_ui(denom, denom, 1);
        mpf_ui_div(denom, 1, denom);
        // evaluate all extrapolations
        for (mpf_t *ptr = end; --ptr > lim; ) {
            // A_n+1(x) = (mult A_n(2x) - A_n(x)) * denom
            mpf_mul(*ptr, *ptr, mult);
            mpf_sub(*ptr, *ptr, *(ptr - 1));
            mpf_mul(*ptr, *ptr, denom);
        }
    }
    mpf_set(ans, *(end - 1));  // move to ans

    // Clean
    mpf_clear(mult);
    mpf_clear(denom);
}
static void destructor(render_t* handle)
{
	mpf_clear(handle->center.real_part);
	mpf_clear(handle->center.imaginary_part);
	mpf_clear(handle->scale);
	free(handle);
}
Esempio n. 7
0
void my_clear()
{
    mpf_clear(t1);
    mpf_clear(t2);
    mpf_clear(d1);
    mpf_clear(d2);
}
Esempio n. 8
0
/* 
 * Evaluate the series 1/0! + 1/1! + 1/2! + 1/3! + 1/4! ...
 * On input, 'bits' is the desired precision in bits.
 * On output, e' contains the calculated value.
 */
static void calculateE(
	unsigned bits,
	mpf_t e)
{
	mpf_t lastE;
	mpf_t invFact;					/* 1/2!, 1/3!, 1/4!, etc. */
	unsigned term;					/* 2, 3, 4... */
	
	/* initial conditions, including the first two terms */
	mpf_init_set_ui(lastE, 0);
	mpf_set_ui(e, 2);				
	mpf_init_set_ui(invFact, 1);	/* 1/1! */
	term = 2;
	
	for(;;) {
		/* invFact /= (term) */
		mpf_div_ui(invFact, invFact, term);
		/* e += 1 / (term!) */
		mpf_add(e, e, invFact);
		
		/* if e == lastE, within the requested precision, we're done */
		if(mpf_eq(e, lastE, bits)) {
			break;
		}
		mpf_set(lastE, e);
		term++;
	}
	/* free memory associated with mpf_t's we allocated */
	mpf_clear(lastE);
	mpf_clear(invFact);
}
Esempio n. 9
0
void freeMem() {
	stopThreads();
	mpf_clear(mandelRange.minRe);
	mpf_clear(mandelRange.maxRe);
	mpf_clear(mandelRange.minIm);
	mpf_clear(mandelRange.maxIm);
	cleanMemory();
}
Esempio n. 10
0
void
check_rand (void)
{
  unsigned long  min_prec = __GMPF_BITS_TO_PREC (1);
  gmp_randstate_t  rands;
  mpf_t              got, u;
  unsigned long      prec, v;
  int                i;

  /* The nails code in mpf_mul_ui currently isn't exact, so suppress these
     tests for now.  */
  if (BITS_PER_UI > GMP_NUMB_BITS)
    return;

  mpf_init (got);
  mpf_init (u);
  gmp_randinit_default(rands);

  for (i = 0; i < 200; i++)
    {
      /* got precision */
      prec = min_prec + gmp_urandomm_ui (rands, 15L);
      refmpf_set_prec_limbs (got, prec);

      /* u precision */
      prec = min_prec + gmp_urandomm_ui (rands, 15L);
      refmpf_set_prec_limbs (u, prec);

      /* u, possibly negative */
      mpf_rrandomb (u, rands, PREC(u), (mp_exp_t) 20);
      if (gmp_urandomb_ui (rands, 1L))
        mpf_neg (u, u);

      /* v, 0 to BITS_PER_ULONG bits (inclusive) */
      prec = gmp_urandomm_ui (rands, BITS_PER_ULONG+1);
      v = gmp_urandomb_ui (rands, prec);

      if ((i % 2) == 0)
        {
          /* separate */
          mpf_mul_ui (got, u, v);
          check_one ("separate", got, u, v);
        }
      else
        {
          /* overlap */
          prec = refmpf_set_overlap (got, u);
          mpf_mul_ui (got, got, v);
          check_one ("overlap src==dst", got, u, v);

          mpf_set_prec_raw (got, prec);
        }
    }

  mpf_clear (got);
  mpf_clear (u);
  gmp_randclear(rands);
}
Esempio n. 11
0
void mpfr_bench(mpfr_prec_t prec_a, mpfr_prec_t prec_b, mpfr_prec_t prec_c, 
		const char *b_str, const char *c_str, unsigned long seed)
{
  mpfr_t a,b,c;
  mpf_t x,y,z;
  mpz_t zz;
  gmp_randstate_t state;

  gmp_randinit_lc_2exp_size (state, 128);
  gmp_randseed_ui (state, seed);

  mpfr_init2(a, prec_a);
  mpfr_init2(b, prec_b);
  mpfr_init2(c, prec_c);

  mpf_init2(x,  prec_a);
  mpf_init2(y,  prec_b);
  mpf_init2(z,  prec_c);

  if (b_str)
    mpf_set_str(y, b_str, 10);
  else
    mpf_urandomb(y, state, prec_b);
  if (c_str)
    mpf_set_str(z, c_str, 10);
  else
    mpf_urandomb(z, state, prec_c);
  mpfr_set_f(b, y, MPFR_RNDN);
  mpfr_set_f(c, z, MPFR_RNDN);
  mpz_init (zz);
  mpz_urandomb (zz, state, 2*prec_b);

  if (verbose)
    {
      printf("B="); mpfr_out_str(stdout, 10, 0, b, MPFR_RNDD);
      printf("\nC="); mpfr_out_str(stdout, 10, 0, c, MPFR_RNDD);
      putchar('\n');
    }
  TIMP_OVERHEAD ();
#undef BENCH
#define BENCH(TEST_STR, TEST) printf(" "TEST_STR": %Lu\n", TIMP_MEASURE(TEST))
  TEST_LIST;

  mpz_clear (zz);
  mpfr_clear (a);
  mpfr_clear (b);
  mpfr_clear (c);
  mpf_clear (x);
  mpf_clear (y);
  mpf_clear (z);
  gmp_randclear (state);
}
Esempio n. 12
0
/**
 * @brief Print a float to stdout (or whatever the output stream is
 * atm) respecting the given options, and only with the significant
 * digits.
 *
 * @param s A pointer to the current mps_context.
 * @param f The float approximation that should be printed.
 * @param rad The current inclusion radius for that approximation.
 * @param out_digit The number of output digits required.
 * @param sign The sign of the approximation.
 */
MPS_PRIVATE void
mps_outfloat (mps_context * s, mpf_t f, rdpe_t rad, long out_digit,
              mps_boolean sign)
{
  mpf_t t;
  rdpe_t r, ro;
  double d;
  long l, digit, true_digit;

  if (s->output_config->format == MPS_OUTPUT_FORMAT_FULL)
    {
      mpf_init2 (t, mpf_get_prec (f));
      mpf_set (t, f);
      mpf_out_str (s->outstr, 10, 0, t);
      mpf_clear (t);
      return;
    }

  mpf_init2 (t, s->output_config->prec);

  mpf_get_rdpe (ro, f);
  if (s->output_config->format == MPS_OUTPUT_FORMAT_GNUPLOT ||
      s->output_config->format == MPS_OUTPUT_FORMAT_GNUPLOT_FULL)
    rdpe_out_str_u (s->outstr, ro);
  else
    {
      rdpe_abs_eq (ro);
      if (rdpe_ne (ro, rdpe_zero))
        rdpe_div (r, rad, ro);
      else
        rdpe_set_d (r, 1.0e-10);
      digit = (long)(-rdpe_log10 (r) - 0.5);
      if (digit <= 0)
        {
          rdpe_get_dl (&d, &l, ro);
          fprintf (s->outstr, "0.e%ld", l);
        }
      else
        {
          true_digit = (long)(LOG10_2 * mpf_get_prec (f));
          true_digit = MIN (digit, true_digit);
          true_digit = MIN (true_digit, out_digit);
          if (sign)
            mpf_set (t, f);
          else
            mpf_abs (t, f);
          mpf_out_str (s->outstr, 10, true_digit, t);
        }
    }

  mpf_clear (t);
}
Esempio n. 13
0
/* New calculate function. */
ordinal_number_t cache_calculator(render_t* handle,const view_position_t render_position)
{
	/* Volatile data. */
	ordinal_number_t* help;
	complex_number_t  complex_position;
	view_position_t   shift;
	real_number_t     scaling_factor;
	mpf_t             help_mpf;
	mpf_t             help_two;

	mpf_set_default_prec(sizeof(char)*handle->prec);
	mpf_init(help_mpf);
	mpf_init(Re(complex_position));
	mpf_init(Im(complex_position));
	mpf_init(scaling_factor);
	mpf_init(help_two);

	/* Check if the point has been calculated already. */
	help=handle->points+render_position.y*handle->geometry.width+render_position.x;
	if(*help==0)
	{
		/* Has not been calculated till now, calculate the iteration. */

		/* Precalculate scaling factor and center shift for speed reasons. */
		mpf_div_ui(scaling_factor,handle->scale,handle->geometry.width);
		shift.x=handle->geometry.width/2;
		shift.y=handle->geometry.height/2;

		/* Calculate the iteration. */
		mpf_set_si(help_two,(render_position.x-shift.x));
		mpf_mul(help_mpf,scaling_factor,help_two);
		mpf_add(complex_position.real_part,help_mpf,handle->center.real_part);

		mpf_set_si(help_two,(render_position.y-shift.y));
		mpf_mul(help_mpf,scaling_factor,help_two);
		mpf_sub(Im(complex_position),Im(handle->center),help_mpf);

		*help=(*handle->fractal_facility->facility.fractal.calculate_function)(handle->fractal,&complex_position);
	}

	mpf_clear(help_mpf);
	mpf_clear(Re(complex_position));
	mpf_clear(Im(complex_position));
	mpf_clear(scaling_factor);
	mpf_clear(help_two);
	

	/* Return the iteration. */
	return(*help);
	//return(0);
}
/*
 * Function:  compute_bbp_first_sum_gmp 
 * --------------------
 * Computes the first summand in the BBP formula using GNU GMP.
 *
 *  d: digit to be calculated
 *  base: the base
 *  c: a fixed positive integer
 *  p: a simple polynomial like x or x^2
 *  start_at_0: start the summation at k=0, if true, at k=1, otherwise.  Most 
 *              instances of the BBP formula, such as pi, have you start at 0.  
 *              But some, such as log(2), have you start at 1.
 *
 *  returns: the value of the first sum
 */
void compute_bbp_first_sum_gmp(mpf_t sum, unsigned long int d, int base, long int c, void (*p)(mpz_t, mpz_t), bool start_at_0) {
    mpf_set_d(sum, 0.0);
    signed long int k_start = start_at_0 ? 0 : 1;

    mpz_t k;
    mpz_init_set_si(k, k_start);
    double upper = floor((double) d / (double) c);
    while (mpz_cmp_d(k, upper) <= 0)
    {        
        mpz_t poly_result;
        mpz_init(poly_result);
        (*p)(poly_result, k);
        
        mpz_t num;
        mpz_init(num);
        
        mpz_t exponent;
        mpz_init_set(exponent, k);
        mpz_mul_si(exponent, exponent, c);
        mpz_mul_si(exponent, exponent, -1);
        mpz_add_ui(exponent, exponent, d);
        
        modular_pow_gmp(num, base, exponent, poly_result);
        mpf_t num_float;
        mpf_init(num_float);
        mpf_set_z(num_float, num);
        mpz_clear(num);
        mpz_clear(exponent);
        
        mpf_t denom;
        mpf_init_set_d(denom, mpz_get_d(poly_result));
        
        mpz_clear(poly_result);
        
        mpf_t quotient;
        mpf_init(quotient);
        mpf_div(quotient, num_float, denom);        
        mpf_clear(num_float);
        mpf_clear(denom);
        
        mpf_add(sum, sum, quotient);
        mpf_clear(quotient);
        
        mod_one_gmp(sum, sum);
        
        mpz_add_ui(k, k, 1);
    }
    mpz_clear(k);
        
    mod_one_gmp(sum, sum);
}
Esempio n. 15
0
void recalcZoom() {
	int32_t diffX = selectZoomW - selectZoomX;
	int32_t diffY = selectZoomH - selectZoomY;
	if(abs(diffX) >= 10 || abs(diffY) >= 10) {
		long double x = sizeX;
		long double y = sizeY;
		long double factorLeft = ((selectZoomX*100.0)/x)/100.0;
		long double factorRight = 1.0-((selectZoomW*100.0)/x)/100.0;
		long double factorBottom = 1.0-((selectZoomH*100.0)/y)/100.0;
		
		mpf_t facLeft;
		mpf_init2(facLeft,gmpBit);
		mpf_set_d(facLeft,factorLeft);
		mpf_t facRight;
		mpf_init2(facRight,gmpBit);
		mpf_set_d(facRight,factorRight);
		mpf_t facBottom;
		mpf_init2(facBottom,gmpBit);
		mpf_set_d(facBottom,factorBottom);
		mpf_t tmpDiff;
		mpf_init2(tmpDiff,gmpBit);
		mpf_t tmp;
		mpf_init2(tmp,gmpBit);
		
		//(maxRe - minRe)
		mpf_sub(tmpDiff,mandelRange.maxRe,mandelRange.minRe);
		
		//minRe+=(maxRe - minRe)*factorLeft;
		mpf_mul(tmp,tmpDiff,facLeft);
		mpf_add(mandelRange.minRe,mandelRange.minRe,tmp);
		
		//maxRe-=(maxRe - minRe)*factorRight;
		mpf_mul(tmp,tmpDiff,facRight);
		mpf_sub(mandelRange.maxRe,mandelRange.maxRe,tmp);
		
		
		//minIm+=(maxIm - minIm)*factorBottom;
		mpf_sub(tmpDiff,mandelRange.maxIm,mandelRange.minIm);
		mpf_mul(tmp,tmpDiff,facBottom);
		mpf_add(mandelRange.minIm,mandelRange.minIm,tmp);
		
		
		mpf_clear(tmp);
		mpf_clear(tmpDiff);
		mpf_clear(facLeft);
		mpf_clear(facRight);
		mpf_clear(facBottom);
		
		restart();
	}
}
Esempio n. 16
0
/* merit (rop, t, v, m) -- calculate merit for spectral test result in
   dimension T, see Knuth p. 105.  BUGS: Only valid for 2 <= T <=
   6. */
void
merit (mpf_t rop, unsigned int t, mpf_t v, mpz_t m)
{
  int f;
  mpf_t f_m, f_const, f_pi;

  mpf_init (f_m);
  mpf_set_z (f_m, m);
  mpf_init_set_d (f_const, M_PI);
  mpf_init_set_d (f_pi, M_PI);

  switch (t)
    {
    case 2:			/* PI */
      break;
    case 3:			/* PI * 4/3 */
      mpf_mul_ui (f_const, f_const, 4);
      mpf_div_ui (f_const, f_const, 3);
      break;
    case 4:			/* PI^2 * 1/2 */
      mpf_mul (f_const, f_const, f_pi);
      mpf_div_ui (f_const, f_const, 2);
      break;
    case 5:			/* PI^2 * 8/15 */
      mpf_mul (f_const, f_const, f_pi);
      mpf_mul_ui (f_const, f_const, 8);
      mpf_div_ui (f_const, f_const, 15);
      break;
    case 6:			/* PI^3 * 1/6 */
      mpf_mul (f_const, f_const, f_pi);
      mpf_mul (f_const, f_const, f_pi);
      mpf_div_ui (f_const, f_const, 6);
      break;
    default:
      fprintf (stderr,
	       "spect (merit): can't calculate merit for dimensions > 6\n");
      mpf_set_ui (f_const, 0);
      break;
    }

  /* rop = v^t */
  mpf_set (rop, v);
  for (f = 1; f < t; f++)
    mpf_mul (rop, rop, v);
  mpf_mul (rop, rop, f_const);
  mpf_div (rop, rop, f_m);

  mpf_clear (f_m);
  mpf_clear (f_const);
  mpf_clear (f_pi);
}
Esempio n. 17
0
void my_out_str_raw(FILE *fp, unsigned long digits, mpf_t f, unsigned long offset)
{
    unsigned long d;

    if (digits <= LINE_SIZE*NUM_BLOCKS) {
        unsigned long cursor = offset % LINE_SIZE;
        for (d = 0; d < digits; ) {
            mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1));
            mpf_mul_ui(f, f, UNIT_MOD);
            unsigned long i = mpf_get_ui(f);
            mpf_sub_ui(f, f, i);

            utoa(i, UNIT_SIZE);
            *out_ptr++ = ' ';
            d += UNIT_SIZE;
            cursor += UNIT_SIZE;
            if (cursor == LINE_SIZE) {
                cursor = 0;
                *out_ptr++ = ':';
                *out_ptr++ = ' ';
                utoa(offset + d, 0);
                *out_ptr++ = '\n';
                if ((offset + d) % (LINE_SIZE*10) == 0)
                    flush_out(fp);
            }
        }
    } else {
        mpf_t block, mod;
        unsigned long num_units = (digits + UNIT_SIZE-1)/UNIT_SIZE;
        unsigned long block_size =  (num_units + NUM_BLOCKS-1)/NUM_BLOCKS*UNIT_SIZE;
        mpf_set_default_prec((int)(block_size*BITS_PER_DIGIT+1));
        mpf_init(block);
        mpf_init_set_ui(mod, 10);
        mpf_pow_ui(mod, mod, block_size);

        for (d = 0; d < digits; d += block_size) {
            unsigned long size = block_size < digits - d ? block_size : digits - d;
            mpf_set_prec_raw(block, (int)(size*BITS_PER_DIGIT+1));
            mpf_set(block, f);
            my_out_str_raw(fp, size, block, offset+d);
            if (block_size < digits - d) {
                mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1));
                mpf_mul(f, f, mod);
                mpf_floor(trunk, f);
                mpf_sub(f, f, trunk);
            }
        }
        mpf_clear(block);
        mpf_clear(mod);
    }
}
Esempio n. 18
0
void
check_data (void)
{
  static const struct {
    long       x;
    mp_size_t  want_size;
    mp_limb_t  want_limb;
  } data[] = {

    {  0L,  0 },
    {  1L,  1, 1 },
    { -1L, -1, 1 },

    {  LONG_MAX,  1, LONG_MAX },
    { -LONG_MAX, -1, LONG_MAX },

    { LONG_HIGHBIT, -1, ULONG_HIGHBIT },
  };

  mpf_t  x;
  int    i;

  for (i = 0; i < numberof (data); i++)
    {
      mpf_init (x);
      mpf_set_si (x, data[i].x);
      MPF_CHECK_FORMAT (x);
      if (x->_mp_size != data[i].want_size
          || (x->_mp_size != 0
              && (x->_mp_d[0] != data[i].want_limb
                  || x->_mp_exp != 1)))
        {
          printf ("mpf_set_si wrong on data[%d]\n", i); 
          abort();                                    
        }
      mpf_clear (x);

      mpf_init_set_si (x, data[i].x);
      MPF_CHECK_FORMAT (x);
      if (x->_mp_size != data[i].want_size
          || (x->_mp_size != 0
              && (x->_mp_d[0] != data[i].want_limb
                  || x->_mp_exp != 1)))
        {
          printf ("mpf_init_set_si wrong on data[%d]\n", i); 
          abort();                                    
        }
      mpf_clear (x);
    }
}
Esempio n. 19
0
File: hilbert.c Progetto: blynn/pbc
static void mpc_cis(mpc_t res, mpf_t theta) {
  mpf_t a;

  mpf_init(a); mpf_set(a, theta);
  //res = exp(i a)
  //  = cos a + i sin a
  //converges quickly near the origin
  mpf_t f0;
  mpf_ptr rx = mpc_re(res), ry = mpc_im(res);
  int i;
  int toggle = 1;

  mpf_init(f0);

  mpf_set(f0, a);
  mpf_set_ui(rx, 1);
  mpf_set(ry, f0);
  i = 1;
  for(;;) {
    toggle = !toggle;
    i++;
    mpf_div_ui(f0, f0, i);
    mpf_mul(f0, f0, a);
    if (toggle) {
      mpf_add(rx, rx, f0);
    } else {
      mpf_sub(rx, rx, f0);
    }

    i++;
    mpf_div_ui(f0, f0, i);
    mpf_mul(f0, f0, a);

    if (toggle) {
      mpf_add(ry, ry, f0);
    } else {
      mpf_sub(ry, ry, f0);
    }

    if (mpf_sgn(f0) > 0) {
      if (mpf_cmp(f0, epsilon) < 0) break;
    } else {
      if (mpf_cmp(f0, negepsilon) > 0) break;
    }
  }

  mpf_clear(f0);
  mpf_clear(a);
}
Esempio n. 20
0
	void clear()
	{
		clear_vec_mp(checker_1_);
		clear_vec_mp(checker_2_);
		
		clear_mp(diff_);
		
		mpf_clear(abs_);
		mpf_clear(zerothresh_);
		
		for (int ii=0; ii<num_projections_; ii++) {
			clear_vec_mp(projections_[ii]);
		}
		free(projections_);
	}
Esempio n. 21
0
inline Mtbdd
readMPQAttribute(const TiXmlNode* node, const char* att)
{
    const std::string numberString = readStringAttribute(node, att);
    mpq_t gmp_value;
    mpq_init(gmp_value);
    try {
        size_t pos = 0;
        if ((pos = numberString.find('.')) != std::string::npos) {
            mpf_t f_value;
            mpf_init(f_value);
            mpf_set_str(f_value, numberString.c_str(), 10);
            mpq_set_f(gmp_value, f_value);
            mpf_clear(f_value);
        } else {
            mpq_set_str(gmp_value, numberString.c_str(), 10);
        }
        if (mpq_sgn(gmp_value) == 0) {
            mpq_clear(gmp_value);
            return mtbdd_false;
        }
        MTBDD res = mtbdd_gmp(gmp_value);
        mpq_clear(gmp_value);
        return res;
    } catch(boost::bad_lexical_cast&) {
        throw ParseError("[ERROR] String " + numberString + " is not a number");
    }
}
Esempio n. 22
0
void fmpz_poly_disc_gauss_rounding(fmpz_poly_t rop, const fmpq_poly_t x, const mpfr_t r_f, gmp_randstate_t randstate) {
  mpfr_t xi;  mpfr_init2(xi, mpfr_get_prec(r_f));
  mpf_t xi_f; mpf_init2(xi_f, mpfr_get_prec(r_f));
  mpq_t xi_q; mpq_init(xi_q);
  mpz_t s_z;  mpz_init(s_z);

  const long n = fmpq_poly_length(x);
  const size_t tau = (ceil(2*sqrt(log2((double)n))) > 3) ? ceil(2*sqrt(log2((double)n))) : 3;

  fmpz_poly_zero(rop);

  for(int i=0; i<n; i++) {
    fmpq_poly_get_coeff_mpq(xi_q, x, i);
    mpf_set_q(xi_f, xi_q);
    mpfr_set_f(xi, xi_f, MPFR_RNDN);

    dgs_disc_gauss_mp_t *D = dgs_disc_gauss_mp_init(r_f, xi, tau, DGS_DISC_GAUSS_UNIFORM_ONLINE);
    D->call(s_z, D, randstate);
    dgs_disc_gauss_mp_clear(D);

    fmpz_poly_set_coeff_mpz(rop, i, s_z);
  }
  mpz_clear(s_z);
  mpq_clear(xi_q);
  mpf_clear(xi_f);
  mpfr_clear(xi);
}
Esempio n. 23
0
// TODO: adjust sign
int mpf_gamma(mp_float * a, mp_float * b)
{
    int err;
    long oldeps, eps;
    mp_float t;

    err = MP_OKAY;

    oldeps = a->radix;
    eps = oldeps + MP_DIGIT_BIT;
    if ((err = mpf_init(&t, oldeps)) != MP_OKAY) {
	return err;
    }
    if ((err = mpf_copy(a, &t)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_normalize_to(&t, eps)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_lngamma(&t, &t)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_exp(&t, &t)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_normalize_to(&t, oldeps)) != MP_OKAY) {
	goto _ERR;
    }
    mpf_exch(&t, b);
_ERR:
    mpf_clear(&t);
    return err;
}
Esempio n. 24
0
void
check_various (void)
{
  mpf_t got;
  mpq_t q;

  mpf_init (got);
  mpq_init (q);

  /* 1/1 == 1 */
  mpf_set_prec (got, 20L);
  mpq_set_ui (q, 1L, 1L);
  mpf_set_q (got, q);
  MPF_CHECK_FORMAT (got);
  ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0);

  /* 1/(2^n+1), a case where truncating the divisor would be wrong */
  mpf_set_prec (got, 500L);
  mpq_set_ui (q, 1L, 1L);
  mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L);
  mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L);
  check_one (got, q);

  mpf_clear (got);
  mpq_clear (q);
}
Esempio n. 25
0
void
test_denorms (int prc)
{
#ifdef _GMP_IEEE_FLOATS
  double d1, d2;
  mpf_t f;
  int i;

  mpf_set_default_prec (prc);

  mpf_init (f);

  d1 = 1.9;
  for (i = 0; i < 820; i++)
    {
      mpf_set_d (f, d1);
      d2 = mpf_get_d (f);
      if (d1 != d2)
        abort ();
      d1 *= 0.4;
    }

  mpf_clear (f);
#endif
}
void
check_max (void)
{
  mpf_t  f;
  long   want;
  long   got;

  mpf_init2 (f, 200L);

#define CHECK_MAX(name)                                         \
  if (got != want)                                              \
    {                                                           \
      printf ("mpf_get_si wrong on %s\n", name);                \
      printf ("   f    ");                                      \
      mpf_out_str (stdout, 10, 0, f); printf (", hex ");        \
      mpf_out_str (stdout, 16, 0, f); printf ("\n");            \
      printf ("   got  %ld, hex %lX\n", got, got);              \
      printf ("   want %ld, hex %lX\n", want, want);            \
      abort();                                                  \
    }

  want = LONG_MAX;
  mpf_set_si (f, want);
  got = mpf_get_si (f);
  CHECK_MAX ("LONG_MAX");

  want = LONG_MIN;
  mpf_set_si (f, want);
  got = mpf_get_si (f);
  CHECK_MAX ("LONG_MIN");

  mpf_clear (f);
}
/**
 * Convert a Ruby function value into a long double
 *
 * Parameters::
 * * *iValBD* (_Rational_): The value to convert
 * Return::
 * * <em>long double</em>: The equivalent long double
 */
inline long double value2ld(
  VALUE iValBD) {
  long double rResult;

  mpf_t lDenominator;
  mpf_init_set_str(lDenominator, RSTRING_PTR(rb_funcall(rb_funcall(iValBD, gID_denominator, 0), gID_to_s, 0)), 10);
  mpf_t lDivResult;
  mpf_init_set_str(lDivResult, RSTRING_PTR(rb_funcall(rb_funcall(iValBD, gID_numerator, 0), gID_to_s, 0)), 10);
  mpf_div(lDivResult, lDivResult, lDenominator);
  // TODO: Find a way to round correctly lDivResult. It is currently truncated.
  rResult = mpf_get_d(lDivResult);
  mpf_clear(lDivResult);
  mpf_clear(lDenominator);

  return rResult;
}
Esempio n. 28
0
static void
mps_context_init (mps_context * s)
{
  mpf_t test;

  /* Set default streams */
  s->instr = stdin;
  s->outstr = stdout;
  s->logstr = stdout;

  /* Allocate space for the configurations */
  s->input_config = (mps_input_configuration*)mps_malloc (sizeof(mps_input_configuration));
  s->output_config = (mps_output_configuration*)mps_malloc (sizeof(mps_output_configuration));

  mps_set_default_values (s);

  /* Find minimum GMP supported precision */
  mpf_init2 (test, 1);
  s->minimum_gmp_precision = mpf_get_prec (test);
  mpf_clear (test);

  /* Set standard precision */
  s->output_config->prec = (int)(0.9 * DBL_DIG * LOG2_10);
  MPS_DEBUG (s, "Setting prec_out to %ld digits", s->output_config->prec);

  mps_mp_set_prec (s, DBL_DIG * LOG2_10 + 1);

  s->initialized = false;
  s->exit_required = false;
}
void
check_consistency (void)
{
  mpf_t  x;
  unsigned long  i, a, b;

  mpf_init (x);

  for (i = 1; i < 2000; i++)
    {
      mpf_set_prec (x, i);
      a = mpf_get_prec (x);
      mpf_set_prec (x, a);
      b = mpf_get_prec (x);
      if (a != b)
        {
          printf ("mpf_get_prec / mpf_set_prec inconsistent\n");
          printf ("   set %lu gives %lu, but then set %lu gives %lu\n",
                  i, a,
                  a, b);
          abort ();
        }
    }

  mpf_clear (x);
}
Esempio n. 30
-1
void w3j(mpf_t w, long j1, long j2, long j3, long m1, long m2, long m3)
{
  mpq_t delta_sq,r;
  mpz_t i;
  mpf_t h;

  mpq_init(delta_sq);
  mpq_init(r);
  mpz_init(i);
  mpf_init(h);
  mpq_set_si(r,0,1);

  if(m1+m2+m3!=0) return;
  if((iabs(m1)>j1) || (iabs(m2)>j2) || (iabs(m3)>j3)) return;
  if((j3<iabs(j1-j2)) || ((j1+j2)<j3)) return;

  w3j_Delta_sq(delta_sq, j1, j2, j3);
  w3j_intterm(i, j1, j2, j3, m1, m2, m3);
  if(iabs(j1-j2-m3)%2 == 1) mpz_neg(i,i);

  w3j_sqrt_sq(r, j1, j2, j3, m1, m2, m3);

  mpq_mul(r,r,delta_sq);
  mpf_set_q(w,r);
  mpf_sqrt(w,w);
  mpf_set_z(h,i);
  mpf_mul(w,w,h);

  mpf_clear(h);
  mpz_clear(i);
  mpq_clear(r);
  mpq_clear(delta_sq);
}