/*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; }
//------------------------------------------------------------------------------ // Name: //------------------------------------------------------------------------------ knumber_base *knumber_float::pow(knumber_base *rhs) { if(knumber_integer *const p = dynamic_cast<knumber_integer *>(rhs)) { mpf_pow_ui(mpf_, mpf_, mpz_get_ui(p->mpz_)); if(p->sign() < 0) { return reciprocal(); } else { return this; } } else if(knumber_float *const p = dynamic_cast<knumber_float *>(rhs)) { return execute_libc_func< ::pow>(mpf_get_d(mpf_), mpf_get_d(p->mpf_)); } else if(knumber_fraction *const p = dynamic_cast<knumber_fraction *>(rhs)) { knumber_float f(p); return execute_libc_func< ::pow>(mpf_get_d(mpf_), mpf_get_d(f.mpf_)); } else if(knumber_error *const p = dynamic_cast<knumber_error *>(rhs)) { if(p->sign() > 0) { knumber_error *e = new knumber_error(knumber_error::ERROR_POS_INFINITY); delete this; return e; } else if(p->sign() < 0) { knumber_integer *n = new knumber_integer(0); delete this; return n; } else { knumber_error *e = new knumber_error(knumber_error::ERROR_UNDEFINED); delete this; return e; } } Q_ASSERT(0); return 0; }
void* calc_b(void* args) { /*2 ^ ( 2 * i + 3 )*/ mpf_set_ui(a0Aux2, 2); x = (2*iteracoes) + 3; mpf_pow_ui(a0Aux2, a0Aux2, x); pthread_exit(NULL); }
/** * void calculate_t() * * Descricao: * Calcula o valor da variavel t na n-esima iteracao. * * Parametros de entrada: * - * * Parametros de retorno: * - */ void calculate_t(){ mpf_sub(t_n[n_count+1], a_n[n_count], a_n[n_count+1]); mpf_pow_ui(t_n[n_count+1], t_n[n_count+1], 2); mpf_mul(t_n[n_count+1], p_n[n_count], t_n[n_count+1]); mpf_sub(t_n[n_count+1], t_n[n_count], t_n[n_count+1]); }
/** * 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]); }
Float Float::pow(const long exponent) const { Float result(*this); if (exponent < 0) mpf_ui_div(result.value, 1, result.value); mpf_pow_ui(result.value, result.value, std::abs(exponent)); return result; }
void calculate_term(unsigned int n,double x, mpf_t term, mpf_t a, mpf_t b) { /* a = ((-1)^n) */ mpf_set_str(a,"-1.0",10); mpf_pow_ui(a,a,n); /* b = (x^(2*n)) */ mpf_set_d(b,x); mpf_pow_ui(b,b,2*n); /* a = a*b (liberamos o uso de b para contas futuras) */ mpf_mul(a,a,b); /* fatorial(2*n) */ mpf_fac_ui(b,2*n); /* a = a/b */ mpf_div(term,a,b); }
TEST_F(InputTest, Precision) { mpf_class expected_precision {10.0}; mpf_pow_ui(expected_precision.get_mpf_t(), expected_precision.get_mpf_t(), 110); expected_precision = 1.0 / expected_precision; EXPECT_TRUE(mpf_cmp(expected_precision.get_mpf_t(), inputF.getPrecision().get_mpf_t())); EXPECT_TRUE(mpf_cmp(expected_precision.get_mpf_t(), inputM.getPrecision().get_mpf_t())); }
void determine_pivot_tolerances(mpf_t pivot_tol, mpf_t pivot_drop_tol, int prec) /***************************************************************\ * USAGE: compute tolerances based on prec * \***************************************************************/ { int num_digits = (int) floor(prec * log10(2.0) - 2.5); // setup pivot_tol mpf_set_prec(pivot_tol, prec); mpf_set_ui(pivot_tol, 10); mpf_pow_ui(pivot_tol, pivot_tol, num_digits); mpf_ui_div(pivot_tol, 1, pivot_tol); // setup pivot_drop_tol mpf_set_prec(pivot_drop_tol, prec); mpf_set_ui(pivot_drop_tol, 10); mpf_pow_ui(pivot_drop_tol, pivot_drop_tol, num_digits - 3); return; }
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); }
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); } }
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); }
unsigned char matrix_sub_d_d_mpf(double***** omatrix, unsigned long long* omsize, double**** imatrix, unsigned long long* imsize, double t) { unsigned long long n,m,p; unsigned char flag; mpf_t z1,tmp1,tmp2; unsigned char prec=21; if (imsize[1]==2) { mpf_init2(z1,prec); mpf_set_d(z1,t); omsize[1]=imsize[1]-1; } else { printf("ERROR: No free variables to use for substitution\n"); return 3; } omsize[0]=imsize[0]; flag = matrix_alloc_d(omatrix,omsize,1); //allocate each row to previous row allocation if (flag!=0) { mpf_clear(z1); return flag; } mpf_init2(tmp1,prec); mpf_init2(tmp2,prec); for (n=0ULL;n<omsize[0];n++) { for (m=0ULL;m<omsize[0];m++) { (*omatrix)[n][m][0][0]=1; for (p=0;p<imatrix[n][m][0][0];p++) { mpf_pow_ui(tmp1,z1,(unsigned long int)imatrix[n][m][1][imsize[1]*p]); mpf_set_d(tmp2,imatrix[n][m][1][imsize[1]*p+1]); mpf_mul(tmp1,tmp1,tmp2); (*omatrix)[n][m][1][0] = (*omatrix)[n][m][1][0] + mpf_get_d(tmp1); } } } mpf_clears(z1,tmp1,tmp2,NULL); return 0; }
// Computes q = exp(2 pi i tau). static void compute_q(mpc_t q, mpc_t tau) { mpc_t z0; mpf_t f0, f1; mpf_ptr fp0; pbc_mpui pwr; mpc_init(z0); mpf_init(f0); mpf_init(f1); //compute z0 = 2 pi i tau mpc_set(z0, tau); //first remove integral part of Re(tau) //since exp(2 pi i) = 1 //it seems |Re(tau)| < 1 anyway? fp0 = mpc_re(z0); mpf_trunc(f1, fp0); mpf_sub(fp0, fp0, f1); mpc_mul_mpf(z0, z0, pi); mpc_mul_ui(z0, z0, 2); mpc_muli(z0, z0); //compute q = exp(z0); //first write z0 = A + a + b i //where A is a (negative) integer //and a, b are in [-1, 1] //compute e^A separately fp0 = mpc_re(z0); pwr = mpf_get_ui(fp0); mpf_pow_ui(f0, recipeulere, pwr); mpf_add_ui(fp0, fp0, pwr); mpf_exp(f1, mpc_re(z0)); mpf_mul(f0, f1, f0); mpc_cis(q, mpc_im(z0)); /* old_mpc_exp(q, z0); */ mpc_mul_mpf(q, q, f0); mpc_clear(z0); mpf_clear(f0); mpf_clear(f1); }
void *calc_y(thr_gmpf_t *vars) { /*vars.y1 = (1-sqrt(sqrt((1-pow(vars.y0,4))))) / (1+sqrt(sqrt(1-pow(vars.y0,4))));*/ /*b = sqrt(sqrt(1-y0^4))*/ mpf_pow_ui(vars->y1,vars->y0,4); /*y1 = pow(y0,4)*/ mpf_ui_sub(vars->y1,1,vars->y1); /*y1 = 1 - y1*/ mpf_sqrt(vars->y1,vars->y1); mpf_sqrt(vars->y1,vars->y1); /*y1 = 1-b/1+b*/ mpf_ui_sub(vars->aux_y1,1,vars->y1); mpf_add_ui(vars->aux_y2,vars->y1,1); mpf_div(vars->y1,vars->aux_y1,vars->aux_y2); pthread_exit(NULL); return NULL; }
void correction_factor_Q(mpf_t Q, int N,mpf_t rho,int k){ unsigned long int f; mpf_t tmp,tmp1,tmp2; /* if (PNK_N_1_rho_1_PN == 0) cout << "Algun factor es 0 N = " << N << " rho = " << rho << " k = " << k << " P__0 = " << P__0 << " P__N = " << P__N << endl; */ mpf_init(tmp); mpf_init_set_d(tmp1,pow(N,k-1)); // N^(k-1) f = 1; mpf_set_ui(Q,0); for (unsigned long int j = k;j < N;j++) { /*Q += (N - j) * pow(N,j) * pow(rho,j - k) * P__0 * N_k_1 / (factorial(j - k) * PNK_N_1_rho_1_PN); */ /* Q += [(N - j) * N^j * rho^(j - k) / (j - k)!] * P__0 * N_k_1 / PNK_N_1_rho_1_PN; */ mpf_mul_ui(tmp1,tmp1,N); // * N if (j - k > 1) mpf_div_ui(tmp1,tmp1,j - k); // / (j - k) mpf_mul_ui(tmp,tmp1,N - j); // * (N - j) mpf_add(Q,Q,tmp); // Q += (N - j) * N^j * rho^(j - k) / (j - k)! mpf_mul(tmp1,tmp1,rho); // * rho } mpf_init(tmp2); mpf_ui_sub(tmp2,1,P__N); // 1 - P__N mpf_pow_ui(tmp1,tmp2,k); // (1 - P__N)^k mpf_mul(tmp2,tmp2,rho); // rho * (1 - P__N) mpf_ui_sub(tmp2,1,tmp2); // 1 - rho * (1 - P__N) mpf_mul(Q,Q,P__0); // * P__0 mpf_mul_ui(Q,Q,factorial(N-k-1)); // * (N - k - 1)! /* pow(1 - P__N,k) * factorial(N) * (1 - rho * (1 - P__N)) */ mpf_mul(tmp2,tmp1,tmp2); // (1 - P__N)^k * (1 - rho * (1 - P__N)) mpf_mul_ui(tmp2,tmp2,factorial(N)); // (1 - P__N)^k * (1 - rho * (1 - P__N)) * N! mpf_div(Q,Q,tmp2); // / [(1 - P__N)^k * (1 - rho * (1 - P__N)) * N!] mpf_clear(tmp); mpf_clear(tmp1); mpf_clear(tmp2); }
void ks_table (mpf_t p, mpf_t val, const unsigned int n) { /* We use Eq. (27), Knuth p.58, skipping O(1/n) for simplicity. This shortcut will result in too high probabilities, especially when n is small. Pr(Kp(n) <= s) = 1 - pow(e, -2*s^2) * (1 - 2/3*s/sqrt(n) + O(1/n)) */ /* We have 's' in variable VAL and store the result in P. */ mpf_t t1, t2; mpf_init (t1); mpf_init (t2); /* t1 = 1 - 2/3 * s/sqrt(n) */ mpf_sqrt_ui (t1, n); mpf_div (t1, val, t1); mpf_mul_ui (t1, t1, 2); mpf_div_ui (t1, t1, 3); mpf_ui_sub (t1, 1, t1); /* t2 = pow(e, -2*s^2) */ #ifndef OLDGMP mpf_pow_ui (t2, val, 2); /* t2 = s^2 */ mpf_set_d (t2, exp (-(2.0 * mpf_get_d (t2)))); #else /* hmmm, gmp doesn't have pow() for floats. use doubles. */ mpf_set_d (t2, pow (M_E, -(2 * pow (mpf_get_d (val), 2)))); #endif /* p = 1 - t1 * t2 */ mpf_mul (t1, t1, t2); mpf_ui_sub (p, 1, t1); mpf_clear (t1); mpf_clear (t2); }
/* * Function: compute_bbp_second_sum_gmp * -------------------- * Computes the second summand in the BBP formula. * * d: digit to be calculated * base: the base * c: a fixed positive integer * p: a simple polynomial like x or x^2 * * returns: the value of the second sum */ void compute_bbp_second_sum_gmp(mpf_t sum, int d, int base, int c, void (*p)(mpz_t, mpz_t)) { mpf_set_d(sum, 0.0); mpz_t k; mpz_init_set_si(k, floor((double) d / (double) c) + 1); mpf_t prev_sum; mpf_init(prev_sum); mpf_set(prev_sum, sum); mpf_t base_gmp; mpf_init(base_gmp); mpf_set_si(base_gmp, base); double d_diff = 0.0; do { mpf_set(prev_sum, sum); mpz_t poly_result; mpz_init(poly_result); (*p)(poly_result, k); mpf_t num; mpf_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); signed long int exp = mpz_get_si(exponent); unsigned long int neg_exp = -1 * exp; mpf_pow_ui(num, base_gmp, neg_exp); mpf_ui_div(num, 1, 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, denom); mpf_clear(num); mpf_clear(denom); mpf_add(sum, sum, quotient); mpf_clear(quotient); mpz_add_ui(k, k, 1); mpf_t diff; mpf_init(diff); mpf_sub(diff, prev_sum, sum); d_diff = mpf_get_d(diff); d_diff = fabs(d_diff); mpf_clear(diff); } while (d_diff > 0.00000001); mpz_clear(k); mpf_clear(base_gmp); mpf_clear(prev_sum); }
extern void _jl_mpf_pow_ui(mpf_t* rop, mpf_t* base, unsigned long int exp) { mpf_pow_ui(*rop, *base, exp); }
int scanhash_m7m_hash(int thr_id, uint32_t *pdata, const uint32_t *ptarget, uint64_t max_nonce, unsigned long *hashes_done) { uint32_t data[32] __attribute__((aligned(128))); uint32_t *data_p64 = data + (M7_MIDSTATE_LEN / sizeof(data[0])); uint32_t hash[8] __attribute__((aligned(32))); uint8_t bhash[7][64] __attribute__((aligned(32))); uint32_t n = pdata[19] - 1; const uint32_t first_nonce = pdata[19]; char data_str[161], hash_str[65], target_str[65]; uint8_t *bdata = 0; mpz_t bns[8]; int rc = 0; int bytes, nnNonce2; mpz_t product; mpz_init(product); for(int i=0; i < 8; i++){ mpz_init(bns[i]); } memcpy(data, pdata, 80); sph_sha256_context ctx_final_sha256; sph_sha256_context ctx_sha256; sph_sha512_context ctx_sha512; sph_keccak512_context ctx_keccak; sph_whirlpool_context ctx_whirlpool; sph_haval256_5_context ctx_haval; sph_tiger_context ctx_tiger; sph_ripemd160_context ctx_ripemd; sph_sha256_init(&ctx_sha256); sph_sha256 (&ctx_sha256, data, M7_MIDSTATE_LEN); sph_sha512_init(&ctx_sha512); sph_sha512 (&ctx_sha512, data, M7_MIDSTATE_LEN); sph_keccak512_init(&ctx_keccak); sph_keccak512 (&ctx_keccak, data, M7_MIDSTATE_LEN); sph_whirlpool_init(&ctx_whirlpool); sph_whirlpool (&ctx_whirlpool, data, M7_MIDSTATE_LEN); sph_haval256_5_init(&ctx_haval); sph_haval256_5 (&ctx_haval, data, M7_MIDSTATE_LEN); sph_tiger_init(&ctx_tiger); sph_tiger (&ctx_tiger, data, M7_MIDSTATE_LEN); sph_ripemd160_init(&ctx_ripemd); sph_ripemd160 (&ctx_ripemd, data, M7_MIDSTATE_LEN); sph_sha256_context ctx2_sha256; sph_sha512_context ctx2_sha512; sph_keccak512_context ctx2_keccak; sph_whirlpool_context ctx2_whirlpool; sph_haval256_5_context ctx2_haval; sph_tiger_context ctx2_tiger; sph_ripemd160_context ctx2_ripemd; do { data[19] = ++n; nnNonce2 = (int)(data[19]/2); memset(bhash, 0, 7 * 64); ctx2_sha256 = ctx_sha256; sph_sha256 (&ctx2_sha256, data_p64, 80 - M7_MIDSTATE_LEN); sph_sha256_close(&ctx2_sha256, (void*)(bhash[0])); ctx2_sha512 = ctx_sha512; sph_sha512 (&ctx2_sha512, data_p64, 80 - M7_MIDSTATE_LEN); sph_sha512_close(&ctx2_sha512, (void*)(bhash[1])); ctx2_keccak = ctx_keccak; sph_keccak512 (&ctx2_keccak, data_p64, 80 - M7_MIDSTATE_LEN); sph_keccak512_close(&ctx2_keccak, (void*)(bhash[2])); ctx2_whirlpool = ctx_whirlpool; sph_whirlpool (&ctx2_whirlpool, data_p64, 80 - M7_MIDSTATE_LEN); sph_whirlpool_close(&ctx2_whirlpool, (void*)(bhash[3])); ctx2_haval = ctx_haval; sph_haval256_5 (&ctx2_haval, data_p64, 80 - M7_MIDSTATE_LEN); sph_haval256_5_close(&ctx2_haval, (void*)(bhash[4])); ctx2_tiger = ctx_tiger; sph_tiger (&ctx2_tiger, data_p64, 80 - M7_MIDSTATE_LEN); sph_tiger_close(&ctx2_tiger, (void*)(bhash[5])); ctx2_ripemd = ctx_ripemd; sph_ripemd160 (&ctx2_ripemd, data_p64, 80 - M7_MIDSTATE_LEN); sph_ripemd160_close(&ctx2_ripemd, (void*)(bhash[6])); for(int i=0; i < 7; i++){ set_one_if_zero(bhash[i]); mpz_set_uint512(bns[i],bhash[i]); } mpz_set_ui(bns[7],0); for(int i=0; i < 7; i++){ mpz_add(bns[7], bns[7], bns[i]); } mpz_set_ui(product,1); for(int i=0; i < 8; i++){ mpz_mul(product,product,bns[i]); } mpz_pow_ui(product, product, 2); bytes = mpz_sizeinbase(product, 256); bdata = (uint8_t *)realloc(bdata, bytes); mpz_export((void *)bdata, NULL, -1, 1, 0, 0, product); sph_sha256_init(&ctx_final_sha256); sph_sha256 (&ctx_final_sha256, bdata, bytes); sph_sha256_close(&ctx_final_sha256, (void*)(hash)); int digits=(int)((sqrt((double)(nnNonce2))*(1.+EPS))/9000+75); int iterations=20; mpf_set_default_prec((long int)(digits*BITS_PER_DIGIT+16)); mpz_t magipi; mpz_t magisw; mpf_t magifpi; mpf_t mpa1, mpb1, mpt1, mpp1; mpf_t mpa2, mpb2, mpt2, mpp2; mpf_t mpsft; mpz_init(magipi); mpz_init(magisw); mpf_init(magifpi); mpf_init(mpsft); mpf_init(mpa1); mpf_init(mpb1); mpf_init(mpt1); mpf_init(mpp1); mpf_init(mpa2); mpf_init(mpb2); mpf_init(mpt2); mpf_init(mpp2); uint32_t usw_; usw_ = sw_(nnNonce2, SW_DIVS); if (usw_ < 1) usw_ = 1; mpz_set_ui(magisw, usw_); uint32_t mpzscale=mpz_size(magisw); for(int i=0; i < NM7M; i++){ if (mpzscale > 1000) { mpzscale = 1000; } else if (mpzscale < 1) { mpzscale = 1; } mpf_set_ui(mpa1, 1); mpf_set_ui(mpb1, 2); mpf_set_d(mpt1, 0.25*mpzscale); mpf_set_ui(mpp1, 1); mpf_sqrt(mpb1, mpb1); mpf_ui_div(mpb1, 1, mpb1); mpf_set_ui(mpsft, 10); for(int j=0; j <= iterations; j++){ mpf_add(mpa2, mpa1, mpb1); mpf_div_ui(mpa2, mpa2, 2); mpf_mul(mpb2, mpa1, mpb1); mpf_abs(mpb2, mpb2); mpf_sqrt(mpb2, mpb2); mpf_sub(mpt2, mpa1, mpa2); mpf_abs(mpt2, mpt2); mpf_sqrt(mpt2, mpt2); mpf_mul(mpt2, mpt2, mpp1); mpf_sub(mpt2, mpt1, mpt2); mpf_mul_ui(mpp2, mpp1, 2); mpf_swap(mpa1, mpa2); mpf_swap(mpb1, mpb2); mpf_swap(mpt1, mpt2); mpf_swap(mpp1, mpp2); } mpf_add(magifpi, mpa1, mpb1); mpf_pow_ui(magifpi, magifpi, 2); mpf_div_ui(magifpi, magifpi, 4); mpf_abs(mpt1, mpt1); mpf_div(magifpi, magifpi, mpt1); mpf_pow_ui(mpsft, mpsft, digits/2); mpf_mul(magifpi, magifpi, mpsft); mpz_set_f(magipi, magifpi); mpz_add(product,product,magipi); mpz_add(product,product,magisw); mpz_set_uint256(bns[0], (void*)(hash)); mpz_add(bns[7], bns[7], bns[0]); mpz_mul(product,product,bns[7]); mpz_cdiv_q (product, product, bns[0]); if (mpz_sgn(product) <= 0) mpz_set_ui(product,1); bytes = mpz_sizeinbase(product, 256); mpzscale=bytes; bdata = (uint8_t *)realloc(bdata, bytes); mpz_export(bdata, NULL, -1, 1, 0, 0, product); sph_sha256_init(&ctx_final_sha256); sph_sha256 (&ctx_final_sha256, bdata, bytes); sph_sha256_close(&ctx_final_sha256, (void*)(hash)); } mpz_clear(magipi); mpz_clear(magisw); mpf_clear(magifpi); mpf_clear(mpsft); mpf_clear(mpa1); mpf_clear(mpb1); mpf_clear(mpt1); mpf_clear(mpp1); mpf_clear(mpa2); mpf_clear(mpb2); mpf_clear(mpt2); mpf_clear(mpp2); rc = fulltest_m7hash(hash, ptarget); if (rc) { if (opt_debug) { bin2hex(hash_str, (unsigned char *)hash, 32); bin2hex(target_str, (unsigned char *)ptarget, 32); bin2hex(data_str, (unsigned char *)data, 80); applog(LOG_DEBUG, "DEBUG: [%d thread] Found share!\ndata %s\nhash %s\ntarget %s", thr_id, data_str, hash_str, target_str); } pdata[19] = data[19]; goto out; } } while (n < max_nonce && !work_restart[thr_id].restart); pdata[19] = n; out: for(int i=0; i < 8; i++){ mpz_clear(bns[i]); } mpz_clear(product); free(bdata); *hashes_done = n - first_nonce + 1; return rc; }
unsigned char matrix_sub_d_mpf(double***** omatrix, unsigned long long* omsize, unsigned char**** imatrix, unsigned long long* imsize, char* which, ...) { unsigned long long n,m,p; unsigned char ordering[2], remaining; unsigned char flag; mpf_t z1,z2,tmp1,tmp2; unsigned char prec=21; va_list vl; va_start(vl,which); if (strcmp(which,"u")==0) { if (imsize[1]==3) { ordering[0]=0; ordering[1]=1; remaining=1; mpf_init2(z1,prec); mpf_init2(z2,prec); mpf_set_d(z1,va_arg(vl,double)); omsize[1]=imsize[1]-1; } else if (imsize[1]==2) { ordering[0]=0; remaining=0; mpf_init2(z1,prec); mpf_init2(z2,prec); mpf_set_d(z1,va_arg(vl,double)); omsize[1]=imsize[1]-1; } else { printf("ERROR: No free variables to use for substitution\n"); return 3; } } else if (strcmp(which,"x")==0) { if (imsize[1]==3) { ordering[0]=1; ordering[1]=0; remaining=1; mpf_init2(z1,prec); mpf_init2(z2,prec); mpf_set_d(z1,va_arg(vl,double)); omsize[1]=imsize[1]-1; } else if (imsize[1]==2) { ordering[0]=0; remaining=0; mpf_init2(z1,prec); mpf_init2(z2,prec); mpf_set_d(z1,va_arg(vl,double)); omsize[1]=imsize[1]-1; } else { printf("ERROR: No free variables to use for substitution\n"); return 3; } } else if (strcmp(which,"ux")==0) { if (imsize[1]==2) { printf("ERROR: Not enough free variables to use for substitution\n"); return 3; } else if (imsize[1]<2) { printf("ERROR: No free variables to use for substitution\n"); return 3; } ordering[0]=0; ordering[1]=1; remaining=0; mpf_init2(z1,prec); mpf_init2(z2,prec); mpf_set_d(z1,va_arg(vl,double)); mpf_set_d(z2,va_arg(vl,double)); omsize[1]=imsize[1]-2; } else if (strcmp(which,"xu")==0) { if (imsize[1]==2) { printf("ERROR: Not enough free variables to use for substitution\n"); return 3; } else if (imsize[1]<2) { printf("ERROR: No free variables to use for substitution\n"); return 3; } ordering[0]=1; ordering[1]=0; remaining=0; mpf_init2(z1,prec); mpf_init2(z2,prec); mpf_set_d(z2,va_arg(vl,double)); mpf_set_d(z1,va_arg(vl,double)); omsize[1]=imsize[1]-2; } else { printf("ERROR: Incorrect choice of variable substitution. Only \"u\", \"x\", \"ux\", \"xu\" allowed.\n"); return 3; } va_end(vl); omsize[0]=imsize[0]; if (remaining==1) { flag = matrix_alloc_d(omatrix,omsize,imatrix[0][0][0][1]); //allocate each row to previous row allocation } else if (remaining==0) { flag = matrix_alloc_d(omatrix,omsize,1); } if (flag!=0) { mpf_clears(z1,z2,NULL); return flag; } mpf_init2(tmp1,prec); mpf_init2(tmp2,prec); if (remaining==1) { for (n=0ULL;n<omsize[0];n++) { for (m=0ULL;m<omsize[0];m++) { if ((*omatrix)[n][m][0][1]<imatrix[n][m][0][1]) { (*omatrix)[n][m][0][1] = imatrix[n][m][0][0]; (*omatrix)[n][m][1] = (double*) realloc((*omatrix)[n][m][1],omsize[1]*imatrix[n][m][0][0]*sizeof(double)); if ((*omatrix)[n][m][1]==NULL) { printf("ERROR: Unable to reallocate memory.\n"); mpf_clears(z1,z2,tmp1,tmp2,NULL); matrix_free_d((*omatrix),omsize); return 2; } } (*omatrix)[n][m][0][0]=imatrix[n][m][0][0]; for (p=0;p<imatrix[n][m][0][0];p++) { (*omatrix)[n][m][1][omsize[1]*p]=imatrix[n][m][1][imsize[1]*p+ordering[1]]; mpf_pow_ui(tmp1,z1,imatrix[n][m][1][imsize[1]*p+ordering[0]]); mpf_mul_ui(tmp1,tmp1,imatrix[n][m][1][imsize[1]*p+2]); (*omatrix)[n][m][1][omsize[1]*p+1] = mpf_get_d(tmp1); } } } } else if ((remaining==0)&(imsize[1]==3)) { for (n=0ULL;n<omsize[0];n++) { for (m=0ULL;m<omsize[0];m++) { (*omatrix)[n][m][0][0]=1; for (p=0;p<imatrix[n][m][0][0];p++) { mpf_pow_ui(tmp1,z1,imatrix[n][m][1][imsize[1]*p]); mpf_pow_ui(tmp2,z2,imatrix[n][m][1][imsize[1]*p+1]); mpf_mul_ui(tmp1,tmp1,imatrix[n][m][1][imsize[1]*p+2]); mpf_mul(tmp1,tmp1,tmp2); (*omatrix)[n][m][1][0] = (*omatrix)[n][m][1][0] + mpf_get_d(tmp1); } } } } else if ((remaining==0)&(imsize[1]==2)) { for (n=0ULL;n<omsize[0];n++) { for (m=0ULL;m<omsize[0];m++) { (*omatrix)[n][m][0][0]=1; for (p=0;p<imatrix[n][m][0][0];p++) { mpf_pow_ui(tmp1,z1,imatrix[n][m][1][imsize[1]*p]); mpf_mul_ui(tmp1,tmp1,imatrix[n][m][1][imsize[1]*p+1]); (*omatrix)[n][m][1][0] = (*omatrix)[n][m][1][0] + mpf_get_d(tmp1); } } } } mpf_clears(z1,z2,tmp1,tmp2,NULL); return 0; }
int main () { int i = 0; /*Contador de iterações*/ int k = 8; /*Fator de multiplicação*/ mpf_t pi_pas, pi_novo; mpf_t a_pas, a_novo; mpf_t y_pas, y_novo; mpf_t temp1, temp2; mpf_t e; FILE *fileTime; /*Ponteiro do arquivo de saída*/ clock_t start, end; double cpu_time_used; mpf_set_default_prec(BITS_PER_DIGIT * 11000000); /*Precisão default*/ /*Inicialização das variáveis*/ mpf_init(pi_pas); mpf_init(pi_novo); mpf_init(a_pas); mpf_init(y_pas); mpf_init(temp1); mpf_init(temp2); mpf_init_set_d(a_novo, 32.0); mpf_sqrt(a_novo, a_novo); mpf_ui_sub(a_novo, 6, a_novo); mpf_init_set_d(y_novo, 2.0); mpf_sqrt(y_novo, y_novo); mpf_sub_ui(y_novo, y_novo, 1); mpf_init_set_str(e, "1e-10000000", 10); mpf_ui_div(pi_novo, 1, a_novo); start = clock(); /*Calcula as iterações*/ do { mpf_swap(pi_pas, pi_novo); mpf_swap(a_pas, a_novo); mpf_swap(y_pas, y_novo); mpf_pow_ui(y_pas, y_pas, 4); mpf_ui_sub(y_pas, 1, y_pas); mpf_sqrt(y_pas, y_pas); mpf_sqrt(y_pas, y_pas); mpf_add_ui(temp1, y_pas, 1); mpf_ui_sub(y_novo, 1, y_pas); mpf_div(y_novo, y_novo, temp1); mpf_add_ui(temp1, y_novo, 1); mpf_pow_ui(temp2, y_novo, 2); mpf_add(temp2, temp2, temp1); mpf_mul(temp2, temp2, y_novo); mpf_mul_ui(temp2, temp2, k); k *= 4; mpf_pow_ui(temp1, temp1, 4); mpf_mul(temp1, temp1, a_pas); mpf_sub(a_novo, temp1, temp2); mpf_ui_div(pi_novo, 1, a_novo); mpf_sub(pi_pas, pi_novo, pi_pas); mpf_abs(pi_pas, pi_pas); gmp_printf("\nIteracao %d | pi = %.25Ff", i, pi_novo); i++; } while ( mpf_cmp(e, pi_pas) < 0 ); end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; fileTime = fopen("execution_time.txt", "w"); fprintf(fileTime, "Execution time: %f\n", cpu_time_used); fclose(fileTime); /*Libera espaço de memória*/ mpf_clear(pi_pas); mpf_clear(pi_novo); mpf_clear(a_pas); mpf_clear(a_novo); mpf_clear(y_pas); mpf_clear(y_novo); mpf_clear(temp1); mpf_clear(temp2); mpf_clear(e); return 0; }
bool bbp_pi(bool const & abortTask, std::string const & digitIndex, uint32_t const digitStep, std::string & piSequence) { unsigned long const mantissa_bits = 132; unsigned long const count_offset = 7; // settings above gives 32 hexadecimal digits unsigned long count = static_cast<unsigned long>( floor(log10(pow(2.0, static_cast<double>(mantissa_bits))))); count -= count_offset; // starting digit mpz_t digit; mpz_init(digit); if (mpz_set_str(digit, digitIndex.c_str(), 10) < 0) return false; mpz_sub_ui(digit, digit, 1); // subtract the 3 digit mpz_add_ui(digit, digit, digitStep); mpz_t tmpI[TEMPORARY_INTEGERS]; for (size_t i = 0; i < (sizeof(tmpI) / sizeof(mpz_t)); ++i) mpz_init(tmpI[i]); mpf_t tmpF[TEMPORARY_FLOATS]; for (size_t i = 0; i < (sizeof(tmpF) / sizeof(mpf_t)); ++i) mpf_init2(tmpF[i], mantissa_bits); // determine epsilon based on the number of digits required mpf_t epsilon; mpf_init2(epsilon, mantissa_bits); mpf_set_ui(epsilon, 10); mpf_pow_ui(epsilon, epsilon, count + count_offset); mpf_ui_div(epsilon, 1, epsilon); // integer constant mpz_t sixteen; mpz_init(sixteen); mpz_set_ui(sixteen, 16); // determine the series mpf_t s1, s2, s3, s4; mpf_init2(s1, mantissa_bits); mpf_init2(s2, mantissa_bits); mpf_init2(s3, mantissa_bits); mpf_init2(s4, mantissa_bits); series(abortTask, s1, 1, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; series(abortTask, s2, 4, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; series(abortTask, s3, 5, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; series(abortTask, s4, 6, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; // pid = 4. * s1 - 2. * s2 - s3 - s4; mpf_mul_ui(s1, s1, 4); mpf_mul_ui(s2, s2, 2); mpf_t result; mpf_init2(result, mantissa_bits); mpf_sub(result, s1, s2); mpf_sub(result, result, s3); mpf_sub(result, result, s4); // pid = pid - (int) pid + 1.; mpf_t & tmp1 = tmpF[0]; mpf_floor(tmp1, result); mpf_sub(result, result, tmp1); mpf_add_ui(result, result, 1); mpf_abs(result, result); // output the result char resultStr[256]; mp_exp_t exponent; mpf_get_str(resultStr, &exponent, 16, 254, result); resultStr[count + 1] = '\0'; // cut off any erroneous bits piSequence.assign(&resultStr[1]); // cleanup for (size_t i = 0; i < (sizeof(tmpI) / sizeof(mpz_t)); ++i) mpz_clear(tmpI[i]); for (size_t i = 0; i < (sizeof(tmpF) / sizeof(mpf_t)); ++i) mpf_clear(tmpF[i]); mpz_clear(digit); mpf_clear(epsilon); mpz_clear(sixteen); mpf_clear(s1); mpf_clear(s2); mpf_clear(s3); mpf_clear(s4); mpf_clear(result); return true; }
/** * Algoritmo de Borwein * * a1 se aproxima do valor de 1/PI. * Cada iteração quadruplica o número de dígitos corretos. * */ void gmp_borwein() { /*variáveis para calculo com respectivos valores iniciais*/ mpf_t a0, a1, y0, y1, aux1, aux2; /*contador de iterações*/ long unsigned int k=0; //mpf_set_default_prec(33220); /*define a precisao do float em bits*/ mpf_set_default_prec(33219280);//(33219280); /*define a precisao do float em bits*/ mpf_init(a0),mpf_init(a1),mpf_init(y0),mpf_init(y1),mpf_init(aux1),mpf_init(aux2); /*Inicializa as variáveis em 0*/ /*seta os valores inicias das váriaveis de precisão variável*/ /*a0 = 6-4*sqrt(2)*/ mpf_set_ui(aux1,2); mpf_sqrt(aux1,aux1); /*sqrt(2)*/ mpf_mul_ui(a0,aux1,4); mpf_ui_sub(a0,6,a0); //mpf_set_d(a0, 6-4*sqrt(2)); mpf_sub_ui(y0,aux1,1); /*y0 = sqrt(2)-1*/ while(k<12) { /*y1 = (1-sqrt(sqrt((1-pow(y0,4))))) / (1+sqrt(sqrt(1-pow(y0,4))));*/ mpf_pow_ui(y1,y0,4); /*y1 = pow(y0,4)*/ mpf_ui_sub(y1,1,y1); /*y1 = 1 - y1*/ mpf_sqrt(y1,y1); mpf_sqrt(y1,y1); mpf_ui_sub(aux1,1,y1); mpf_add_ui(aux2,y1,1); mpf_div(y1,aux1,aux2); /*a1 = a0*pow(1 + y1,4) - pow(2,2*k+3)*y1*(1+y1+pow(y1,2));*/ mpf_add_ui(a1,y1,1); /*a1 = y1+1*/ mpf_pow_ui(a1,a1,4); /*a1 = a1^4*/ mpf_mul(a1,a0,a1); /*a1 = a0*a1*/ mpf_pow_ui(aux2,y1,2); /*aux2 = pow(y1,2)*/ mpf_add(aux2,aux2,y1); /*aux2 += y1*/ mpf_add_ui(aux2,aux2,1); /*aux2++*/ mpf_mul(aux2,aux2,y1); /*aux2 *= y1*/ mpf_set_ui(aux1,2); /* aux1=2 */ mpf_pow_ui(aux1,aux1,2*k+3); /* aux1 = pow(aux1,2*k+3)*/ mpf_mul(aux1,aux1,aux2); /*aux1 = aux1*aux2*/ mpf_sub(a1,a1,aux1); /*troca os valores das variáveis de maneira eficiente*/ mpf_swap(a0,a1); mpf_swap(y0,y1); k++; /*gmp_printf("k=%ld, y1=%.*Ff, 1/PI=%.*Ff\n", k, 20, y1, 20, a1); gmp_printf("a0=%.*Ff, y0=%.*Ff\n", 20, a0, 20, y0);*/ } mpf_ui_div(a1,1,a1); /*PI=1/a1*/ gmp_printf("%.*Ff\n",10000000,a1); mpf_clear(a0),mpf_clear(a1),mpf_clear(y0),mpf_clear(y1),mpf_clear(aux1),mpf_clear(aux2); /*Limpa as variáveis da memória*/ }
// // The core Gauss-Legendre routine. // On input, 'bits' is the desired precision in bits. // On output, 'pi' contains the calculated value. // static void calculatePi( unsigned bits, mpf_t pi) { mpf_t lastPi; mpf_t scratch; // variables per the formal Gauss-Legendre formulae mpf_t a; mpf_t b; mpf_t t; mpf_t x; mpf_t y; unsigned p = 1; mpf_init_set_ui(lastPi, 0); mpf_init(x); mpf_init(y); mpf_init(scratch); // initial conditions mpf_init_set_ui(a, 1); // a := 1 mpf_init(b); // b := 1 / sqrt(2) mpf_sqrt_ui(b, 2); mpf_ui_div(b, 1, b); mpf_init_set_ui(t, 4); // t := 1/4 mpf_ui_div(t, 1, t); for(;;) { // x := (a+b)/2 mpf_add(x, a, b); mpf_div_ui(x, x, 2); // y := sqrt(a*b) mpf_mul(y, a, b); mpf_sqrt(y, y); // t := t - p * (a-x)**2 mpf_sub(scratch, a, x); mpf_pow_ui(scratch, scratch, 2); mpf_mul_ui(scratch, scratch, p); mpf_sub(t, t, scratch); // a := x // b := y // p := 2p mpf_set(a, x); mpf_set(b, y); p <<= 1; // pi := ((a + b)**2) / 4t mpf_add(pi, a, b); mpf_pow_ui(pi, pi, 2); mpf_mul_ui(scratch, t, 4); mpf_div(pi, pi, scratch); // if pi == lastPi, within the requested precision, we're done if(mpf_eq(pi, lastPi, bits)) { break; } mpf_set(lastPi, pi); } // free memory associated with mpf_t's we allocated mpf_clear(a); mpf_clear(b); mpf_clear(t); mpf_clear(x); mpf_clear(y); mpf_clear(lastPi); mpf_clear(scratch); }
int main(int argc, char * * argv) { int k; mpf_t one; mpf_t three; mpf_t negone; mpf_t two; mpf_t negthird; mpf_t sr12; mpf_t num; mpf_t denom; mpf_t tmp; FILE *f; if (argc < 2) { printf("%s <prec> [file]\n", argv[0]); return 1; } PREC = atol(argv[1]); if (argc > 2) f = fopen(argv[2], "w"); mpf_set_default_prec(4 * PREC); mpf_init(pi); mpf_init(one); mpf_init(two); mpf_init(three); mpf_init(negone); mpf_init(negthird); mpf_init(sr12); mpf_init(num); mpf_init(denom); mpf_init(tmp); mpf_set_ui(pi, 0); mpf_set_ui(one, 1); mpf_set_ui(two, 2); mpf_set_ui(three, 3); mpf_set_si(negone, -1); mpf_sqrt_ui(sr12, 12); mpf_div(negthird, negone, three); mpf_set_ui(num, 1); mpf_set_ui(denom, 1); printf("Alloc\r"); fflush(stdout); for (k = 0; k < PREC; k++) { mpf_pow_ui(num, negthird, k); mpf_set_ui(denom, 2*k + 1); mpf_div(tmp, num, denom); mpf_add(pi, pi, tmp); //if (k % 50 == 0) { double progress = k; progress /= PREC; progress *= 100; printf("\rProgress: %.2lf\r", progress); fflush(stdout); } } mpf_mul(pi, pi, sr12); gmp_printf("%.*Ff\n", PREC, pi); if (f) gmp_fprintf(f, "%.*Ff", PREC, pi); }
int main() { pthread_t thread_a, thread_b; /* My threads*/ int i; FILE *filePi, *fileTime; clock_t start, end; double cpu_time_used; mpf_set_default_prec(BITS_PER_DIGIT * 11000000); /* Borwein Variable Initialization */ for(i=0; i<2; i++) for(j=0; j<2; j++) mpf_init(params[i][j]); mpf_init(real_pi); mpf_init(y0Aux); mpf_init(y0Aux2); mpf_init(a0Aux); mpf_init(a0Aux2); mpf_init(pi[0]); mpf_init(pi[1]); mpf_init_set_str(error, "1e-10000000", 10); /* Initial value setting */ mpf_sqrt_ui(params[A][0], 2.0); /* a0 = sqrt(2)*/ mpf_mul_ui(params[A][0], params[A][0], 4.0); /* a0 = 4 * sqrt(2) */ mpf_ui_sub(params[A][0], 6.0, params[A][0]); /* a0 = 6 - 4 * sqrt(2) */ mpf_sqrt_ui(params[Y][0], 2.0); /* y0 = sqrt(2) */ mpf_sub_ui(params[Y][0], params[Y][0], 1.0); /* y0 = sqrt(2) - 1 */ mpf_set_ui(pi[0], 0); mpf_set_ui(pi[1], 0); i = 1; j = 1; iteracoes = 0; x = 0; /* Load the reals digits of pi */ filePi = fopen("pi.txt", "r"); gmp_fscanf(filePi, "%Ff", real_pi); fclose(filePi); start = clock(); while(1){ /* y = ( 1 - (1 - y0 ^ 4) ^ 0.25 ) / ( 1 + ( 1 - y0 ^ 4) ^ 0.25 ) */ mpf_pow_ui(y0Aux, params[Y][0], 4); mpf_ui_sub(y0Aux, 1.0, y0Aux); mpf_sqrt(y0Aux, y0Aux); mpf_sqrt(y0Aux, y0Aux); mpf_add_ui(y0Aux2, y0Aux, 1.0); mpf_ui_sub(y0Aux, 1.0, y0Aux); mpf_div(params[Y][1], y0Aux, y0Aux2); /* a = a0 * ( 1 + params[Y][1] ) ^ 4 - 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */ /* Threads creation */ pthread_create(&thread_a, NULL, calc_a, NULL); pthread_create(&thread_b, NULL, calc_b, NULL); pthread_join(thread_a, NULL); pthread_join(thread_b, NULL); /* 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */ mpf_mul(a0Aux, a0Aux, a0Aux2); /*a0 * ( 1 + params[Y][1] ) ^ 4*/ mpf_add_ui(a0Aux2, params[Y][1], 1); mpf_pow_ui(a0Aux2, a0Aux2, 4); mpf_mul(a0Aux2, params[A][0], a0Aux2); /* form the entire expression */ mpf_sub(params[A][1], a0Aux2, a0Aux); mpf_set(params[A][0], params[A][1]); mpf_set(params[Y][0], params[Y][1]); mpf_ui_div(pi[j], 1, params[A][0]); gmp_printf("\nIteracao %d | pi = %.25Ff", iteracoes, pi[j]); /* Calculate the error */ mpf_sub(pi[(j+1)%2], real_pi, pi[j]); mpf_abs(pi[(j+1) % 2], pi[(j+1) % 2]); if(mpf_cmp(pi[(j+1)%2], error) < 0){ printf("\n%d iteracoes para alcancar 10 milhoes de digitos de corretos.", iteracoes); break; } j = (j+1) % 2; iteracoes++; i++; } end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; fileTime = fopen("execution_time.txt", "w"); fprintf(fileTime, "Execution time: %f\n", cpu_time_used); fclose(fileTime); /* Clean up*/ for(i=0; i<2; i++) for(j=0; j<2; j++) mpf_clear(params[i][j]); mpf_clear(pi[0]); mpf_clear(pi[1]); mpf_clear(real_pi); mpf_clear(error); return 0; }
mpf CosineCalculator::calculateTerm(const mpf& radians, unsigned int n) { mpf aux(0, exponent); int signal = n % 2 == 0 ? 1 : -1; mpf_pow_ui(aux.get_mpf_t(), radians.get_mpf_t(), 2*n); return signal * (aux / factorial(2*n)); }
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);*/ }
mpf CosineCalculator::calculatePrecision(int exponent) { mpf epsilon(0, exponent); mpf_pow_ui(epsilon.get_mpf_t(), mpf(exponent >= 0 ? 0.1 : 10).get_mpf_t(), std::abs(exponent)); return epsilon; }