Simpatico  v1.10
Dbl.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "Dbl.h"
9 #include "Format.h"
10 
11 #include <iostream>
12 
13 namespace Util
14 {
15 
16  // Default constructor.
18  : value_(0.0),
19  width_(Format::defaultWidth()),
20  precision_(Format::defaultPrecision()),
21  isFixed_(false)
22  {}
23 
24  // Constructor, value only.
25  Dbl::Dbl(double value)
26  : value_(value),
27  width_(Format::defaultWidth()),
28  precision_(Format::defaultPrecision()),
29  isFixed_(false)
30  {}
31 
32  // Constructor, value and width.
33  Dbl::Dbl(double value, int width)
34  : value_(value),
35  width_(width),
36  precision_(Format::defaultPrecision()),
37  isFixed_(false)
38  {}
39 
40  // Constructor: value, width, and precision.
41  Dbl::Dbl(double value, int width, int precision, bool isFixed)
42  : value_(value),
43  width_(width),
44  precision_(precision),
45  isFixed_(isFixed)
46  {}
47 
48  void Dbl::setValue(double value)
49  { value_ = value; }
50 
51  void Dbl::setWidth(int width)
52  { width_ = width; }
53 
55  { precision_ = precision; }
56 
57  double Dbl::value()
58  { return value_; }
59 
60  int Dbl::width()
61  { return width_; }
62 
64  { return precision_; }
65 
73  std::istream& operator>>(std::istream& in, Dbl &object)
74  {
75  in >> object.value_;
76  return in;
77  }
78 
86  std::ostream& operator<<(std::ostream& out, const Dbl &object)
87  {
88  if (object.isFixed_) {
89  out.setf(std::ios::fixed, std::ios::floatfield);
90  } else {
91  out.setf(std::ios::scientific, std::ios::floatfield);
92  }
93  out.width(object.width_);
94  out.precision(object.precision_);
95  out << object.value_;
96  return out;
97  }
98 
99 }
Base class for output wrappers for formatted C++ ostream output.
Definition: Format.h:24
Dbl()
Default constructor.
Definition: Dbl.cpp:17
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
int precision()
Get floating point precision.
Definition: Dbl.cpp:63
double value()
Get value of associated double.
Definition: Dbl.cpp:57
void setWidth(int width)
Set output field width.
Definition: Dbl.cpp:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
void setPrecision(int precision)
Set output floating point precision.
Definition: Dbl.cpp:54
void setValue(double value)
Set value of associated double.
Definition: Dbl.cpp:48
int width()
Get field width.
Definition: Dbl.cpp:60
friend std::ostream & operator<<(std::ostream &out, const Dbl &object)
Output stream inserter for an Dbl object.
Definition: Dbl.cpp:86
friend std::istream & operator>>(std::istream &in, Dbl &object)
Input stream extractor for an Dbl object.
Definition: Dbl.cpp:73