Example #1
0
File: hilbert.c Project: 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);
}
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);
}
	void FractalTexture::computeColor(unsigned char *pixel) {
#ifdef ROE_FRACTAL_GMP_USE_C
		mpf_set(xPos, m_xPos);
		mpf_set(yPos, m_yPos);
#else
		xPos = m_xPos;
		yPos = m_yPos;
#endif
		for(long iteration = 0; iteration < m_iMaxIterations; ++iteration) {
#ifdef ROE_FRACTAL_GMP_USE_C
			mpf_mul(xPos2, xPos, xPos);
			mpf_mul(yPos2, yPos, yPos);
			mpf_add(tmp, xPos2, yPos2); //save sum temporarily
			if(mpf_cmp_ui(tmp,4) >= 0) {
#else
				xPos2 = xPos*xPos;
				yPos2 = yPos*yPos;
				if(xPos2 + yPos2 > 4.0) {
#endif
					//(coloured) outer
					const double ratio = iteration*m_dInvMaxIterations;
					//const Color c = Color::WHITE;
					const Color c = colorInterpolation.interpolate(ratio);
					pixel[0] = c.byter();
					pixel[1] = c.byteg();
					pixel[2] = c.byteb();
					return;
				}
#ifdef ROE_FRACTAL_GMP_USE_C
				mpf_mul(yPos, yPos, xPos);
				mpf_mul_ui(yPos, yPos, 2);
				mpf_add(yPos, yPos, m_yPos);
				mpf_sub(xPos, xPos2, yPos2);
				mpf_add(xPos, xPos, m_xPos);
#else
				yPos *= xPos;
				yPos *= 2;
				yPos += m_yPos;
				xPos = xPos2;
				xPos -= yPos2;
				xPos += m_xPos;
#endif
			}
			//inner
			pixel[0] = m_innerR;
			pixel[1] = m_innerG;
			pixel[2] = m_innerB;
		}
		void FractalTexture::updateMipmaps() {
			S_Texture *subtex1 = &texture, *subtex2 = nullptr;
			while (subtex1->next_mipmap) {
				subtex2 = subtex1->next_mipmap;
				Texture::scaleImage2(subtex1->data, subtex1->width, subtex1->height, subtex2->data, subtex2->width, subtex2->height, subtex1->bpp); //scaling image to new size
				subtex1 = subtex2; //one level deeper
			}
		}
Example #4
0
void precision_change_gmp(mpf_t x, mp_bitcnt_t p)
{
    mpf_t tmp;

    mpf_init2(tmp, p);
    mpf_set(tmp,   x);
    mpf_set_prec(  x,      p);
    mpf_set(       x,      tmp);

    mpf_clear(tmp);
}
Example #5
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);
}
Example #6
0
mpf_class gregoryLeibniz(int times){
  mpf_class pi (128), realpi (128);
  mpf_set(pi, 0);
  mpf_set(realpi,atan(1)*4);
  for (int a =0; a<times; a++){
    mpf_add(pi,(pow(-1,a))/(2*a+1));
    if (!(a%(times/100))) {
      std::cout<<pi*4<<"\n";
      std::cout<<pi*4-realpi<<"\n";
    }
  }
  return pi*4;
}
/* Constructor and destructor for recurse render function. */
static render_t* constructor(
		const complex_number_t   center,
		const view_dimension_t   geometry,
		const real_number_t      scale,
		const plugin_facility_t* fractal_facility,
		const plugin_facility_t* output_facility,
		const void*              fractal,
		const void*              output,
		const char               args[],
		long long int            prec)
{
	render_t*        context;

	/* Check if parameter was given, */
	if(args==NULL)
	{
		fprintf(stderr,"You have to specify render parameters\n");
		exit(EXIT_FAILURE);
	}

	#ifdef DEBUG
	fprintf(stderr,"Render parameter is: %s\n",args);
	#endif
	
	/* Get memory for the fractal context. */
	if (!(context=malloc(sizeof(render_t)))) return NULL;

	
	VARCOPY(context->prec,prec);
	mpf_set_default_prec(sizeof(char)*prec);
	
		
	/* Set the fractal context. */
	mpf_init(context->center.real_part);
	mpf_init(context->center.imaginary_part);
	mpf_init(context->scale);

	mpf_set(context->center.real_part,Re(center));
	mpf_set(context->center.imaginary_part,Im(center));
	VARCOPY(context->geometry,geometry);
	mpf_set(context->scale,scale);
	VARCOPY(context->fractal_facility,fractal_facility);
	VARCOPY(context->output_facility,output_facility);
	VARCOPY(context->fractal,fractal);
	VARCOPY(context->output,output);
	sscanf(args,"%d", &context->param);

	/* Return the handle. */
	return context;
}
Example #8
0
File: hilbert.c Project: 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);
}
Example #9
0
void copyReal(Real src, Real dest){
  #ifdef USE_MPFR
  mpfr_set(dest->mpfr_val, src->mpfr_val, MPFR_RNDN);
  #else
  mpf_set(dest->mpf_val, src->mpf_val);
  #endif
}
Example #10
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);
}
Example #11
0
void DoUniaryOperation(number_t result, number_t number1, char *op)
{
	switch( op[0] )
	{
		case '!':
			mpf_set_si(result, mpf_cmp_ui(number1, 0));
			break;
		case '~':
			DO_INTEGER_OPERATION1_ON_FLOAT(mpz_com, result, number1);
			break;
		case '+':
			if (op[1] == '+') {
				mpf_add_ui(result, number1, 1);
			} else {
				mpf_set(result, number1);
			}
			break;
		case '-':
			if (op[1] == '-') {
				mpf_sub_ui(result, number1, 1);
			} else {
				mpf_sub(result, result, number1);
			}
			break;
	}
}
Example #12
0
void AssignValueToIdentifier(char *Id, number_t value)
{
	symbol_value_t *s;
	
	s = (symbol_value_t *)symtab->get(symtab, (void *)Id);
	if (s) {
		mpf_init(s->value);
		mpf_set(s->value, value);
	} else {
		s = (symbol_value_t *)malloc(sizeof(symbol_value_t));
		mpf_init(s->value);
		mpf_set(s->value, value);
		symtab->put(symtab, Id, s, NULL);
	}
	mpf_set(*fExpressionResult, value);
}
Example #13
0
/* Don't want to change the precision of w, can only do an actual swap when
   w and x have the same precision.  */
static void
e_mpf_set_or_swap (mpf_ptr w, mpf_ptr x)
{
  if (mpf_get_prec (w) == mpf_get_prec (x))
    mpf_swap (w, x);
  else
    mpf_set (w, x);
}
Example #14
0
int sg_big_float_set_big_float(sg_big_float_t *dst, sg_big_float_t *src)
{
    if (!dst || !src)
        return -1;

    mpf_set(dst->mpf, src->mpf);
    return 0;
}
Example #15
0
int main(void) {


  int i ;
  char buf[4096] ;
  mpf_t x, y , result ;
  mpf_set_default_prec(LIMIT);
  mpf_init ( x );
  mpf_init ( y );
  mpf_init (result) ;




   mpf_set_str(result, "1" , 10 );

   for ( i = 1 ; i < LIMIT ; i++ ) {
     sprintf ( buf , "%d" , (2*i + 1) ) ;
     mpf_set_str(x, buf , 10 );
     mpf_ui_div (y, 1, x) ;
     if ( i % 2 ) {
       mpf_sub ( x , result , y ) ;
       mpf_set (result , x) ;
     }
     else {
       mpf_add ( x , result , y ) ;
       mpf_set (result , x) ;
     }
   }

   mpf_mul_ui (x, result, 4 )  ;


   printf("\n pi = " ) ;
   mpf_out_str (stdout , 10, 0, x) ;
   printf ("\n") ;


   mpf_clear (x);
   mpf_clear (y);
   mpf_clear (result);

   return EXIT_SUCCESS;

}
Example #16
0
void AssignValueFromIdentifier(number_t result, char *Id)
{
	symbol_value_t *s;
	
	s = (symbol_value_t *)symtab->get(symtab, (void *)Id);
	if (s) {
		mpf_set(result, s->value);
	}
}
Example #17
0
static void newton(mpf_t x, double seed,
                   fun_3term *fun, fun_3term *der, int n)
{
  DECLARE_3VARS(ox,f,df);
  mpf_set_d(x, seed);
  do {
    mpf_set(ox, x);
    fun(f, n,x), der(df, n,x), mpf_div(f, f,df), mpf_sub(x, x,f);
  } while(!is_small(f,x));
  fun( f, n,x), der(df, n,x), mpf_div(f, f,df), mpf_sub(x, x,f);
}
Example #18
0
void
mpf_pow_ui (mpf_ptr r, mpf_srcptr b, unsigned long int e)
{
  mpf_t b2;
  unsigned long int e2;

  mpf_init2 (b2, mpf_get_prec (r));
  mpf_set (b2, b);
  mpf_set_ui (r, 1);

  if ((e & 1) != 0)
    mpf_set (r, b2);
  for (e2 = e >> 1; e2 != 0; e2 >>= 1)
    {
      mpf_mul (b2, b2, b2);
      if ((e2 & 1) != 0)
	mpf_mul (r, r, b2);
    }

  mpf_clear (b2);
}
Example #19
0
void
mpf_pow_ui (mpf_ptr r, mpf_srcptr b, unsigned long int e)
{
  mpf_t b2;

  mpf_init2 (b2, mpf_get_prec (r));
  mpf_set (b2, b);

  if ((e & 1) != 0)
    mpf_set (r, b);
  else
    mpf_set_ui (r, 1);
  while (e >>= 1)
    {
      mpf_mul (b2, b2, b2);
      if ((e & 1) != 0)
	mpf_mul (r, r, b2);
    }

  mpf_clear (b2);
}
Example #20
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);
}
Example #21
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);
    }
}
Example #22
0
static void
Pks (mpf_t p, mpf_t x)
{
  double dt;			/* temp double */

  mpf_set (p, x);
  mpf_mul (p, p, p);		/* p = x^2 */
  mpf_mul_ui (p, p, 2);		/* p = 2*x^2 */
  mpf_neg (p, p);		/* p = -2*x^2 */
  /* No pow() in gmp.  Use doubles. */
  /* FIXME: Use exp()? */
  dt = pow (M_E, mpf_get_d (p));
  mpf_set_d (p, dt);
  mpf_ui_sub (p, 1, p);
}
Example #23
0
void UniEvalF(mpf_ptr r, poly_f & f, mpf_ptr x)
{
	uint i=f.size()-1;
	mpf_set(r,f[i]);
	if(i==0)return ;
	--i;
	while(1)
	{
		mpf_mul(r,r,x);
		mpf_add(r,r,f[i]);
		if(i==0)break;
		--i;
	}
	return ;
}
Example #24
0
libmaus2::math::GmpFloat & libmaus2::math::GmpFloat::operator=(
	GmpFloat const &
	#if defined(LIBMAUS2_HAVE_GMP)
		o
	#endif
)
{
	#if defined(LIBMAUS2_HAVE_GMP)
	if ( this != &o )
	{
		mpf_set_prec(decode(v),mpf_get_prec(decode(o.v)));
		mpf_set(decode(v),decode(o.v));
	}
	#endif
	return *this;
}
Example #25
0
void
check_reuse (void)
{
  /* Try mpf_set(f,f) when f is bigger than prec.  In the past this had
     resulted in an MPN_COPY with invalid operand overlap. */
  mpf_t  f;
  mp_size_t      limbs = 20;
  unsigned long  bits = limbs * GMP_NUMB_BITS;
  mpf_init2 (f, bits);
  refmpf_fill (f, limbs, GMP_NUMB_MAX);
  mpf_set_prec_raw (f, bits / 2);
  mpf_set (f, f);
  MPF_CHECK_FORMAT (f);
  mpf_set_prec_raw (f, bits);
  mpf_clear (f);
}
Example #26
0
void
mpf_sub_ui (mpf_ptr sum, mpf_srcptr u, unsigned long int v)
{
  __mpf_struct vv;
  mp_limb_t vl;

  if (v == 0)
    {
      mpf_set (sum, u);
      return;
    }

  vl = v;
  vv._mp_size = 1;
  vv._mp_d = &vl;
  vv._mp_exp = 1;
  mpf_sub (sum, u, &vv);
}
/**
 * void initAttributes()
 * 
 * Descricao:
 * 	Inicializa as variaveis globas e atribui os valores iniciais necessarios para a execucao do algoritmo
 * 	Gauss-Legendre.
 * 
 * Parametros de entrada:
 * 	-
 * 
 * Parametros de retorno:
 * 	-
 */
void initAttributes(){
  
  /* Atribui como precisao default 10 milhoes de casas decimais. A funcao mpf_set_default_prec() tem como parametro de entrada
   * a precisao desejada em bits, ou seja, e necessario utilizar a conversao (log de 2 na base 10)*10 milhoes para obter o valor correto. */
  mpf_set_default_prec((log(10)/log(2))*10000000);

  int i;
  mpf_t sqrt2; 	// Variavel auxiliar para o calculo de b0    
  mpf_t b0;  	// Variavel auxiliar para o calculo de b0
  
  n_count = 0;

  /* Arrays de variaveis utilizadas pelo algoritmo. Como o algoritmo converge para o valor correto do "pi" com 45 milhoes de casas decimais
   * em apenas 25 iteracoes, serao utilizado arrays com 25 posicoes */
  a_n = (mpf_t*) malloc(25*sizeof(mpf_t));
  b_n = (mpf_t*) malloc(25*sizeof(mpf_t));
  t_n = (mpf_t*) malloc(25*sizeof(mpf_t));
  p_n = (mpf_t*) malloc(25*sizeof(mpf_t));
  pi = (mpf_t*) malloc(25*sizeof(mpf_t));  
  
  for(i=0; i<25; i++){
    mpf_init(a_n[i]);
    mpf_init(b_n[i]);
    mpf_init(p_n[i]);
    mpf_init(t_n[i]);
    mpf_init(pi[i]);    
  }
  
  /* Atribuicao dos valores iniciais */
  mpf_init(sqrt2);
  mpf_init(b0);
  mpf_sqrt_ui(sqrt2, 2);
  mpf_ui_div(b0, 1, sqrt2);  
  
  mpf_set_d(a_n[n_count], 1.0);
  mpf_set(b_n[n_count], b0);
  mpf_set_d(t_n[n_count], 0.25);
  mpf_set_d(p_n[n_count], 1.0);
  
}
Example #28
0
int
main (int argc, char **argv)
{
  double d, e, r;
  mpf_t u, v;

  tests_start ();
  mpf_init (u);
  mpf_init (v);

  mpf_set_d (u, LOW_BOUND);
  for (d = 2.0 * LOW_BOUND; d < HIGH_BOUND; d *= 1.01)
    {
      mpf_set_d (v, d);
      if (mpf_cmp (u, v) >= 0)
	abort ();
      e = mpf_get_d (v);
      r = e/d;
      if (r < 0.99999999999999 || r > 1.00000000000001)
	{
	  fprintf (stderr, "should be one ulp from 1: %.16f\n", r);
	  abort ();
	}
      mpf_set (u, v);
    }

  mpf_clear (u);
  mpf_clear (v);

  test_denorms (10);
  test_denorms (32);
  test_denorms (64);
  test_denorms (100);
  test_denorms (200);

  tests_end ();
  exit (0);
}
Example #29
0
  void log10(const mpf_t n, mpf_t l, int prec) {
    // Approximate log10 to prec digits.
    // Ref.: http://www.brics.dk/RS/04/17/BRICS-RS-04-17.pdf
    mpf_t tmp, tmp2;
    mpf_inits(tmp, tmp2, NULL);
    mpf_set(tmp, n);

    stringstream res;
    for (int i = 0; i < prec + 1; i++) { 
      int d = digits(tmp);

      // m_(i+1) = (m_i / (10 ^ a_i)) ^ 10 
      mpf_set_ui(tmp2, 10);
      mpf_pow_ui(tmp2, tmp2, d);
      mpf_div(tmp2, tmp, tmp2);
      mpf_pow_ui(tmp, tmp2, 10);

      res << d;      
      if (i == 0) res << ".";
    }

    mpf_set_str(l, res.str().c_str(), 10);
    mpf_clears(tmp, tmp2, NULL);
  }
Example #30
0
File: omega.c Project: ramos/omega
void 
omega(long int n, long int m, double tau, long int q, 
			  long int k1, long int k2, mpf_t *factoriales, mpf_t pi) {

  mpf_t aux, aux2, aux3, sqrf, acum, Ltau, z, zelev;
  int j;
  unsigned long int qsqr;

  /* 
	* Set n = min(n,m), and m = max(n,m)
	*/
  j = n;
  n = min(n,m);
  m = max(j,m);
	 
  /*
	* The sqrt(n!m!) pre-factor
	*/
  mpf_init(aux);
  mpf_init(sqrf);
  mpf_mul(aux, factoriales[n], factoriales[m]);
  mpf_sqrt(sqrf, aux);
  
  /*
	* Calculus of tau
	*/
  mpf_init(aux2);
  mpf_init_set_d(Ltau, tau);
  mpf_init(z);
  mpf_init(zelev);
  qsqr = (unsigned long int) q;
  
  
  mpf_set_d(aux, (double) pow((double) k1, (double) 2));
  mpf_mul(aux, aux, Ltau);
  
  mpf_set_d(aux2, (double) pow((double) k2, (double) 2));
  mpf_div(aux, aux, Ltau);

  mpf_add(aux, aux, aux2);
  mpf_div_ui(aux, aux, qsqr);

  mpf_mul(z, aux, pi);

  

  if ( ((m-n)%2) == 0)
	 mpf_pow_ui(zelev, z, (unsigned long int) (m-n)/2);
  else {
	 mpf_pow_ui(zelev, z, m-n);
	 mpf_sqrt(zelev, zelev);
  } 
	 

  /*  mpf_pow_ui(zelev, z, m-n);
	*mpf_pow_ui(z, z, 2);
	*/
  /*  mpf_out_str(stdout, 10, 20, z);
		printf("\n");*/
  /* 
	* The loop
	*/
  mpf_init(acum);
  mpf_init(aux3);
  mpf_set_ui(acum, (unsigned long int) 0); 

  for (j=0; j <= n; j++) {
	 mpf_mul(aux, factoriales[j], factoriales[n-j]);
	 mpf_mul(aux2, aux, factoriales[j+m-n]);

	 mpf_pow_ui(aux3, z, j);
	 mpf_div(aux, aux3, aux2);
	 if ((j%2) == 0) 
		mpf_set(aux2, aux);
	 else
		mpf_neg(aux2, aux);
	 
	 mpf_add(acum, acum, aux2);

  }
  


  mpf_mul(aux, acum, sqrf);
  mpf_mul(aux, aux, zelev);

  gmp_printf("%4d %4d %4d %4d %20.20Fe\n", n, m, k1, k2, aux);
  /*mpf_out_str(stdout, 10, 20, aux);*/

}