2 namespace til { namespace math
5 //---------------------------------------------------------------------------
7 template < typename TPrec, typename TInfinitySolutionsPolicy >
9 PolySolver_real< TPrec, TInfinitySolutionsPolicy >::
10 solve(TPrec a, TPrec b)
12 const TPrec EPSILON = 128*std::numeric_limits<TPrec>::epsilon();
14 if (std::abs(a) < EPSILON)
16 if (std::abs(b) < EPSILON)
18 m_infSolPolicy(*this);
32 //---------------------------------------------------------------------------
34 template < typename TPrec, typename TInfinitySolutionsPolicy >
36 PolySolver_real< TPrec, TInfinitySolutionsPolicy >::
37 solve(TPrec a, TPrec b, TPrec c)
39 const TPrec EPSILON = 128*std::numeric_limits<TPrec>::epsilon();
41 // Check that a is not too small -- otherwise we'll have numerical problems, that
42 // are solved here by considering that a small a makes the polynomial first degree
43 if (std::abs(a) < EPSILON)
49 TPrec delta = b*b-4*a*c;
51 // Start with this case, as it is probably the most frequent case
55 delta = std::sqrt(delta);
57 m_sols[0] = (-b-delta)/a;
58 m_sols[1] = (-b+delta)/a;
60 // TODO: I am not sure whether this should not be delta < 0 instead...
61 else if (delta < -EPSILON)
68 m_sols[0] = -b / (2*a);
72 //---------------------------------------------------------------------------
74 }} // namespace til::math