1 | <html> |
---|
2 | <head> |
---|
3 | <title> |
---|
4 | A Tour of NTL: Examples: Floating Point Classes </title> |
---|
5 | </head> |
---|
6 | |
---|
7 | <body bgcolor="#fff9e6"> |
---|
8 | <center> |
---|
9 | <a href="tour-ex5.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a> |
---|
10 | <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> |
---|
11 | <img src="arrow3.gif" alt="[Next]" align=bottom> |
---|
12 | </center> |
---|
13 | |
---|
14 | <h1> |
---|
15 | <p align=center> |
---|
16 | A Tour of NTL: Examples: Floating Point Classes |
---|
17 | </p> |
---|
18 | </h1> |
---|
19 | |
---|
20 | <p> <hr> <p> |
---|
21 | |
---|
22 | NTL also supports arbitrary precision floating point with |
---|
23 | the class <tt>RR</tt>. |
---|
24 | Additionally, it supports two specialized classes: <tt>quad_float</tt>, |
---|
25 | which gives a form of quadruple precision, but without an extended |
---|
26 | exponent range, |
---|
27 | and <tt>xdouble</tt>, |
---|
28 | which gives double precision, but with an extended exponent range. |
---|
29 | The advantage of the latter two classes is efficiency. |
---|
30 | |
---|
31 | <p> |
---|
32 | |
---|
33 | Here again is a program that reads a list of numbers from the input, |
---|
34 | and outputs the sum of their squares, using the class <tt>RR</tt>. |
---|
35 | <p> |
---|
36 | |
---|
37 | <pre> |
---|
38 | #include <NTL/RR.h> |
---|
39 | |
---|
40 | int main() |
---|
41 | { |
---|
42 | RR acc, val; |
---|
43 | |
---|
44 | acc = 0; |
---|
45 | while (SkipWhiteSpace(cin)) { |
---|
46 | cin >> val; |
---|
47 | acc += val*val; |
---|
48 | } |
---|
49 | |
---|
50 | cout << acc << "\n"; |
---|
51 | } |
---|
52 | </pre> |
---|
53 | |
---|
54 | <p> |
---|
55 | |
---|
56 | The precision used for the computation can be set by executing |
---|
57 | <pre> |
---|
58 | RR::SetPrecision(p); |
---|
59 | </pre> |
---|
60 | which sets the effective precision to <tt>p</tt> bits. |
---|
61 | By default, <tt>p=150</tt>. |
---|
62 | All of the basic arithmetic operations compute their results |
---|
63 | by rounding to the nearest <tt>p</tt>-bit floating point number. |
---|
64 | The semantics of this are exactly the same as in the IEEE floating |
---|
65 | point standard (except that there are no special values, like |
---|
66 | "infinity" and "not a number"). |
---|
67 | |
---|
68 | <p> |
---|
69 | |
---|
70 | The number of <i>decimal</i> digits of precision that are used when |
---|
71 | printing an <tt>RR</tt> can be set be executing |
---|
72 | <pre> |
---|
73 | RR::SetOutputPrecision(d); |
---|
74 | </pre> |
---|
75 | which sets the output precision to <tt>d</tt>. |
---|
76 | By default, <tt>d=10</tt>. |
---|
77 | |
---|
78 | <p> |
---|
79 | See <a href="RR.txt"><tt>RR.txt</tt></a> for details. |
---|
80 | |
---|
81 | <p> |
---|
82 | |
---|
83 | By replacing the occurences of <tt>RR</tt> by either <tt>quad_float</tt> |
---|
84 | or <tt>xdouble</tt>, one gets an equivalent program using one of the |
---|
85 | other floating point classes. |
---|
86 | The output precision for these two classes can be controlled just |
---|
87 | as with <tt>RR</tt>. |
---|
88 | See <a href="quad_float.txt"><tt>quad_float.txt</tt></a> and |
---|
89 | <a href="xdouble.txt"><tt>xdouble.txt</tt></a> |
---|
90 | for details. |
---|
91 | |
---|
92 | <p> |
---|
93 | |
---|
94 | |
---|
95 | <center> |
---|
96 | <a href="tour-ex5.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a> |
---|
97 | <a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> |
---|
98 | <img src="arrow3.gif" alt="[Next]" align=bottom> |
---|
99 | </center> |
---|
100 | |
---|
101 | </body> |
---|
102 | </html> |
---|