The Finite Resolution Dynamics Software
numberpi.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// @file numberpi.h
4 ///
5 /// Rigorous bounds for the number pi.
6 /// This file contains the definition of two numbers which bound
7 /// the number pi from below and from above.
8 /// These numbers are provided for two types: double and long double.
9 ///
10 /// @author Pawel Pilarczyk
11 ///
12 /////////////////////////////////////////////////////////////////////////////
13 
14 // Copyright (C) 2007-2010 by Pawel Pilarczyk.
15 //
16 // This file is part of my research program package. This is free software;
17 // you can redistribute it and/or modify it under the terms of the GNU
18 // General Public License as published by the Free Software Foundation;
19 // either version 2 of the License, or (at your option) any later version.
20 //
21 // This software is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License along
27 // with this software; see the file "license.txt". If not, write to the
28 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 // MA 02111-1307, USA.
30 
31 // Started on December 23, 2008. Last revision: December 31, 2008.
32 
33 
34 #ifndef _FINRESDYN_NUMBERPI_H_
35 #define _FINRESDYN_NUMBERPI_H_
36 
37 
38 // BOOST (this header must be included first)
39 #include "boost/numeric/interval.hpp"
40 
41 
42 // --------------------------------------------------
43 // -------- a class that holds the number pi --------
44 // --------------------------------------------------
45 
46 /// A class that holds the number pi.
47 template <class numType>
48 class tNumberPi
49 {
50 public:
51  /// A function that returns a lower bound for the number Pi.
52  static const numType &lowerBound ();
53 
54  /// A function that returns an upper bound for the number Pi.
55  static const numType &upperBound ();
56 
57 private:
58  /// A lower bound for the number pi.
59  static numType theLowerPi;
60 
61  /// An upper bound for the number pi.
62  static numType theUpperPi;
63 
64  /// Computation of a lower and an upper bound for the number pi.
65  static void computeBoundsForPi ();
66 
67 }; /* class tNumberPi */
68 
69 // --------------------------------------------------
70 
71 template <class numType>
72 inline const numType &tNumberPi<numType>::lowerBound ()
73 {
74  if (theLowerPi < 1)
76  return theLowerPi;
77 } /* lowerBound */
78 
79 template <class numType>
80 inline const numType &tNumberPi<numType>::upperBound ()
81 {
82  if (theUpperPi < 1)
84  return theUpperPi;
85 } /* upperBound */
86 
87 template <class numType>
89 
90 template <class numType>
92 
93 // --------------------------------------------------
94 
95 template <>
97 {
98  theLowerPi = boost::numeric::interval_lib::constants::
99  pi_lower<float> ();
100  theUpperPi = boost::numeric::interval_lib::constants::
101  pi_upper<float> ();
102  return;
103 } /* computeBoundsForPi */
104 
105 template <>
107 {
108  theLowerPi = boost::numeric::interval_lib::constants::
109  pi_lower<double> ();
110  theUpperPi = boost::numeric::interval_lib::constants::
111  pi_upper<double> ();
112  return;
113 } /* computeBoundsForPi */
114 
115 template <>
117 {
118  volatile long double approxPi = 3.141592653589793238462643383279\
119 502884197169399375105820974944592307816406286208998628034825342117067\
120 982148086513282306647093844609550582231725359408128481117450284102701\
121 9385211055596446229489549303820L;
122  long double approxPi1 = approxPi;
123  theLowerPi = tRounding<long double>::sub_down (approxPi1, 1e-198L);
124  long double approxPi2 = approxPi;
125  theUpperPi = tRounding<long double>::add_up (approxPi2, 1e-198L);
126  if (theLowerPi == theUpperPi)
127  throw "Wrong rounding for pi.";
128  if (theLowerPi >= approxPi)
129  throw "Wrong lower bound for pi.";
130  if (theUpperPi <= approxPi)
131  throw "Wrong upper bound for pi.";
132  return;
133 } /* computeBoundsForPi */
134 
135 
136 #endif // _FINRESDYN_NUMBERPI_H_
137 
static numType theUpperPi
An upper bound for the number pi.
Definition: numberpi.h:62
static numType theLowerPi
A lower bound for the number pi.
Definition: numberpi.h:59
A class for rounding operations which uses the BOOST library.
Definition: rounding.h:48
static const numType & lowerBound()
A function that returns a lower bound for the number Pi.
Definition: numberpi.h:72
A class that holds the number pi.
Definition: numberpi.h:48
static const numType & upperBound()
A function that returns an upper bound for the number Pi.
Definition: numberpi.h:80
static void computeBoundsForPi()
Computation of a lower and an upper bound for the number pi.