Exemple #1
0
std::ostream& operator << (std::ostream &sout, mpfr_t m){

  if (&m != NULL){
    

    // int mp_prec = int(mpfr_get_prec(m));
    // assume a base of 10 to output

    // d = log10(2^b);

    //  std::cout << "int(mpfr_get_prec(m)) = " << int(mpfr_get_prec(m)) << "\n";
    
    int buf_size = floor( int(mpfr_get_prec(m))*  log10(2));
    
    //    std::cout << "buf_size = " << buf_size;
    
    char* the_buf = new char[buf_size];
    std::stringstream somess;
    somess << "%." << buf_size << "RNf";
    mpfr_sprintf(the_buf, somess.str().c_str() , m);
    
    sout << the_buf;
    delete[] the_buf;
  }

  return sout;


}
Exemple #2
0
/* 1. compare expected string with the string BUFFER returned by
   mpfr_sprintf(buffer, fmt, x)
   2. then test mpfr_snprintf (buffer, p, fmt, x) with a random p. */
static int
check_sprintf (const char *expected, const char *fmt, mpfr_srcptr x)
{
    int n0, n1, p;
    char buffer[BUF_SIZE];

    /* test mpfr_sprintf */
    n0 = mpfr_sprintf (buffer, fmt, x);
    if (strcmp (buffer, expected) != 0)
    {
        printf ("Error in mpfr_sprintf (s, \"%s\", x);\n", fmt);
        printf ("expected: \"%s\"\ngot:      \"%s\"\n", expected, buffer);

        exit (1);
    }

    /* test mpfr_snprintf */
    p = (int) (randlimb () % n0);
    if (p == 0 && (randlimb () & 1) == 0)
    {
        n1 = mpfr_snprintf (NULL, 0, fmt, x);
    }
    else
    {
        buffer[p] = 17;
        n1 = mpfr_snprintf (buffer, p, fmt, x);
        if (buffer[p] != 17)
        {
            printf ("Buffer overflow in mpfr_snprintf for p = %d!\n", p);
            exit (1);
        }
    }
    if (n0 != n1)
    {
        printf ("Error in mpfr_snprintf (s, %d, \"%s\", x) return value\n",
                p, fmt);
        printf ("expected: %d\ngot:      %d\n", n0, n1);
        exit (1);
    }
    if ((p > 1 && strncmp (expected, buffer, p-1) != 0)
            || (p == 1 && buffer[0] != '\0'))
    {
        char part_expected[BUF_SIZE];
        strncpy (part_expected, expected, p);
        part_expected[p-1] = '\0';
        printf ("Error in mpfr_vsnprintf (s, %d, \"%s\", ...);\n", p, fmt);
        printf ("expected: \"%s\"\ngot:      \"%s\"\n", part_expected, buffer);
        exit (1);
    }
    return n0;
}
Exemple #3
0
void tostring ()
	{
	int i;
	char sstr[20000];
	tempstring = g_string_new(NULL);

	uarray = g_array_index (intervals, GArray *, 0);

	if (uarray->len!=0)	//EMPTY INTERVAL LOOP
	{

	for (i=0; i<=uarray->len-1; i++)
	    {
	    temp1 = g_array_index (uarray, struct interval, i);

	    if (temp1.openleft == TRUE) {g_string_append(tempstring,"]");} else {g_string_append(tempstring,"[");}
	    if (mpz_cmp (&temp1.left, &ocon1e100) == 0) {g_string_append(tempstring,"-inf");} else 
		    {
		    mpfr_set_z (&temp, &temp1.left, 0);
		    mpfr_div (&temp, &temp, &con1e30, 0);
		    mpfr_sprintf(sstr, "%.30RNf", &temp);
		    g_string_append(tempstring,sstr);
		    };
	    g_string_append(tempstring,";");
	    if (mpz_cmp (&temp1.right, &pcon1e100) == 0) {g_string_append(tempstring,"+inf");} else 
		    {
		    mpfr_set_z (&temp, &temp1.right, 0);
		    mpfr_div (&temp, &temp, &con1e30, 0);
		    mpfr_sprintf(sstr, "%.30RNf", &temp);
		    g_string_append(tempstring,sstr);
		    };
	    if (temp1.openright == TRUE) {g_string_append(tempstring,"[");} else {g_string_append(tempstring,"]");}

	    if (i!=uarray->len-1) {g_string_append(tempstring,"U"); }
	    }
	    } //EMPTY INTERVAL LOOP
	    else
	    {
Exemple #4
0
static void
bug20111102 (void)
{
    mpfr_t t;
    char s[100];

    mpfr_init2 (t, 84);
    mpfr_set_str (t, "999.99999999999999999999", 10, MPFR_RNDN);
    mpfr_sprintf (s, "%.20RNg", t);
    if (strcmp (s, "1000") != 0)
    {
        printf ("Error in bug20111102, expected 1000, got %s\n", s);
        exit (1);
    }
    mpfr_clear (t);
}
Exemple #5
0
int main(int argc, char **argv) {
    mpfr_t tmp1;
    mpfr_t tmp2;
    mpfr_t tmp3;
    mpfr_t s1;
    mpfr_t s2;
    mpfr_t r;
    mpfr_t a1;
    mpfr_t a2;

    time_t start_time;
    time_t end_time;

    // Parse command line opts
    int hide_pi = 0;
    if(argc == 2) {
        if(strcmp(argv[1], "--hide-pi") == 0) {
            hide_pi = 1;
        } else if((precision = atoi(argv[1])) == 0) {
            fprintf(stderr, "Invalid precision specified. Aborting.\n");
            return 1;
        }
    } else if(argc == 3) {
        if(strcmp(argv[1], "--hide-pi") == 0) {
            hide_pi = 1;
        }

        if((precision = atoi(argv[2])) == 0) {
            fprintf(stderr, "Invalid precision specified. Aborting.\n");
            return 1;
        }
    }

    // If the precision was not specified, default it
    if(precision == 0) {
        precision = DEFAULT_PRECISION;
    }


    // Actual number of correct digits is roughly 3.35 times the requested precision
    precision *= 3.35;

    mpfr_set_default_prec(precision);

    mpfr_inits(tmp1, tmp2, tmp3, s1, s2, r, a1, a2, NULL);

    start_time = time(NULL);

    // a0 = 1/3
    mpfr_set_ui(a1, 1, MPFR_RNDN);
    mpfr_div_ui(a1, a1, 3, MPFR_RNDN);

    // s0 = (3^.5 - 1) / 2
    mpfr_sqrt_ui(s1, 3, MPFR_RNDN);
    mpfr_sub_ui(s1, s1, 1, MPFR_RNDN);
    mpfr_div_ui(s1, s1, 2, MPFR_RNDN);

    unsigned long i = 0;
    while(i < MAX_ITERS) {
        // r = 3 / (1 + 2(1-s^3)^(1/3))
        mpfr_pow_ui(tmp1, s1, 3, MPFR_RNDN);
        mpfr_ui_sub(r, 1, tmp1, MPFR_RNDN);
        mpfr_root(r, r, 3, MPFR_RNDN);
        mpfr_mul_ui(r, r, 2, MPFR_RNDN);
        mpfr_add_ui(r, r, 1, MPFR_RNDN);
        mpfr_ui_div(r, 3, r, MPFR_RNDN);

        // s = (r - 1) / 2
        mpfr_sub_ui(s2, r, 1, MPFR_RNDN);
        mpfr_div_ui(s2, s2, 2, MPFR_RNDN);

        // a = r^2 * a - 3^i(r^2-1)
        mpfr_pow_ui(tmp1, r, 2, MPFR_RNDN);
        mpfr_mul(a2, tmp1, a1, MPFR_RNDN);
        mpfr_sub_ui(tmp1, tmp1, 1, MPFR_RNDN);
        mpfr_ui_pow_ui(tmp2, 3UL, i, MPFR_RNDN);
        mpfr_mul(tmp1, tmp1, tmp2, MPFR_RNDN);        
        mpfr_sub(a2, a2, tmp1, MPFR_RNDN);
        
        // s1 = s2
        mpfr_set(s1, s2, MPFR_RNDN);
        // a1 = a2
        mpfr_set(a1, a2, MPFR_RNDN);

        i++;
    }

    // pi = 1/a
    mpfr_ui_div(a2, 1, a2, MPFR_RNDN);

    end_time = time(NULL);

    mpfr_clears(tmp1, tmp2, tmp3, s1, s2, r, a1, NULL);

    // Write the digits to a string for accuracy comparison
    char *pi = malloc(precision + 100);
    if(pi == NULL) {
        fprintf(stderr, "Failed to allocated memory for output string.\n");
        return 1;
    }

    mpfr_sprintf(pi, "%.*R*f", precision, MPFR_RNDN, a2);

    // Check out accurate we are
    unsigned long accuracy = check_digits(pi);

    // Print the results (only print the digits that are accurate)
    if(!hide_pi) {
        // Plus two for the "3." at the beginning
        for(unsigned long i=0; i<(unsigned long)(precision/3.35)+2; i++) {
            printf("%c", pi[i]);
        }
        printf("\n");
    }    // Send the time and accuracy to stderr so pi can be redirected to a file if necessary
    fprintf(stderr, "Time: %d seconds\nAccuracy: %lu digits\n", (int)(end_time - start_time), accuracy);

    mpfr_clear(a2);
    free(pi);
    pi = NULL;

    return 0;
}