mpf_set_str() seems buggy

Nacino nacino at gmail.com
Tue Jul 23 09:38:08 CEST 2024


I posted this on Stackoverflow because I had no one else to turn to and
couldn't figure out if there was a community for GMP.

https://stackoverflow.com/questions/78781242/how-do-i-format-mpf-set-str-with-exponents-for-gmp

Here is code:

void test_mpf_str(void)
{
    char real_x[2048];
    long exp;
    mpf_t x_coord_mpf;

    mpf_init(x_coord_mpf);

    mpf_set_str(x_coord_mpf, "0.00000123456789", 10);
    gmp_printf("INPUT: %.Ff\n", x_coord_mpf);
    mpf_get_str(real_x, &exp, 10, 2048-2, x_coord_mpf);
    printf("REAL_X: %s EXP: %ld\n", real_x, exp);

    sprintf(real_x, "%se%ld", real_x, exp);
    printf("ASSIGNED REAL_X: %s\n", real_x);

    //all these below are wrong!
    mpf_set_str(x_coord_mpf, real_x, 10);
    gmp_printf("NEW INPUT: %.Ff\n", x_coord_mpf);
    mpf_get_str(real_x, &exp, 10, 2048-2, x_coord_mpf);
    printf("NEW REAL_X: %s EXP: %ld\n", real_x, exp);

    mpf_clear(x_coord_mpf);
}


Here is output:

INPUT: 0.00000123456789
REAL_X: 123456789 EXP: -5
ASSIGNED REAL_X: 123456789e-5
NEW INPUT: 1234.56789
NEW REAL_X: 123456789 EXP: 4


It seems that the exponent being read in by mpf_dset_str() is wrong.

I expect the new exponent (last line) to read -5 but it reads 4.

The error is also seen in the line before that with gmp_printf()

I've installed GMP 6.3.0 via homebrew on macOS Sonoma 14.5 and using
it in Xcode 15.4.


-fred nacino


More information about the gmp-bugs mailing list