GMP does not detect float exponent overflow while reading floating point numbers

Paul Zimmermann Paul.Zimmermann at
Fri Sep 30 08:48:20 CEST 2022

       Hi Eric,

> GMP version: gmp-6.2.1-2.fc36.x86_64, installed using dnf on Fedora 36.
> Test program: a.c and b.cpp, see attachment.
> To run a.c, use "gcc a.c -o a -lgmp && ./a". The output on my machine
> is "0.1e-3215911262793760767".
> To run b.cpp, use "g++ b.cpp -o b -lgmp -lgmpxx && ./b". The output on
> my machine is "1e+-1294967296".
> The results are wrong because I entered a very large number, but got a
> number between 0 and 1. I am expecting GMP to return an error in
> mpf_init_set_str() indicating that the exponent is too large.

on you can read:

High-level floating-point arithmetic functions (mpf). This is the GMP function category to use if the C type `double' doesn't give enough precision for an application. There are about 70 functions in this category. New projects should strongly consider using the much more complete GMP extension library mpfr instead of mpf.

Indeed with the following MPFR program you will get:

$ gcc -g /tmp/b.c -lmpfr
$ ./a.out

$ cat /tmp/b.c
#include <assert.h>
#include <stdio.h>
#include <mpfr.h>

int main(void) {
    mpfr_t f;
    const char *s = "1e3000000000000000000000000000000";
    mpfr_init_set_str(f, s, 10, MPFR_RNDN);
    mpfr_out_str (stdout, 10, 100, f, MPFR_RNDN);

Hope this helps,

More information about the gmp-bugs mailing list