void agm(mpf_class& rop1, mpf_class& rop2, const mpf_class& op1,
        const mpf_class& op2)
{
    rop1 = (op1 + op2) / 2;
    rop2 = op1 * op2;
    mpf_sqrt(rop2.get_mpf_t(), rop2.get_mpf_t());
}
示例#2
0
bool _Format_f(Signal &sig, Formatter *pFormatter,
					const Formatter::Flags &flags, const mpf_class &num)
{
	char *str = nullptr;
	::gmp_asprintf(&str, Formatter::ComposeFlags(flags, "Ff").c_str(), num.get_mpf_t());
	bool rtn = pFormatter->PutString(sig, str);
	::free(str);
	return rtn;
}
示例#3
0
//
// 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_class & pi )
{
    mpf_class lastPi( 0.0 );
    mpf_class scratch;

    // variables per the formal Gauss - Legendre formulae
    mpf_class a;
    mpf_class b;
    mpf_class t;
    mpf_class x;
    mpf_class y;
    unsigned p = 1;

    // initial conditions
    a = 1;
    // b := 1 / sqrt( 2 )
    mpf_sqrt_ui( b.get_mpf_t( ), 2 );
    b = 1.0 / b;
    t = 0.25;

    for( ;; ) {
        x = ( a + b )/2;

        // y := sqrt( ab )
        y = a * b;
        mpf_sqrt( y.get_mpf_t( ), y.get_mpf_t( ) );

        // t := t - p * ( a - x )**2
        scratch =  a - x;
        scratch *= scratch;
        scratch *= p;
        t -= scratch;

        a = x;
        b = y;
        p <<= 1;

        // pi := ( ( a + b )**2 ) / 4t
        pi = a + b;
        pi *= pi;
        pi /= ( 4 * t );

        // if pi == lastPi, within the requested precision, we're done
        if ( mpf_eq( pi.get_mpf_t( ), lastPi.get_mpf_t( ), bits ) ) {
            break;
        }
        lastPi =  pi;
    }
}
示例#4
0
std::string fixTrailingZeros(mpf_class &num, int precision) {
    char *buffer = NULL;
    gmp_asprintf(&buffer, "%.*Ff", precision + 2, num.get_mpf_t());

    int counter = strlen(buffer) - 2;

    buffer[counter] = '\0';
    --counter;

    while (buffer[counter] == '0') {
        buffer[counter] = '\0';
        --counter;
    }

    if (buffer[counter] == '.') {
        buffer[counter] = '\0';
    }

    return std::string(buffer);
}
示例#5
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_class &e)
{
	/* initial conditions, including the first two terms */
	mpf_class lastE(0.0);
	mpf_class invFact(1.0);			/* 1/2!, 1/3!, 1/4!, etc. */
	unsigned term = 2;				/* 2, 3, 4... */
	e = 2;
	
	for(;;) {
		invFact /= term;
		e += invFact;
		
		/* if e == lastE, within the requested precision, we're done */
		if(mpf_eq(e.get_mpf_t(), lastE.get_mpf_t(), bits)) {
			return;
		}
		lastE = e;
		term++;
	}
	/* NOT REACHED */
}
示例#6
0
QDebug operator<<(QDebug s, const mpf_class& m) {
    char b[65536] = "";
    gmp_sprintf (b, "%.9Ff", m.get_mpf_t());
    s << b;
    return s;
}