|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef T_INTERVAL_INCLUDED
|
|
Toshihiro Shimizu |
890ddd |
#define T_INTERVAL_INCLUDED
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include <assert.h></assert.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <limits.h></limits.h>
|
|
Toshihiro Shimizu |
890ddd |
#include "tcommon.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#undef DVAPI
|
|
Toshihiro Shimizu |
890ddd |
#undef DVVAR
|
|
Toshihiro Shimizu |
890ddd |
#ifdef TNZCORE_EXPORTS
|
|
Toshihiro Shimizu |
890ddd |
#define DVAPI DV_EXPORT_API
|
|
Toshihiro Shimizu |
890ddd |
#define DVVAR DV_EXPORT_VAR
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
#define DVAPI DV_IMPORT_API
|
|
Toshihiro Shimizu |
890ddd |
#define DVVAR DV_IMPORT_VAR
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=============================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// class TInterval implementa "Interval Arithmetic" (non vengono computati gli
|
|
Toshihiro Shimizu |
890ddd |
// errori macchina):
|
|
Toshihiro Shimizu |
890ddd |
// TInterval(min, max) (min <= max) rapresenta l'intervallo reale chiuso [min, max];
|
|
Toshihiro Shimizu |
890ddd |
// per m_min == m_max si ottiene l'algebra dei reali;
|
|
Toshihiro Shimizu |
890ddd |
// TInterval (m_min = 1, m_max = -1) rappresenta l'intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
class TInterval
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
double m_min, m_max;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
// costruisce l'intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
// TInterval (m_min = 1, m_max = -1) rappresenta l'intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
TInterval() : m_min(1), m_max(-1) {}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
// costruisce gli intervalli degeneri [x, x] che rappresentano i reali
|
|
Toshihiro Shimizu |
890ddd |
TInterval(double x) : m_min(x), m_max(x) {}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
TInterval(double min, double max)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(min <= max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
m_min = min;
|
|
Toshihiro Shimizu |
890ddd |
m_max = max;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
TInterval(const TInterval &w)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max ||
|
|
Toshihiro Shimizu |
890ddd |
(w.m_min == 1 && w.m_max == -1)); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
m_min = w.m_min;
|
|
Toshihiro Shimizu |
890ddd |
m_max = w.m_max;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval &operator=(const TInterval &w)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max ||
|
|
Toshihiro Shimizu |
890ddd |
(w.m_min == 1 && w.m_max == -1)); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
m_min = w.m_min;
|
|
Toshihiro Shimizu |
890ddd |
m_max = w.m_max;
|
|
Toshihiro Shimizu |
890ddd |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval operator+() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval operator-() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(-m_max, -m_min);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval operator+(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(m_min + w.m_min, m_max + w.m_max);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval operator-(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(m_min - w.m_max, m_max - w.m_min);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval operator*(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
double value[4];
|
|
Toshihiro Shimizu |
890ddd |
value[0] = m_min * w.m_min;
|
|
Toshihiro Shimizu |
890ddd |
value[1] = m_min * w.m_max;
|
|
Toshihiro Shimizu |
890ddd |
value[2] = m_max * w.m_min;
|
|
Toshihiro Shimizu |
890ddd |
value[3] = m_max * w.m_max;
|
|
Toshihiro Shimizu |
890ddd |
double minValue = value[0];
|
|
Toshihiro Shimizu |
890ddd |
double maxValue = value[0];
|
|
Toshihiro Shimizu |
890ddd |
int i = 1;
|
|
Toshihiro Shimizu |
890ddd |
for (i = 1; i <= 3; ++i) {
|
|
Toshihiro Shimizu |
890ddd |
if (value[i] < minValue)
|
|
Toshihiro Shimizu |
890ddd |
minValue = value[i];
|
|
Toshihiro Shimizu |
890ddd |
if (value[i] > maxValue)
|
|
Toshihiro Shimizu |
890ddd |
maxValue = value[i];
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(minValue, maxValue);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval operator/(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert((0 < w.m_min && 0 < w.m_max) ||
|
|
Toshihiro Shimizu |
890ddd |
(0 > w.m_min && 0 > w.m_max)); // divisore non nullo
|
|
Toshihiro Shimizu |
890ddd |
double value[4];
|
|
Toshihiro Shimizu |
890ddd |
value[0] = m_min / w.m_min;
|
|
Toshihiro Shimizu |
890ddd |
value[1] = m_min / w.m_max;
|
|
Toshihiro Shimizu |
890ddd |
value[2] = m_max / w.m_min;
|
|
Toshihiro Shimizu |
890ddd |
value[3] = m_max / w.m_max;
|
|
Toshihiro Shimizu |
890ddd |
double minValue = value[0];
|
|
Toshihiro Shimizu |
890ddd |
double maxValue = value[0];
|
|
Toshihiro Shimizu |
890ddd |
int i = 1;
|
|
Toshihiro Shimizu |
890ddd |
for (i = 1; i <= 3; ++i) {
|
|
Toshihiro Shimizu |
890ddd |
if (value[i] < minValue)
|
|
Toshihiro Shimizu |
890ddd |
minValue = value[i];
|
|
Toshihiro Shimizu |
890ddd |
if (value[i] > maxValue)
|
|
Toshihiro Shimizu |
890ddd |
maxValue = value[i];
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(minValue, maxValue);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool operator==(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max ||
|
|
Toshihiro Shimizu |
890ddd |
(w.m_min == 1 && w.m_max == -1)); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_min == w.m_min && m_max == w.m_max);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/* la definizione e' discutibile...
|
|
Toshihiro Shimizu |
890ddd |
inline bool operator!=(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{return (m_min != w.m_min || m_max != w.m_max);}
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool operator>(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_min > w.m_max);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool operator<(const TInterval &w) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_max < w.m_min);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/* la definizione e' discutibile...
|
|
Toshihiro Shimizu |
890ddd |
// A >= B
|
|
Toshihiro Shimizu |
890ddd |
inline bool operator>=(const TInterval &w) const {
|
|
Toshihiro Shimizu |
890ddd |
assert (m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert (w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_min >= w.m_min && m_max >= w.m_max);}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
// A <= B
|
|
Toshihiro Shimizu |
890ddd |
inline bool operator<=(const TInterval &w) const {
|
|
Toshihiro Shimizu |
890ddd |
assert (m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert (w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_min <= w.m_min && m_max <= w.m_max);}
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline void setMin(double min)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(min <= m_max); // non vuoto dopo setMin
|
|
Toshihiro Shimizu |
890ddd |
m_min = min;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline void setMax(double max)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= max); // non vuoto dopo setMax
|
|
Toshihiro Shimizu |
890ddd |
m_max = max;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline double getMin() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// isEmpty() => return 1
|
|
Toshihiro Shimizu |
890ddd |
return m_min;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline double getMax() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// isEmpty() => return -1
|
|
Toshihiro Shimizu |
890ddd |
return m_max;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline double getLength() const { return m_max - m_min; } // isEmpty() => (getLength() < 0)
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline double getCenter() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_max + m_min) / 2;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline double getRadius() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_max - m_min) / 2;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool isEmpty() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// TInterval (m_min = 1, m_max = -1) rappresenta l'intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max ||
|
|
Toshihiro Shimizu |
890ddd |
(m_min == 1 && m_max == -1)); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_min > m_max);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool isProper() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// isProper() <=> !isEmpty() && m_min < m_max (cioe' non degenere)
|
|
Toshihiro Shimizu |
890ddd |
// TInterval (m_min = 1, m_max = -1) rappresenta l'intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max ||
|
|
Toshihiro Shimizu |
890ddd |
(m_min == 1 && m_max == -1)); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_min < m_max);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool contain(double t) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(m_min <= m_max ||
|
|
Toshihiro Shimizu |
890ddd |
(m_min == 1 && m_max == -1)); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
return (m_min <= t && t <= m_max);
|
|
Toshihiro Shimizu |
890ddd |
} // isEmpty() => return false
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool include(const TInterval &interval) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (interval.isEmpty())
|
|
Toshihiro Shimizu |
890ddd |
return true;
|
|
Toshihiro Shimizu |
890ddd |
else if (isEmpty())
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
else {
|
|
Toshihiro Shimizu |
890ddd |
assert(!interval.isEmpty() && !isEmpty());
|
|
Toshihiro Shimizu |
890ddd |
return (m_min <= interval.m_min && interval.m_max <= m_max);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline bool isIncluded(const TInterval &interval) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (isEmpty())
|
|
Toshihiro Shimizu |
890ddd |
return true;
|
|
Toshihiro Shimizu |
890ddd |
else if (interval.isEmpty())
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
else {
|
|
Toshihiro Shimizu |
890ddd |
assert(!isEmpty() && !interval.isEmpty());
|
|
Toshihiro Shimizu |
890ddd |
return (interval.m_min <= m_min && m_max <= interval.m_max);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
// Friend helper function declarations
|
|
Toshihiro Shimizu |
890ddd |
friend TInterval operator*(const double s, const TInterval &w);
|
|
Toshihiro Shimizu |
890ddd |
friend TInterval intersection(const TInterval &a, const TInterval &b);
|
|
Toshihiro Shimizu |
890ddd |
friend TInterval square(const TInterval &w);
|
|
Toshihiro Shimizu |
890ddd |
friend TInterval sqrt(const TInterval &w);
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
//friend functions
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval operator*(const double s, const TInterval &w)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
if (s >= 0)
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(s * w.m_min, s * w.m_max);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(s * w.m_max, s * w.m_min);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval square(const TInterval &w)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// return w^2
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
double a = w.m_min * w.m_min;
|
|
Toshihiro Shimizu |
890ddd |
double b = w.m_max * w.m_max;
|
|
Toshihiro Shimizu |
890ddd |
if (0 <= w.m_min)
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(a, b); // return [m_min^2, m_max^2]
|
|
Toshihiro Shimizu |
890ddd |
else if (w.m_max <= 0)
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(b, a); // return [m_max^2, m_min^2]
|
|
Toshihiro Shimizu |
890ddd |
else {
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min < 0 && 0 < w.m_max);
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(0, tmax(a, b)); // [0, max(w.m_min^2, w.m_max^2)]
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval sqrt(const TInterval &w)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// return sqrt(w)
|
|
Toshihiro Shimizu |
890ddd |
assert(w.m_min <= w.m_max); // non vuoto
|
|
Toshihiro Shimizu |
890ddd |
assert(0 <= w.m_min);
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(sqrt(w.m_min), sqrt(w.m_max));
|
|
Toshihiro Shimizu |
890ddd |
} // [sqrt(w.m_min), sqrt(w.m_max)]
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
// helper function
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval intersection(const TInterval &a, const TInterval &b)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// return a_intersezione_b (insiemistico) se questa e' non vuota, altrimenti
|
|
Toshihiro Shimizu |
890ddd |
// return TInterval() (intervallo vuoto)
|
|
Toshihiro Shimizu |
890ddd |
double min = tmax(a.m_min, b.m_min);
|
|
Toshihiro Shimizu |
890ddd |
double max = tmin(a.m_max, b.m_max);
|
|
Toshihiro Shimizu |
890ddd |
if (min <= max) // a.isEmpty() || b.isEmpty() => (min >= 1 && max <= -1) => min > max
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(min, max);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval createTInterval(double center, double radius)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (radius >= 0)
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(center - radius, center + radius);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(); // intervallo vuoto
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
inline TInterval createErrorTInterval(double center,
|
|
Toshihiro Shimizu |
890ddd |
double minError =
|
|
Toshihiro Shimizu |
890ddd |
(std::numeric_limits<double>::min)())</double>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// type double standard IEEE (1 bit segno + 11 bit esponente + 52 bit mantissa)
|
|
Toshihiro Shimizu |
890ddd |
// corrisponde ad almeno 15 decimali significativi.
|
|
Toshihiro Shimizu |
890ddd |
// minError serve ad assegnare un errore positivo quando center = 0.
|
|
Toshihiro Shimizu |
890ddd |
// Aumentare 100 volte (da 1e-15 a 1e-13) l'errore relativo serve a compensare
|
|
Toshihiro Shimizu |
890ddd |
// la propagazione degli errori macchina (TInterval non li computa) ed a
|
|
Toshihiro Shimizu |
890ddd |
// stabilizzare il codice relativamente a processori che potrebbero male
|
|
Toshihiro Shimizu |
890ddd |
// implementare l'artimetica double standard IEEE.
|
|
Toshihiro Shimizu |
890ddd |
assert(minError >= 0);
|
|
Toshihiro Shimizu |
890ddd |
const double relativeDoubleError = 1e-13;
|
|
Toshihiro Shimizu |
890ddd |
double error = tmax(fabs(center) * relativeDoubleError, minError);
|
|
Toshihiro Shimizu |
890ddd |
return TInterval(center - error, center + error);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif // __T_INTERVAL_INCLUDED__
|