50 lines
906 B
C++
50 lines
906 B
C++
#include "triangle.h"
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
|
|
namespace triangle {
|
|
|
|
using namespace std;
|
|
|
|
namespace {
|
|
|
|
inline bool fuzzy_equal(double a, double b)
|
|
{
|
|
typedef numeric_limits<double> limits_t;
|
|
|
|
static const double &epsilon = limits_t::epsilon();
|
|
static const double &min = limits_t::min();
|
|
static const double &round_error = limits_t::round_error();
|
|
|
|
|
|
return abs(a - b) < epsilon * abs(a + b) * round_error
|
|
|| abs(a - b) < min;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
kind_t kind(double a, double b, double c)
|
|
{
|
|
if (a > b) swap(a, b);
|
|
if (a > c) swap(a, c);
|
|
if (b > c) swap(b, c);
|
|
|
|
if (a <= 0)
|
|
throw domain_error("invalid triangle");
|
|
|
|
if (a + b <= c)
|
|
throw domain_error("");
|
|
|
|
if (fuzzy_equal(a, c))
|
|
return equilateral;
|
|
|
|
if (fuzzy_equal(a, b) || fuzzy_equal(b, c))
|
|
return isosceles;
|
|
|
|
return scalene;
|
|
}
|
|
|
|
}
|