Domanda

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...

È stato utile?

Soluzione

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.

Altri suggerimenti

Everything you want can be found by Googling for "floating point" and "precision". For example:

http://en.wikipedia.org/wiki/Floating_point

Floating-point representation is similar in concept to scientific notation. Logically, a floating-point number consists of:

A signed digit string of a given length in a given base (or radix). This digit string is referred to as the significand, coefficient or, less often, the mantissa (see below). The length of the significand determines the precision to which numbers can be represented. The radix point position is assumed to always be somewhere within the significand—often just after or just before the most significant digit, or to the right of the rightmost (least significant) digit. This article will generally follow the convention that the radix point is just after the most significant (leftmost) digit.

In other words:

  1. A floating point number is an "approximation", not necessarily an exact value

  2. The "precision" of a floating point number determines how accurate the approximation can be. More bits mean more precision (your number is "more exact"). Fewer bits mean less precision (the number is "more rounded").

  3. The amount of "precision" can and is represented as an integral number.

From the MPFR web site:

http://www.mpfr.org/mpfr-current/mpfr.html#MPFR-Basics

A floating-point number, or float for short, is an arbitrary precision significand (also called mantissa) with a limited precision exponent. ...

The precision is the number of bits used to represent the significand of a floating-point number; the corresponding C data type is mpfr_prec_t. The precision can be any integer between MPFR_PREC_MIN and MPFR_PREC_MAX. In the current implementation, MPFR_PREC_MIN is equal to 2. ...

"set_default_prec()", of course, is the mechanism for changing the precision (from "2").

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top