scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
lab_number.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_MECA_NUMBER_LAB_NUMBER_HPP_INCLUDED
2#define SCIFIR_UNITS_MECA_NUMBER_LAB_NUMBER_HPP_INCLUDED
3
4#include "../util/is_number.hpp"
5#include "../util/types.hpp"
6#include "../units/scalar_unit.hpp"
7
8#include "boost/algorithm/string.hpp"
9
10#include <cmath>
11#include <iostream>
12#include <string>
13
14using namespace std;
15
16namespace scifir
17{
18 template<typename T>
20 {
21 public:
24
27
28 lab_number(lab_number<T>&& x) : value(std::move(x.value)),error_value(std::move(x.error_value))
29 {}
30
31 explicit lab_number(const scalar_unit& x,const scalar_unit& y) : value(x),error_value(y)
32 {}
33
34 explicit lab_number(const string& x,const string& y) : value(x),error_value(y)
35 {}
36
37 explicit lab_number(const string& init_lab_number)
38 {
39 vector<string> values;
40 boost::split(values,init_lab_number,boost::is_any_of("+-,\u00B1"));
41 if (values.size() == 3)
42 {
43 boost::trim(values[0]);
44 boost::trim(values[2]);
45 value = T(values[0]);
46 error_value = T(values[2]);
47 }
48 }
49
51 {
52 value = x.value;
53 error_value = x.error_value;
54 return *this;
55 }
56
58 {
59 value = std::move(x.value);
60 error_value = std::move(x.error_value);
61 return *this;
62 }
63
64 template<typename U>
66 {
67 return lab_number<scalar_unit>(value + x.value,error_value + x.error_value);
68 }
69
70 template<typename U>
72 {
73 return lab_number<scalar_unit>(value - x.value,error_value - x.error_value);
74 }
75
76 template<typename U>
78 {
79 return lab_number<scalar_unit>(value * x.value,error_value * x.error_value);
80 }
81
82 template<typename U>
84 {
85 return lab_number<scalar_unit>(value / x.value,error_value / x.error_value);
86 }
87
88 template<typename U>
90 {
91 value += x.value;
92 error_value += x.error_value;
93 }
94
95 template<typename U>
97 {
98 value -= x.value;
99 error_value -= x.error_value;
100 }
101
102 string display(int number_of_decimals = 2) const
103 {
105 output << value.display(number_of_decimals) << " \u00B1 " << error_value.display(number_of_decimals);
106 return output.str();
107 }
108
111 };
112
113 template<typename T>
114 string to_string(const lab_number<T>& x)
115 {
116 return x.display(2);
117 }
118
119 bool is_lab_number(const string& init_lab_number);
120}
121
122template<typename T,typename U>
124{
125 if (x.value == y.value and x.error_value == y.error_value)
126 {
127 return true;
128 }
129 else
130 {
131 return false;
132 }
133}
134
135template<typename T,typename U>
137{
138 return !(x == y);
139}
140
141template<typename T>
142bool operator ==(const scifir::lab_number<T>& x, const string& init_lab_number)
143{
144 scifir::lab_number<T> y(init_lab_number);
145 return (x == y);
146}
147
148template<typename T>
149bool operator !=(const scifir::lab_number<T>& x, const string& init_lab_number)
150{
151 return !(x == init_lab_number);
152}
153
154template<typename T>
155bool operator ==(const string& init_lab_number, const scifir::lab_number<T>& x)
156{
157 scifir::lab_number<T> y(init_lab_number);
158 return (x == y);
159}
160
161template<typename T>
162bool operator !=(const string& init_lab_number, const scifir::lab_number<T>& x)
163{
164 return !(init_lab_number == x);
165}
166
167template<typename T>
168void operator +=(string& x, const scifir::lab_number<T>& y)
169{
170 x += to_string(y);
171}
172
173template<typename T>
174string operator +(const string& x, const scifir::lab_number<T>& y)
175{
176 return x + to_string(y);
177}
178
179template<typename T>
180string operator +(const scifir::lab_number<T>& x, const string& y)
181{
182 return to_string(x) + y;
183}
184
185template<typename T>
186ostream& operator <<(ostream& os, const scifir::lab_number<T>& x)
187{
188 return os << to_string(x);
189}
190
191template<typename T>
192istream& operator >>(istream& is, scifir::lab_number<T>& x)
193{
194 char a[256];
195 is.getline(a, 256);
196 string b(a);
198 return is;
199}
200
201#endif // SCIFIR_UNITS_MECA_NUMBER_LAB_NUMBER_HPP_INCLUDED
lab_number< scalar_unit > operator+(const lab_number< U > &x) const
void operator-=(const lab_number< U > &x)
lab_number(const scalar_unit &x, const scalar_unit &y)
lab_number(const string &x, const string &y)
lab_number(const lab_number< T > &x)
lab_number< T > & operator=(const lab_number< T > &x)
string display(int number_of_decimals=2) const
lab_number< scalar_unit > operator/(const lab_number< U > &x) const
lab_number< scalar_unit > operator-(const lab_number< U > &x) const
lab_number(lab_number< T > &&x)
lab_number(const string &init_lab_number)
void operator+=(const lab_number< U > &x)
lab_number< scalar_unit > operator*(const lab_number< U > &x) const
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
void operator+=(string &x, const scifir::lab_number< T > &y)
bool operator==(const scifir::lab_number< T > &x, const scifir::lab_number< U > &y)
istream & operator>>(istream &is, scifir::lab_number< T > &x)
string operator+(const string &x, const scifir::lab_number< T > &y)
ostream & operator<<(ostream &os, const scifir::lab_number< T > &x)
bool operator!=(const scifir::lab_number< T > &x, const scifir::lab_number< U > &y)
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
bool is_lab_number(const string &init_lab_number)
Definition lab_number.cpp:7