Import fmit upstream version 0.97.6
[fmit.git] / libs / CppAddons / CAMath.cpp
1 // Copyright 2003 "Gilles Degottex"
2
3 // This file is part of "CppAddons"
4
5 // "CppAddons" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // "CppAddons" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20 #include "CAMath.h"
21
22 double Math::SolOfEq2::getPosSol()
23 {
24         if(x1<0)
25         {
26                 if(x2<0)
27                 {
28                         m_err=NE_X1_AND_X2_NEG;
29                         return 0;
30                 }
31                 else            return x2;
32         }
33         else
34         {
35                 if(x2>0)
36                 {
37                         m_err=NE_X1_AND_X2_POS;
38                         return 0;
39                 }
40                 else            return x1;
41         }
42 }
43
44 Math::SolOfEq2::SolOfEq2(double a, double b, double c)
45 {
46         m_err = NE_OK;
47
48         if(a==0)
49         {
50                 if(b==0)
51                 {
52                         m_err=NE_A_AND_B_EQ_ZERO;
53                         x1=0;
54                         x2=0;
55                 }
56                 else
57                 {
58                         x1=-c/b;
59                         x2=x1;
60                 }
61         }
62         else if(b==0)
63         {
64                 double d=-c/a;
65                 if(d<0)
66                 {
67                         m_err=NE_RACINE_NEG;
68                         x1=0;
69                         x2=0;
70                 }
71                 else
72                 {
73                         x1=sqrt(d);
74                         x2=-x1;
75                 }
76         }
77         else
78         {
79                 double d=b*b-4*a*c;
80                 if(d<0)
81                 {
82                         m_err=NE_DISCRIMINENT_NEG;
83                         x1=0;
84                         x2=0;
85                 }
86                 else
87                 {
88                         d=sqrt(d);
89
90                         a*=2;
91                         x1=(-b+d)/a;
92                         x2=(-b-d)/a;
93                 }
94         }
95 }