Search code examples
c++precisionmpfr

what is precision?


I haven't been able to find an answer on Wikipedia (or SO) or in the documentation for this very simple question.

How is the precision of a floating point number represented by an integer?

I am using the wrapper MPFRC++ over the arbitrary precision floating point library MPFR in C++. There is the option to set default precision, and it takes an integer as an argument.

What does such an integer mean?

e.g.: set_default_prec(128) .

Also, I check the sizeof for various default precision, but they always seem to be the same? Why?

e.g.:

set_default_prec(128); sizeof(mpfr::mpreal); // 16

set_default_prec(4096); sizeof(mpfr::mpreal); // still 16...


Solution

  • set_default_prec undoubtedly changes the amount of storage (number of bits) allocated to store the data. It takes about 3.5 bits to store one decimal digit, so from there you just use simple math to figure out how many bytes are needed for a given number of digits.

    As far as sizeof remaining constant, you undoubtedly have a struct (or class) that just contains a pointer to the actual data, something on this general order:

    namespace mpfr {
        struct real { 
            char *data;
            size_t size_allocated;
            size_t size_inuse;
            size_t num_digits;
        };
    }
    

    When you change the precision, it modifies the value stored in num_digits, and possibly the size of the block that data points at, but the size of the structure remains constant.