C++QEDCore  v2 Milestone 10
a framework for simulating open quantum dynamics – core
FormDouble.h
Go to the documentation of this file.
1 // Copyright András Vukics 2006–2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt)
3 #ifndef CPPQEDCORE_UTILS_FORMDOUBLE_H_INCLUDED
4 #define CPPQEDCORE_UTILS_FORMDOUBLE_H_INCLUDED
5 
6 #include "FormDoubleFwd.h"
7 
8 #include "Pars.h"
9 
10 #include <algorithm>
11 #include <iosfwd>
12 
13 
15 
21 namespace formdouble {
22 
24 template<typename T>
25 std::ostream& operator<<(std::ostream&, const Bound<T>&);
26 
27 } // formdouble
28 
29 
31 
35 class FormDouble
36 {
37 public:
38  static const int defaultPrecision;
39 
41 
45  static int overallPrecision;
46 
48 
49  FormDouble(int precision, int width) : precision_(precision), width_(width) {}
51 
53  explicit FormDouble(int precision);
55 
57  template<typename T>
58  const formdouble::Bound<T> operator()(const T&) const;
59 
61 
62  int getPrecision() const {return precision_;}
63  int getWidth () const {return width_;}
65 
66 private:
67  int precision_;
68  int width_;
69 
70 };
71 
72 
73 
74 namespace formdouble {
75 
77 inline int actualPrecision(int precision) {return std::max(FormDouble::defaultPrecision,precision);}
78 
80 
81 template<typename T>
82 class Bound
83 {
84 public:
85  Bound(const FormDouble& ff, const T& v) : f(ff), val(v) {}
86 
87  const FormDouble& f;
88  const T& val;
89 
90 };
91 
92 
93 int widthPositive(int precision);
94 int widthAny (int precision);
95 
96 
99 
100 
101 inline const FormDouble positive (int precision)
102 {return FormDouble(actualPrecision(precision),widthPositive(actualPrecision(precision)));}
103 
104 inline const FormDouble zeroAdditional(int precision)
105 {return FormDouble(actualPrecision(precision),0);}
106 
107 
109 
110 class Zero : public FormDouble
111 {
112 public:
114 
116  Zero(int precision) : FormDouble(precision,0) {}
117 
119  operator int() const {return getPrecision();}
120 
121 };
122 
123 
124 
125 } // formdouble
126 
127 
128 
129 namespace parameters {
130 
131 
133 template<>
134 class Parameter<formdouble::Zero> : public Parameter<int>
135 {
136 public:
137  typedef Parameter<int> Base;
138 
139  Parameter(const std::string& s, const std::string& d, const formdouble::Zero& v) : Base(s,d,v), v_() {}
140 
141  const formdouble::Zero& get() const {return v_=formdouble::Zero(Base::get());}
142 
143  formdouble::Zero& get() {return const_cast<formdouble::Zero&>(static_cast<const Parameter*>(this)->get());}
144 
145 protected:
146  void read_v(std::istream& is) {Base::read_v(is); FormDouble::overallPrecision=get();}
147 
148 private:
149  mutable formdouble::Zero v_; // Just a buffer, the actual value is stored in Base
150 
151 };
152 
153 
154 } // parameters
155 
156 
157 #endif // CPPQEDCORE_UTILS_FORMDOUBLE_H_INCLUDED
The parameter-bundle.
Definition: FormDouble.h:129
The parameter-bundle.
Template containing value for the given parameter.
Definition: Pars.h:110
Class representing formatting options for output (stearming) of doubles.
Definition: FormDouble.h:35
const FormDouble zeroAdditional(int precision)
Definition: FormDouble.h:104
FormDouble(int precision, int width)
Generic constructor.
Definition: FormDouble.h:50
Zero(int precision)
Implicit conversion from int possible.
Definition: FormDouble.h:116
const formdouble::Bound< T > operator()(const T &) const
Binds a value of a double-based type (e.g. double, dcomp, std::vector<double>, etc.) to the present instant.
Comprises tools related to FormDouble.
Definition: FormDouble.h:21
const FormDouble low()
Generic “low” precision (FormDouble::defaultPrecision/2)
Definition: FormDouble.h:97
static int overallPrecision
Generic overall precision accessible throughout the framework.
Definition: FormDouble.h:45
Parameter(const std::string &s, const std::string &d, const T &v)
Definition: Pars.h:113
static const int defaultPrecision
Ultimate default precision for streaming doubles for the whole framework.
Definition: FormDouble.h:38
Essentially a compound of a FormDouble and a value of some double-based type (e.g. double, dcomp, std::vector<double>, etc.)
Definition: FormDouble.h:82
const FormDouble high()
Generic “high” precision (FormDouble::defaultPrecision)
Definition: FormDouble.h:98
const FormDouble positive(int precision)
Definition: FormDouble.h:101
A dummy FormDouble used mainly for setting/getting the overallPrecision.
Definition: FormDouble.h:110
int actualPrecision(int precision)
If precision is larger than FormDouble::defaultPrecision, returns precision, otherwise the default...
Definition: FormDouble.h:77