예제 #1
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();
	}
}
예제 #2
0
파일: my.c 프로젝트: macroxue/const-pi
// r = 1/sqrt(x)
void my_invsqrt_ui(mpf_t r, unsigned long x)
{
    unsigned long prec, bits, prec0;

    prec0 = mpf_get_prec(r);

    if (prec0<=DOUBLE_PREC) {
        mpf_set_d(r, sqrt(x));
        return;
    }

    bits = 0;
    for (prec=prec0; prec>DOUBLE_PREC;) {
        int bit = prec&1;
        prec = (prec+bit)/2;
        bits = bits*2+bit;
    }

    mpf_set_prec_raw(t1, DOUBLE_PREC);
    mpf_set_d(t1, 1/sqrt(x));

    while (prec<prec0) {
        prec *=2;
        if (prec<prec0) {
            /* t1 = t1+t1*(1-x*t1*t1)/2; */
            mpf_set_prec_raw(t2, prec);
            mpf_mul(t2, t1, t1);         // half x half -> full
            mpf_mul_ui(t2, t2, x);
            mpf_ui_sub(t2, 1, t2);
            mpf_set_prec_raw(t2, prec/2);
            mpf_div_2exp(t2, t2, 1);
            mpf_mul(t2, t2, t1);         // half x half -> half
            mpf_set_prec_raw(t1, prec);
            mpf_add(t1, t1, t2);
        } else {
            prec = prec0;
            mpf_set_prec_raw(t2, prec);
            mpf_mul(t2, t1, t1);         // half x half -> full
            mpf_mul_ui(t2, t2, x);
            mpf_ui_sub(t2, 1, t2);
            mpf_set_prec_raw(t2, prec/2);
            mpf_div_2exp(t2, t2, 1);
            mpf_mul(t2, t2, t1);         // half x half -> half
            mpf_add(r, t1, t2);
            break;
        }
        prec -= (bits&1);
        bits /=2;
    }
}
예제 #3
0
파일: hilbert.c 프로젝트: 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);
}
예제 #4
0
파일: floatbasic.cpp 프로젝트: AnZhg/mU
void mpfc_div(mpfc_ptr r,mpfc_ptr x,mpfc_ptr y)
{
	static mpfc_t z;
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Re);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Im);
	mpf_add(mpfc_mpf_temp[2],mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Im);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Re);
	mpf_sub(mpfc_mpf_temp[3],mpfc_mpf_temp[1],mpfc_mpf_temp[0]);
	mpf_mul(mpfc_mpf_temp[0],x->Re,x->Re);
	mpf_mul(mpfc_mpf_temp[1],x->Im,x->Im);
	mpf_add(mpfc_mpf_temp[0],mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
	mpf_div(r->Re,mpfc_mpf_temp[2],mpfc_mpf_temp[0]);
	mpf_div(r->Im,mpfc_mpf_temp[3],mpfc_mpf_temp[0]);
}
// r = y/x   WARNING: r cannot be the same as y.
void
my_div(mpf_t r, mpf_t y, mpf_t x)
{
  unsigned long prec, bits, prec0;

  prec0 = mpf_get_prec(r);

  if (prec0<=DOUBLE_PREC) {
    mpf_set_d(r, mpf_get_d(y)/mpf_get_d(x));
    return;
  }

  bits = 0;
  for (prec=prec0; prec>DOUBLE_PREC;) {
    int bit = prec&1;
    prec = (prec+bit)/2;
    bits = bits*2+bit;
  }

  mpf_set_prec_raw(t1, DOUBLE_PREC);
  mpf_ui_div(t1, 1, x);

  while (prec<prec0) {
    prec *=2;
    if (prec<prec0) {
      /* t1 = t1+t1*(1-x*t1); */
      mpf_set_prec_raw(t2, prec);
      mpf_mul(t2, x, t1);          // full x half -> full
      mpf_ui_sub(t2, 1, t2);
      mpf_set_prec_raw(t2, prec/2);
      mpf_mul(t2, t2, t1);         // half x half -> half
      mpf_set_prec_raw(t1, prec);
      mpf_add(t1, t1, t2);
    } else {
      prec = prec0;
      /* t2=y*t1, t1 = t2+t1*(y-x*t2); */
      mpf_set_prec_raw(t2, prec/2);
      mpf_mul(t2, t1, y);          // half x half -> half
      mpf_mul(r, x, t2);           // full x half -> full
      mpf_sub(r, y, r);
      mpf_mul(t1, t1, r);          // half x half -> half
      mpf_add(r, t1, t2);
      break;
    }
    prec -= (bits&1);
    bits /=2;
  }
}
예제 #6
0
vanilla::object::ptr vanilla::int_object::add(object::ptr const& other)
{
    switch(other->type_id())
    {
        case OBJECT_ID_INT:
        {
            int_object const* rhs = static_cast<int_object const*>(other.get());
            int_type result;
            mpz_add(result.mpz(), _v.mpz(), rhs->value().mpz());
            return allocate_object<int_object>(std::move(result));
        }
        
        case OBJECT_ID_FLOAT:
        {
            float_object::float_type lhs( (_v.mpz()) );
            float_object const* rhs = static_cast<float_object const*>(other.get());
            float_object::float_type result;
            mpf_add(result.mpf(), lhs.mpf(), rhs->value().mpf());
            return allocate_object<float_object>(std::move(result));
        }
        
        default:
        {
            return object::add(other);
        }
    }
}
예제 #7
0
파일: hilbert.c 프로젝트: blynn/pbc
static void precision_init(int prec) {
  int i;
  mpf_t f0;

  mpf_set_default_prec(prec);
  mpf_init2(epsilon, 2);
  mpf_init2(negepsilon, 2);
  mpf_init(recipeulere);
  mpf_init(pi);
  mpf_init(eulere);

  mpf_set_ui(epsilon, 1);
  mpf_div_2exp(epsilon, epsilon, prec);
  mpf_neg(negepsilon, epsilon);

  mpf_init(f0);
  mpf_set_ui(eulere, 1);
  mpf_set_ui(f0, 1);
  for (i=1;; i++) {
    mpf_div_ui(f0, f0, i);
    if (mpf_cmp(f0, epsilon) < 0) {
      break;
    }
    mpf_add(eulere, eulere, f0);
  }
  mpf_clear(f0);

  mpf_ui_div(recipeulere, 1, eulere);

  compute_pi(prec);
}
예제 #8
0
파일: hilbert.c 프로젝트: 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);
}
예제 #9
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;
}
예제 #10
0
	float evaluateFitness(float position[])
	{
		char stringFitness[28];
		mpf_t mpfFitness, one;
		float ris;
		float fitness = formulae(position);
		if (fitness >= 0) 
		{
			sprintf(stringFitness, "%.20f", fitness);
			mpf_init(mpfFitness);
			mpf_init(one);
			
			mpf_set_str (one, "1.0", 10);
			mpf_set_str(mpfFitness, stringFitness, 10);
			
			mpf_add(mpfFitness, mpfFitness, one);
			//printf("fitness: %.20e\t", fitness);
			//mpf_out_str (stdout, 10, 256, mpfFitness);
			mpf_div(mpfFitness, one, mpfFitness);			
			
			printf("\nris %.20e\t", (float) mpf_get_d(mpfFitness));
			ris = (float) mpf_get_d(mpfFitness);
			mpf_out_str (stdout, 10, 256, mpfFitness);
			printf("\n");
			return ris;
			//return 1 / (1 + fitness);
		}
		return 1 + fabs(fitness);
	}
예제 #11
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);
}
예제 #12
0
	void FractalTexture::create() {
		if(glIsTexture(texture.id)) glDeleteTextures(1, &texture.id); //cleaning gl memory up
		m_iMaxIterations = (long)((double)m_iStdMaxIterations * 1.0); //todo?
		m_dInvMaxIterations = 1.0 / (m_iMaxIterations+0.0001);
		const double winv = 1.0/(texture.width -1);
		const double hinv = 1.0/(texture.height-1);
		util::timeDiff();
		for (int x = 0; x < texture.width; ++x) {
			for (int y = 0; y < texture.height; ++y) {
#ifdef ROE_FRACTAL_GMP_USE_C
				mpf_set_d(m_xPos, x*winv);
				mpf_set_d(m_yPos, y*hinv);
				mpf_mul(m_xPos, m_xPos, m_width);
				mpf_mul(m_yPos, m_yPos, m_height);
				mpf_add(m_xPos, m_xUpLt, m_xPos);
				mpf_sub(m_yPos, m_yUpLt, m_yPos);
#else
				m_xPos = m_xUpLt + (x*winv)*m_width;
				m_yPos = m_yUpLt - (y*hinv)*m_height;
#endif
				computeColor(&(texture.data[((texture.height-1-y)*texture.width+x)*texture.bpp]));
			}
		}
		cout << "dt: " << util::timeDiff() << endl;
		cout << getCenterX() << endl;
		cout << getCenterY() << endl;
		cout << getZoomW() << endl;
		updateMipmaps();
		Texture::loadTextureIntoGL(&texture);
	}
예제 #13
0
		void FractalTexture::move(double x, double y) {
			x *= 0.5;
			y *= 0.5;
#ifdef ROE_FRACTAL_GMP_USE_C
			mpf_set_d(xPos, x);
			mpf_set_d(yPos, y);
			mpf_mul(xPos, m_width, xPos);
			mpf_mul(yPos, m_width, yPos);
			mpf_add(m_xCenter, m_xCenter, xPos);
			mpf_add(m_yCenter, m_yCenter, yPos);
#else
			m_xCenter += x*m_width;
			m_yCenter += y*m_height;
#endif
			updateCorners();
		}
예제 #14
0
파일: floatbasic.cpp 프로젝트: AnZhg/mU
void mpfc_abs(mpf_t r,mpfc_ptr x)
{
	mpf_mul(mpfc_mpf_temp[0],x->Re,x->Re);
	mpf_mul(mpfc_mpf_temp[1],x->Im,x->Im);
	mpf_add(r,mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
	mpf_sqrt(r,r);
}
/*TODO verificar se a melhor maneira de proceder com o cálculo abaixo*/
void *calc_a(thr_gmpf_t *vars)
{
    /*vars.a1 = vars.a0*pow(1 + vars.y1,4) - pow(2,2*k+3)*vars.y1*(1+vars.y1+pow(vars.y1,2));*/

    /*c = y1+1;*/
    mpf_add_ui(vars->aux_a1,vars->y_valoratual,1);

    mpf_pow_ui(vars->a1,vars->aux_a1,4); /*a1 = c^4*/
    mpf_mul(vars->a1,vars->a0,vars->a1); /*a1 = a0*a1*/

    mpf_pow_ui(vars->aux_a2,vars->y_valoratual,2); /*aux2 = pow(y_valoratual,2)*/
    mpf_add(vars->aux_a2,vars->aux_a2,vars->aux_a1); /*aux2 += c*/
    mpf_mul(vars->aux_a2,vars->aux_a2,vars->y_valoratual); /*vars->aux2 *= vars->y_valoratual*/


    /*continua os cálculos de a que não dependem de y*/
    /*o cálculo abaixo pode ser feito paralelamente*/
    mpf_set_ui(vars->aux_a1,2); /* aux1=2 */
    mpf_pow_ui(vars->aux_a1,vars->aux_a1,2*vars->k+3); /* aux_a1 = pow(aux_a1,2*k+3)*/

    mpf_mul(vars->aux_a1,vars->aux_a1,vars->aux_a2); /*vars->aux1 = vars->aux1*vars->aux2*/

    mpf_sub(vars->a1,vars->a1,vars->aux_a1);

    vars->k = vars->k+1;

    pthread_exit(NULL);
    return NULL;
}
예제 #16
0
// The Brent-Salamin algorithm
int main(int argc, char* argv[]) {
  if (argc < 2) return -1;
  int n = (int)strtol(argv[1], NULL, 10);
  mpf_set_default_prec(1000);
  mpf_t a, b, t, c, sum;
  // a=1
  mpf_init_set_ui(a, 1);
  mpf_init_set_ui(sum, 0);
  mpf_init(b);
  mpf_init(t);
  mpf_init(c);
  mpf_init(sum);

  // b=1/sqrt(2)
  mpf_sqrt_ui(b, 2);
  mpf_ui_div(b, 1, b);

  // n次迭代的误差小于\frac{2^{n+9}}{20^{2n+1}}
  for (int i = 1; i <= n; ++i) {
    // t=(a+b)/2
    mpf_add(t, a, b);
    mpf_div_ui(t, t, 2);
    // b=sqrt(a*b);
    mpf_mul(b, a, b);
    mpf_sqrt(b, b);
    // a=t
    mpf_swap(t, a);
    mpf_mul(t, a, a);
    mpf_mul(c, b, b);
    mpf_sub(c, t, c);
    mpf_mul_2exp(c, c, i + 1);
    mpf_add(sum, sum, c);
  }
  mpf_mul(t, a, a);
  mpf_mul_ui(t, t, 4);
  mpf_ui_sub(sum, 1, sum);
  mpf_div(t, t, sum);
  mpf_out_str(stdout, 10, 0, t);
  printf("\n");
  mpf_clear(a);
  mpf_clear(b);
  mpf_clear(t);
  mpf_clear(c);
  mpf_clear(sum);
  return 0;
}
예제 #17
0
int sg_big_float_add(sg_big_float_t *a, sg_big_float_t *b, sg_big_float_t *res)
{
    if (!a || !b || !res)
        return -1;

    mpf_add(res->mpf, a->mpf, b->mpf);
    return 0;
}
void *thread2(void *param)
{
	mpf_add(x, sqrtx, invsqrtx);
	mpf_div_ui(x, x, 2);
	mpf_add_ui(aux1, x, 1);
	mpf_mul(aux1 ,p, aux1);
	pthread_exit(0);
}
/**
 * void calculate_a()
 * 
 * Descricao:
 * 	Calcula o valor do "pi" na n-esima iteracao.
 * 
 * Parametros de entrada:
 * 	-
 * 
 * Parametros de retorno:
 * 	-
 */
void calculate_pi(){
  
    mpf_add(pi[n_count], a_n[n_count+1], b_n[n_count+1]);
    mpf_pow_ui(pi[n_count], pi[n_count], 2);    
    mpf_div_ui(pi[n_count], pi[n_count], 4);
    mpf_div(pi[n_count], pi[n_count], t_n[n_count+1]); 
    
}
예제 #20
0
파일: floatbasic.cpp 프로젝트: AnZhg/mU
void mpfc_mul(mpfc_ptr r,mpfc_ptr x,mpfc_ptr y)
{
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Re);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Im);
	mpf_sub(r->Re,mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Im);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Re);
	mpf_add(r->Im,mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
}
예제 #21
0
		void FractalTexture::updateCorners() {
#ifdef ROE_FRACTAL_GMP_USE_C
			mpf_div_ui(xPos, m_width , 2);
			mpf_div_ui(yPos, m_height, 2);
			mpf_sub(m_xUpLt, m_xCenter, xPos);
			mpf_add(m_yUpLt, m_yCenter, yPos);
#else
			m_xUpLt = m_xCenter-0.5*m_width;
			m_yUpLt = m_yCenter+0.5*m_height;
#endif
		}
예제 #22
0
libmaus2::math::GmpFloat & libmaus2::math::GmpFloat::operator+=(
	GmpFloat const &
		#if defined(LIBMAUS2_HAVE_GMP)
		o
		#endif
)
{
	#if defined(LIBMAUS2_HAVE_GMP)
	mpf_add(decode(v),decode(v),decode(o.v));
	#endif
	return *this;
}
void *thread1(void *param)
{
	if (i != 0)
	{
		mpf_mul(aux2, y, sqrtx);
		mpf_add(aux2, aux2, invsqrtx);
		mpf_add_ui(y, y, 1);
		mpf_div(y, aux2, y);		
	}
	mpf_add_ui(aux2, y, 1);
	pthread_exit(0);
}
void* calc_a(void* args) {
	/*params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 )*/
	pthread_mutex_lock(&mutexA);	
	
	mpf_pow_ui(a0Aux, params[Y][1], 2);
        mpf_add(a0Aux, a0Aux, params[Y][1]);
        mpf_add_ui(a0Aux, a0Aux, 1);
        mpf_mul(a0Aux, params[Y][1], a0Aux);
	
	pthread_mutex_unlock(&mutexA);
        pthread_exit(NULL);
}
예제 #25
0
void poly_eval_mult_mpf(mpf_t val, mpf_t x, size_t nterms, mpf_t* coef, mpf_t* temp) {
	mpf_set_d(val,1.0);
	if (nterms==0 || coef==NULL) {
		return;
	}
	for (size_t i=0; i<nterms; i++) {
		mpf_t *tmp = GET_NEXT_TEMP(temp);
		mpf_add(*tmp, x, coef[i]);
		mpf_mul(val, val, *tmp);
	}
	return;
}
예제 #26
0
파일: pi.cpp 프로젝트: jackowayed/pi-calc
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;
}
예제 #27
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);
}
예제 #28
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);
}
예제 #29
0
파일: floatbasic.cpp 프로젝트: AnZhg/mU
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 ;
}
예제 #30
0
void poly_eval_mpf(mpf_t val, mpf_t x, size_t nterms, mpf_t* coef, mpf_t *temp) {
	if (nterms<1 || coef==NULL) {
		mpf_set_d(val,1.0);
		return;
	}
	mpf_t* x_pow = GET_NEXT_TEMP(temp);
	mpf_set_d(*x_pow,1.0);
	mpf_set_d(val,0.0);
	for (size_t i=0; i<nterms; i++) {
		mpf_t* tmp = GET_NEXT_TEMP(temp);
		mpf_mul(*tmp,*x_pow, coef[i]);
		mpf_add(val,val,*tmp);
		mpf_mul(*x_pow,*x_pow,x);
	}
	return;
}