-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterval.cpp
169 lines (130 loc) · 2.51 KB
/
Interval.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// FILE: Interval.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-21
// VERSION: 0.1.4
// PURPOSE: Arduino library for Interval data type
// URL: https://github.com/RobTillaart/Interval
#include "Interval.h"
Interval::Interval(float lo, float hi)
{
if (lo <= hi)
{
_lo = lo;
_hi = hi;
}
else
{
_lo = hi;
_hi = lo;
}
};
Interval::Interval(float f)
{
_lo = f;
_hi = f;
};
Interval::Interval()
{
_lo = 0;
_hi = 0;
};
float Interval::relAccuracy()
{
if (value() == 0.0) return -1;
return abs(range() / value()); // TODO /2 ?
}
void Interval::setRange(float r)
{
float f = value();
_lo = f - r / 2;
_hi = f + r / 2;
}
// PRINTABLE
size_t Interval::printTo(Print& p) const
{
size_t n = 0;
n += p.print('[');
n += p.print(_lo, _decimals);
n += p.print(", ");
n += p.print(_hi, _decimals);
n += p.print(']');
return n;
};
/////////////////////////////////////////////////
//
// MATH BASIC OPERATIONS
//
Interval Interval::operator + (const Interval &in)
{
return Interval(_lo + in._lo, _hi + in._hi);
}
Interval Interval::operator - (const Interval &in)
{
return Interval(_lo - in._hi, _hi - in._lo);
}
Interval Interval::operator * (const Interval &in)
{
return Interval(_lo * in._lo, _hi * in._hi);
}
Interval Interval::operator / (const Interval &in)
{
return Interval(_lo / in._hi, _hi / in._lo);
}
Interval Interval::operator += (const Interval &in)
{
_lo += in._lo;
_hi += in._hi;
return *this;
}
Interval Interval::operator -= (const Interval &in)
{
_lo -= in._hi;
_hi -= in._lo;
return *this;
}
Interval Interval::operator *= (const Interval &in)
{
_lo *= in._lo;
_hi *= in._hi;
return *this;
}
Interval Interval::operator /= (const Interval &in)
{
_lo /= in._hi;
_hi /= in._lo;
return *this;
}
/////////////////////////////////////////////////
//
// COMPARISON OPERATIONS
//
bool Interval::operator == (const Interval &in)
{
return ((_lo == in._lo) && (_hi == in._hi));
}
bool Interval::operator != (const Interval &in)
{
return ((_lo != in._lo) || (_hi != in._hi));
}
// VALUE FOR NOW...
// bool Interval::operator > (const Interval &in)
// {
// return this->value() > in.value();
// }
//
// bool Interval::operator >= (const Interval &in)
// {
// return this->value() >= in.value();
// }
//
// bool Interval::operator < (const Interval &in)
// {
// return this->value() < in.value();
// }
//
// bool Interval::operator <= (const Interval &in)
// {
// return this->value() <= in.value();
// }
// -- END OF FILE --