13 #define MIN(a,b) (((a)<(b))?(a):(b))
17 #define MAX(a,b) (((a)>(b))?(a):(b))
21 #define ABS(x) ((x)>0?(x):-(x))
25 #define MAXABS(a,b) (ABS((a))>ABS((b)) ? ABS((a)): ABS((b)))
29 /*----------------------------------------------------------------------*\
31 \*---------------------------------------------------------------------*/
33 /*--------------------------------------*\
35 \*-------------------------------------*/
37 MathTools::MathTools()
42 MathTools::~MathTools()
47 /*--------------------------------------*\
49 \*-------------------------------------*/
51 /*----------------------*\
53 \*---------------------*/
59 bool MathTools::isEquals(float x1
, float x2
, float epsilon
)
64 if (x1
== 0 || x2
== 0)
66 delta
= fabs(x1
- x2
);
67 isOk
= delta
<= epsilon
;
71 delta
= fabs((x1
- x2
) / MAXABS(x1
,x2
));
72 isOk
= delta
<= epsilon
;
77 cout
<< "isEgale Float: (x1,x2)=(" << x1
<< "," << x2
<< ") : delta (Relatif) = " << delta
<< endl
;
84 bool MathTools::isEquals(float a
, float b
, float reference
, float epsilon
)
86 return fabs((a
- b
) / reference
) <= epsilon
;
89 bool MathTools::isEquals(float* tabA
, float* tabB
, int n
, float epsilon
)
91 for (int i
= 0; i
< n
; i
++)
93 if (!isEquals(tabA
[i
], tabB
[i
], epsilon
))
101 bool MathTools::isEqualsRelatifMax(float* tabA
, float* tabB
, int n
, float epsilon
)
103 float max
= maxAbs(tabA
, tabB
, n
);
107 for (int i
= 0; i
< n
; i
++)
109 if (!isEquals(tabA
[i
], tabB
[i
], max
, epsilon
))
118 return true;// tous à zero
119 //return isEquals(tabA, tabB, n, epsilon);
127 bool MathTools::isEquals(double x1
, double x2
, double epsilon
)
131 if (x1
== 0 || x2
== 0)
133 delta
= fabs(x1
- x2
);
134 isOk
= delta
<= epsilon
;
138 delta
= fabs((x1
- x2
) / MAXABS(x1
,x2
));
139 isOk
= delta
<= epsilon
;
144 cout
<< "isEgale Double : (x1,x2)=(" << x1
<< "," << x2
<< ") : delta (Relatif) = " << delta
<< endl
;
154 bool MathTools::isEquals(long x1
, long x2
)
156 long delta
= labs(x1
- x2
);
157 bool isOk
= (delta
== 0);
161 cout
<< "isEgale Long: (x1,x2)=(" << x1
<< "," << x2
<< ") : delta (Relatif) = " << delta
<< endl
;
167 bool MathTools::isPower2(long i
)
184 bool MathTools::isPower2(int i
) // TODO operateur bit
197 bool MathTools::isPower2(unsigned int i
) // TODO operateur bit
210 /*--------------------------------------*\
211 |* Methode static private *|
212 \*-------------------------------------*/
214 float MathTools::maxAbs(float a
, float b
)
216 return MAXABS(fabs(a
), fabs(b
));
219 float MathTools::maxAbs(float* tabA
, float* tabB
, int n
)
223 for (int i
= 0; i
< n
; i
++)
225 if (fabs(tabA
[i
]) > maxAbs
)
227 maxAbs
= fabs(tabA
[i
]);
229 if (fabs(tabB
[i
]) > maxAbs
)
231 maxAbs
= fabs(tabB
[i
]);
239 /*----------------------------------------------------------------------*\
241 \*---------------------------------------------------------------------*/