GooFit  v2.1.3
Uncertain.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015 Claus Jensby Madsen
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 // https://github.com/clausjensbymadsen/uncertain-values
24 
25 #pragma once
26 
27 #include <fmt/format.h>
28 #include <iosfwd>
29 #include <stdexcept>
30 #include <string>
31 
32 namespace GooFit {
33 
34 class Variable;
35 
36 class Uncertain {
37  private:
38  fptype m_value;
39  fptype m_uncertainty;
40 
41  public:
42  // Constructors
43  Uncertain(fptype value, fptype uncertainty = 0)
44  : m_value(value)
45  , m_uncertainty(uncertainty) {
46  if(m_uncertainty < 0)
47  throw std::invalid_argument("Uncertainty can not be negative.");
48  }
49 
50  Uncertain(const Variable &val)
51  : Uncertain(val.getValue(), val.getError()) {}
52 
53  // Class getters
54  fptype get_value() const { return m_value; }
55 
56  fptype get_uncertainty() const { return m_uncertainty; }
57 
58  fptype get_relative_uncertainty() const { return m_uncertainty / m_value; }
59 
60  // Equality operators
61  bool operator==(const Uncertain &other) const {
62  return (m_value == other.m_value) && (m_uncertainty == other.m_uncertainty);
63  }
64 
65  bool operator!=(const Uncertain &other) const { return !operator==(other); }
66 
67  // Arithmetic operators
68  Uncertain operator+(const Uncertain &other) const {
69  return Uncertain(m_value + other.m_value, m_uncertainty + other.m_uncertainty);
70  }
71 
72  Uncertain operator-(const Uncertain &other) const {
73  return Uncertain(m_value - other.m_value, m_uncertainty + other.m_uncertainty);
74  }
75 
76  Uncertain operator*(const Uncertain &other) const {
77  return Uncertain(m_value * other.m_value, get_relative_uncertainty() + other.get_relative_uncertainty());
78  }
79 
81  Uncertain operator*(fptype other) const { return Uncertain(m_value * other, m_uncertainty * other); }
82 
83  Uncertain operator/(const Uncertain &other) const {
84  return Uncertain(m_value / other.m_value, get_relative_uncertainty() + other.get_relative_uncertainty());
85  }
86 
87  Uncertain operator/(const fptype &other) const { return Uncertain(m_value / other, m_uncertainty / other); }
88 };
89 
91 inline Uncertain operator*(fptype other, const Uncertain &self) {
92  return Uncertain(self.get_value() * other, self.get_uncertainty() * other);
93 }
94 
96 inline std::ostream &operator<<(std::ostream &stream, Uncertain value) {
97  return stream << value.get_value() << " ± " << value.get_uncertainty();
98 }
99 
101 inline void format_arg(fmt::BasicFormatter<char> &f, const char *&format_str, const Uncertain &s) {
102  std::string value{format_str};
103  auto end = value.find('}');
104  std::string val = std::string{"{" + value.substr(0, end) + "}"};
105 
106  f.writer().write(val.c_str(), s.get_value());
107  f.writer().write(" ± ");
108  f.writer().write(val.c_str(), s.get_uncertainty());
109 
110  format_str += end + 1; // Remove :xxx}
111 }
112 
113 } // namespace GooFit
double fptype
Uncertain operator*(const Uncertain &other) const
Definition: Uncertain.h:76
bool operator==(const Uncertain &other) const
Definition: Uncertain.h:61
Uncertain operator*(fptype other) const
Allow int and float multiplies.
Definition: Uncertain.h:81
fptype get_relative_uncertainty() const
Definition: Uncertain.h:58
Uncertain(fptype value, fptype uncertainty=0)
Definition: Uncertain.h:43
Uncertain operator/(const Uncertain &other) const
Definition: Uncertain.h:83
bool operator!=(const Uncertain &other) const
Definition: Uncertain.h:65
fptype get_uncertainty() const
Definition: Uncertain.h:56
Uncertain operator-(const Uncertain &other) const
Definition: Uncertain.h:72
Uncertain operator/(const fptype &other) const
Definition: Uncertain.h:87
std::ostream & operator<<(std::ostream &stream, Uncertain value)
Simple << output.
Definition: Uncertain.h:96
Uncertain operator+(const Uncertain &other) const
Definition: Uncertain.h:68
Uncertain(const Variable &val)
Definition: Uncertain.h:50
fptype get_value() const
Definition: Uncertain.h:54
void format_arg(fmt::BasicFormatter< char > &f, const char *&format_str, const Uncertain &s)
fmt support
Definition: Uncertain.h:101