A Tour of NTL: Examples: Floating Point Classes

#

A Tour of NTL: Examples: Floating Point Classes

NTL also supports arbitrary precision floating point with the class `RR`. Additionally, it supports two specialized classes: `quad_float`, which gives a form of quadruple precision, but without an extended exponent range, and `xdouble`, which gives double precision, but with an extended exponent range. The advantage of the latter two classes is efficiency.

Here again is a program that reads a list of numbers from the input, and outputs the sum of their squares, using the class `RR`.

    #include <NTL/RR.h>

    int main()
    {
       RR acc, val;

       acc = 0;
       while (SkipWhiteSpace(cin)) {
          cin >> val;
          acc += val*val;
       }

       cout << acc << "\n";
    }

The precision used for the computation can be set by executing

       RR::SetPrecision(p);

which sets the effective precision to `p` bits. By default, `p=150`. All of the basic arithmetic operations compute their results by rounding to the nearest `p`-bit floating point number. The semantics of this are exactly the same as in the IEEE floating point standard (except that there are no special values, like "infinity" and "not a number").

The number of decimal digits of precision that are used when printing an `RR` can be set be executing

       RR::SetOutputPrecision(d);

which sets the output precision to `d`. By default, `d=10`.

See `RR.txt` for details.

By replacing the occurences of `RR` by either `quad_float` or `xdouble`, one gets an equivalent program using one of the other floating point classes. The output precision for these two classes can be controlled just as with `RR`. See `quad_float.txt` and `xdouble.txt` for details.