48 #include <visp3/core/vpMath.h>
50 #if defined _MSC_VER && _MSC_VER >= 1200
51 #pragma warning( disable: 4723 )
60 static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
61 #define NAN (*(const float *) __nan)
68 std::cerr <<
"Fail: IsNaN(0.0)=" <<
vpMath::isNaN(0.0) <<
" / should be false" << std::endl;
72 double num = 1.0, den = 0.0;
74 std::cerr <<
"Fail: IsNaN(1.0/0.0)=" <<
vpMath::isNaN(num/den) <<
" / should be false" << std::endl;
79 std::cerr <<
"Fail: IsNaN(NAN)=" <<
vpMath::isNaN(NAN) <<
" / should be true" << std::endl;
85 std::cerr <<
"Fail: IsNaN(0.0/0.0)=" <<
vpMath::isNaN(num/den) <<
" / should be true" << std::endl;
89 if(!
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())) {
90 std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())
91 <<
" / should be true" << std::endl;
95 if(!
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())) {
96 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())
97 <<
" / should be true" << std::endl;
102 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<double>::infinity())
103 <<
" / should be false" << std::endl;
107 if(
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())) {
108 std::cerr <<
"Fail: IsNaN(1.0/epsilon)=" <<
vpMath::isNaN(1.0/std::numeric_limits<double>::epsilon())
109 <<
" / should be false" << std::endl;
113 if(!
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())) {
114 std::cerr <<
"Fail: IsNaN(1.0/epsilon)=" <<
vpMath::isNaN(1.0/std::numeric_limits<double>::epsilon())
115 <<
" / should be true" << std::endl;
119 float a = 0.0f, b = 0.0f;
121 std::cerr <<
"Fail: IsNaN(0.0f/0.0f)=" <<
vpMath::isNaN(a/b) <<
" / should be true" << std::endl;
124 std::cout <<
"vpMath::isNaN is Ok !" << std::endl;
128 #if !defined(VISP_HAVE_FUNC__FINITE)
131 std::cerr <<
"Fail: vpMath::isInf(NAN)=" <<
vpMath::isInf(NAN) <<
" / should be false" << std::endl;
137 std::cerr <<
"Fail: vpMath::isInf(1.0/0.0)=" <<
vpMath::isInf(1.0/a) <<
" / should be true" << std::endl;
142 std::cerr <<
"Fail: vpMath::isInf(0.0)=" <<
vpMath::isInf(0.0) <<
" / should be false" << std::endl;
147 std::cerr <<
"Fail: vpMath::isInf(exp(800.))=" <<
vpMath::isInf(exp(800.)) <<
" / should be true" << std::endl;
152 std::cerr <<
"Fail: vpMath::isInf(DBL_MIN/2.0)=" <<
vpMath::isInf(DBL_MIN/2.0) <<
" / should be false" << std::endl;
155 std::cout <<
"vpMath::isInf is Ok !" << std::endl;
160 std::cerr <<
"Fail: vpMath::round(2.3)=" <<
vpMath::round(2.3) <<
" / should be 2" << std::endl;
165 std::cerr <<
"Fail: vpMath::round(3.8)=" <<
vpMath::round(3.8) <<
" / should be 4" << std::endl;
170 std::cerr <<
"Fail: vpMath::round(5.5)=" <<
vpMath::round(5.5) <<
" / should be 6" << std::endl;
175 std::cerr <<
"Fail: vpMath::round(-2.3)=" <<
vpMath::round(-2.3) <<
" / should be -2" << std::endl;
180 std::cerr <<
"Fail: vpMath::round(-3.8)=" <<
vpMath::round(-3.8) <<
" / should be -4" << std::endl;
185 std::cerr <<
"Fail: vpMath::round(-5.5)=" <<
vpMath::round(-5.5) <<
" / should be -6" << std::endl;
190 std::cerr <<
"Fail: vpMath::round(0.0)=" <<
vpMath::round(0.0) <<
" / should be 0" << std::endl;
193 std::cout <<
"vpMath::round is Ok !" << std::endl;
198 char char_value = -127;
199 unsigned char uchar_value = vpMath::saturate<unsigned char>(char_value);
200 if(uchar_value != 0) {
201 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-127)=" << uchar_value <<
" / should be 0" << std::endl;
205 unsigned short ushort_value = 60000;
206 uchar_value = vpMath::saturate<unsigned char>(ushort_value);
207 if(uchar_value != UCHAR_MAX) {
208 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(60000)=" << uchar_value <<
" / should be " << UCHAR_MAX << std::endl;
212 int int_value = 70000;
213 uchar_value = vpMath::saturate<unsigned char>(int_value);
214 if(uchar_value != UCHAR_MAX) {
215 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(70000)=" << uchar_value <<
" / should be " << UCHAR_MAX << std::endl;
220 uchar_value = vpMath::saturate<unsigned char>(int_value);
221 if(uchar_value != 0) {
222 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-70000)=" << uchar_value <<
" / should be 0" << std::endl;
226 short short_value = 30000;
227 uchar_value = vpMath::saturate<unsigned char>(short_value);
228 if(uchar_value != UCHAR_MAX) {
229 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(30000)=" << uchar_value <<
" / should be " << UCHAR_MAX << std::endl;
233 short_value = -30000;
234 uchar_value = vpMath::saturate<unsigned char>(short_value);
235 if(uchar_value != 0) {
236 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-30000)=" << uchar_value <<
" / should be 0" << std::endl;
240 unsigned int uint_value = 10000;
241 uchar_value = vpMath::saturate<unsigned char>(uint_value);
242 if(uchar_value != UCHAR_MAX) {
243 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000)=" << uchar_value <<
" / should be " << UCHAR_MAX << std::endl;
247 float float_value = 10000.1f;
248 uchar_value = vpMath::saturate<unsigned char>(float_value);
249 if(uchar_value != UCHAR_MAX) {
250 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.1f)=" << uchar_value <<
" / should be " << UCHAR_MAX << std::endl;
254 float_value = -10000.1f;
255 uchar_value = vpMath::saturate<unsigned char>(float_value);
256 if(uchar_value != 0) {
257 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.1f)=" << uchar_value <<
" / should be 0" << std::endl;
261 double double_value = 10000.1;
262 uchar_value = vpMath::saturate<unsigned char>(double_value);
263 if(uchar_value != UCHAR_MAX) {
264 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.0)=" << uchar_value <<
" / should be " << UCHAR_MAX << std::endl;
268 double_value = -10000.1;
269 uchar_value = vpMath::saturate<unsigned char>(double_value);
270 if(uchar_value != 0) {
271 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.0)=" << uchar_value <<
" / should be 0" << std::endl;
274 std::cout <<
"vpMath::saturate<unsigned char>() is Ok !" << std::endl;
279 char_value = vpMath::saturate<char>(uchar_value);
280 if(char_value != SCHAR_MAX) {
281 std::cerr <<
"Fail: vpMath::saturate<char>(255)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
285 ushort_value = 60000;
286 char_value = vpMath::saturate<char>(ushort_value);
287 if(char_value != SCHAR_MAX) {
288 std::cerr <<
"Fail: vpMath::saturate<char>(60000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
293 char_value = vpMath::saturate<char>(int_value);
294 if(char_value != SCHAR_MAX) {
295 std::cerr <<
"Fail: vpMath::saturate<char>(70000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
300 char_value = vpMath::saturate<char>(int_value);
301 if(char_value != (
char)SCHAR_MIN) {
302 std::cerr <<
"Fail: vpMath::saturate<char>(-70000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
307 char_value = vpMath::saturate<char>(short_value);
308 if(char_value != SCHAR_MAX) {
309 std::cerr <<
"Fail: vpMath::saturate<char>(30000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
313 short_value = -30000;
314 char_value = vpMath::saturate<char>(short_value);
315 if(char_value != (
char)SCHAR_MIN) {
316 std::cerr <<
"Fail: vpMath::saturate<char>(-30000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
321 char_value = vpMath::saturate<char>(uint_value);
322 if(char_value != SCHAR_MAX) {
323 std::cerr <<
"Fail: vpMath::saturate<char>(10000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
327 float_value = 10000.1f;
328 char_value = vpMath::saturate<char>(float_value);
329 if(char_value != SCHAR_MAX) {
330 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1f)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
334 float_value = -10000.1f;
335 char_value = vpMath::saturate<char>(float_value);
336 if(char_value != (
char)SCHAR_MIN) {
337 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1f)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
341 double_value = 10000.1;
342 char_value = vpMath::saturate<char>(double_value);
343 if(char_value != SCHAR_MAX) {
344 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
348 double_value = -10000.1;
349 char_value = vpMath::saturate<char>(double_value);
350 if(char_value != (
char)SCHAR_MIN) {
351 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
354 std::cout <<
"vpMath::saturate<char>() is Ok !" << std::endl;
359 ushort_value = vpMath::saturate<unsigned short>(char_value);
360 if(ushort_value != 0) {
361 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-127)=" << ushort_value <<
" / should be 0" << std::endl;
365 short_value = -30000;
366 ushort_value = vpMath::saturate<unsigned short>(short_value);
367 if(ushort_value != 0) {
368 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-30000)=" << ushort_value <<
" / should be 0" << std::endl;
373 ushort_value = vpMath::saturate<unsigned short>(int_value);
374 if(ushort_value != USHRT_MAX) {
375 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX << std::endl;
380 ushort_value = vpMath::saturate<unsigned short>(int_value);
381 if(ushort_value != 0) {
382 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-70000)=" << ushort_value <<
" / should be 0" << std::endl;
387 ushort_value = vpMath::saturate<unsigned short>(uint_value);
388 if(ushort_value != USHRT_MAX) {
389 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX << std::endl;
393 float_value = 70000.1f;
394 ushort_value = vpMath::saturate<unsigned short>(float_value);
395 if(ushort_value != USHRT_MAX) {
396 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1f)=" << ushort_value <<
" / should be " << USHRT_MAX << std::endl;
400 float_value = -10000.1f;
401 ushort_value = vpMath::saturate<unsigned short>(float_value);
402 if(ushort_value != 0) {
403 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1f)=" << ushort_value <<
" / should be 0" << std::endl;
407 double_value = 70000.1;
408 ushort_value = vpMath::saturate<unsigned short>(double_value);
409 if(ushort_value != USHRT_MAX) {
410 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1)=" << ushort_value <<
" / should be " << USHRT_MAX << std::endl;
414 double_value = -10000.1;
415 ushort_value = vpMath::saturate<unsigned short>(double_value);
416 if(ushort_value != 0) {
417 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1)=" << ushort_value <<
" / should be 0" << std::endl;
420 std::cout <<
"vpMath::saturate<unsigned short>() is Ok !" << std::endl;
424 ushort_value = 60000;
425 short_value = vpMath::saturate<short>(ushort_value);
426 if(short_value != SHRT_MAX) {
427 std::cerr <<
"Fail: vpMath::saturate<short>(60000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
432 short_value = vpMath::saturate<short>(int_value);
433 if(short_value != SHRT_MAX) {
434 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
439 short_value = vpMath::saturate<short>(int_value);
440 if(short_value != SHRT_MIN) {
441 std::cerr <<
"Fail: vpMath::saturate<short>(-70000)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
446 short_value = vpMath::saturate<short>(uint_value);
447 if(short_value != SHRT_MAX) {
448 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
452 float_value = 70000.1f;
453 short_value = vpMath::saturate<short>(float_value);
454 if(short_value != SHRT_MAX) {
455 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1f)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
459 float_value = -70000.1f;
460 short_value = vpMath::saturate<short>(float_value);
461 if(short_value != SHRT_MIN) {
462 std::cerr <<
"Fail: vpMath::saturate<short>(-70000.1f)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
466 double_value = 70000.1;
467 short_value = vpMath::saturate<short>(double_value);
468 if(short_value != SHRT_MAX) {
469 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
473 double_value = -70000.1;
474 short_value = vpMath::saturate<short>(double_value);
475 if(short_value != SHRT_MIN) {
476 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
479 std::cout <<
"vpMath::saturate<short>() is Ok !" << std::endl;
483 std::vector<double> vectorOfDoubles(10);
484 vectorOfDoubles[0] = 8.1472;
485 vectorOfDoubles[1] = 9.0579;
486 vectorOfDoubles[2] = 1.2699;
487 vectorOfDoubles[3] = 9.1338;
488 vectorOfDoubles[4] = 6.3236;
489 vectorOfDoubles[5] = 0.9754;
490 vectorOfDoubles[6] = 2.7850;
491 vectorOfDoubles[7] = 5.4688;
492 vectorOfDoubles[8] = 9.5751;
493 vectorOfDoubles[9] = 9.6489;
498 std::cerr <<
"Problem with vpMath::getMean()=" << res << std::endl;
501 std::cout <<
"vpMath::getMean() is Ok !" << std::endl;
505 std::cerr <<
"Problem with vpMath::getStdev()=" << res << std::endl;
511 std::cerr <<
"Problem with vpMath::getStdev() with Bessel correction=" << res << std::endl;
514 std::cout <<
"vpMath::getStdev() is Ok !" << std::endl;
518 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
523 vectorOfDoubles.push_back(1.5761);
526 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
529 std::cout <<
"vpMath::getMedian() is Ok !" << std::endl;
532 std::cout <<
"OK !" << std::endl;
static double getStdev(const std::vector< double > &v, const bool useBesselCorrection=false)
static double getMedian(const std::vector< double > &v)
static bool isNaN(const double value)
static bool equal(double x, double y, double s=0.001)
static bool isInf(const double value)
static int round(const double x)
static double getMean(const std::vector< double > &v)