49 #include <visp3/core/vpMath.h> 51 #if defined _MSC_VER && _MSC_VER >= 1200 52 #pragma warning(disable : 4723) 61 static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
62 #define NAN (*(const float *)__nan) 70 std::cerr <<
"Fail: IsNaN(0.0)=" <<
vpMath::isNaN(0.0) <<
" / should be false" << std::endl;
74 double num = 1.0, den = 0.0;
76 std::cerr <<
"Fail: IsNaN(1.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be false" << std::endl;
81 std::cerr <<
"Fail: IsNaN(NAN)=" <<
vpMath::isNaN(NAN) <<
" / should be true" << std::endl;
87 std::cerr <<
"Fail: IsNaN(0.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be true" << std::endl;
91 if (!
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())) {
92 std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())
93 <<
" / should be true" << std::endl;
97 if (!
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())) {
98 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())
99 <<
" / should be true" << std::endl;
103 if (
vpMath::isNaN(std::numeric_limits<double>::infinity())) {
104 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<double>::infinity())
105 <<
" / should be false" << std::endl;
109 if (
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())) {
110 std::cerr <<
"Fail: IsNaN(1.0/epsilon)=" <<
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())
111 <<
" / should be false" << std::endl;
115 if (!
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())) {
116 std::cerr <<
"Fail: IsNaN(infinity - infinity)=" 117 <<
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())
118 <<
" / should be true" << std::endl;
122 float a = 0.0f, b = 0.0f;
124 std::cerr <<
"Fail: IsNaN(0.0f/0.0f)=" <<
vpMath::isNaN(a / b) <<
" / should be true" << std::endl;
127 std::cout <<
"vpMath::isNaN is Ok !" << std::endl;
130 #if !defined(VISP_HAVE_FUNC__FINITE) 134 std::cerr <<
"Fail: vpMath::isInf(NAN)=" <<
vpMath::isInf(NAN) <<
" / should be false" << std::endl;
140 std::cerr <<
"Fail: vpMath::isInf(1.0/0.0)=" <<
vpMath::isInf(1.0 / a) <<
" / should be true" << std::endl;
145 std::cerr <<
"Fail: vpMath::isInf(0.0)=" <<
vpMath::isInf(0.0) <<
" / should be false" << std::endl;
150 std::cerr <<
"Fail: vpMath::isInf(exp(800.))=" <<
vpMath::isInf(exp(800.)) <<
" / should be true" << std::endl;
155 std::cerr <<
"Fail: vpMath::isInf(DBL_MIN/2.0)=" <<
vpMath::isInf(DBL_MIN / 2.0) <<
" / should be false" 159 std::cout <<
"vpMath::isInf is Ok !" << std::endl;
163 std::cerr <<
"Fail: vpMath::round(2.3)=" <<
vpMath::round(2.3) <<
" / should be 2" << std::endl;
168 std::cerr <<
"Fail: vpMath::round(3.8)=" <<
vpMath::round(3.8) <<
" / should be 4" << std::endl;
173 std::cerr <<
"Fail: vpMath::round(5.5)=" <<
vpMath::round(5.5) <<
" / should be 6" << std::endl;
178 std::cerr <<
"Fail: vpMath::round(-2.3)=" <<
vpMath::round(-2.3) <<
" / should be -2" << std::endl;
183 std::cerr <<
"Fail: vpMath::round(-3.8)=" <<
vpMath::round(-3.8) <<
" / should be -4" << std::endl;
188 std::cerr <<
"Fail: vpMath::round(-5.5)=" <<
vpMath::round(-5.5) <<
" / should be -6" << std::endl;
193 std::cerr <<
"Fail: vpMath::round(0.0)=" <<
vpMath::round(0.0) <<
" / should be 0" << std::endl;
196 std::cout <<
"vpMath::round is Ok !" << std::endl;
200 char char_value = -127;
201 unsigned char uchar_value = vpMath::saturate<unsigned char>(char_value);
202 if (uchar_value != 0) {
203 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-127)=" << uchar_value <<
" / should be 0" << std::endl;
207 unsigned short ushort_value = 60000;
208 uchar_value = vpMath::saturate<unsigned char>(ushort_value);
209 if (uchar_value != UCHAR_MAX) {
210 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(60000)=" << uchar_value <<
" / should be " << UCHAR_MAX
215 int int_value = 70000;
216 uchar_value = vpMath::saturate<unsigned char>(int_value);
217 if (uchar_value != UCHAR_MAX) {
218 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(70000)=" << uchar_value <<
" / should be " << UCHAR_MAX
224 uchar_value = vpMath::saturate<unsigned char>(int_value);
225 if (uchar_value != 0) {
226 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-70000)=" << uchar_value <<
" / should be 0" << std::endl;
230 short short_value = 30000;
231 uchar_value = vpMath::saturate<unsigned char>(short_value);
232 if (uchar_value != UCHAR_MAX) {
233 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(30000)=" << uchar_value <<
" / should be " << UCHAR_MAX
238 short_value = -30000;
239 uchar_value = vpMath::saturate<unsigned char>(short_value);
240 if (uchar_value != 0) {
241 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-30000)=" << uchar_value <<
" / should be 0" << std::endl;
245 unsigned int uint_value = 10000;
246 uchar_value = vpMath::saturate<unsigned char>(uint_value);
247 if (uchar_value != UCHAR_MAX) {
248 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000)=" << uchar_value <<
" / should be " << UCHAR_MAX
253 float float_value = 10000.1f;
254 uchar_value = vpMath::saturate<unsigned char>(float_value);
255 if (uchar_value != UCHAR_MAX) {
256 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.1f)=" << uchar_value <<
" / should be " << UCHAR_MAX
261 float_value = -10000.1f;
262 uchar_value = vpMath::saturate<unsigned char>(float_value);
263 if (uchar_value != 0) {
264 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.1f)=" << uchar_value <<
" / should be 0" << std::endl;
268 double double_value = 10000.1;
269 uchar_value = vpMath::saturate<unsigned char>(double_value);
270 if (uchar_value != UCHAR_MAX) {
271 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.0)=" << uchar_value <<
" / should be " << UCHAR_MAX
276 double_value = -10000.1;
277 uchar_value = vpMath::saturate<unsigned char>(double_value);
278 if (uchar_value != 0) {
279 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.0)=" << uchar_value <<
" / should be 0" << std::endl;
282 std::cout <<
"vpMath::saturate<unsigned char>() is Ok !" << std::endl;
286 char_value = vpMath::saturate<char>(uchar_value);
287 if (char_value != SCHAR_MAX) {
288 std::cerr <<
"Fail: vpMath::saturate<char>(255)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
292 ushort_value = 60000;
293 char_value = vpMath::saturate<char>(ushort_value);
294 if (char_value != SCHAR_MAX) {
295 std::cerr <<
"Fail: vpMath::saturate<char>(60000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
300 char_value = vpMath::saturate<char>(int_value);
301 if (char_value != SCHAR_MAX) {
302 std::cerr <<
"Fail: vpMath::saturate<char>(70000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
307 char_value = vpMath::saturate<char>(int_value);
308 if (char_value != (
char)SCHAR_MIN) {
309 std::cerr <<
"Fail: vpMath::saturate<char>(-70000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
314 char_value = vpMath::saturate<char>(short_value);
315 if (char_value != SCHAR_MAX) {
316 std::cerr <<
"Fail: vpMath::saturate<char>(30000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
320 short_value = -30000;
321 char_value = vpMath::saturate<char>(short_value);
322 if (char_value != (
char)SCHAR_MIN) {
323 std::cerr <<
"Fail: vpMath::saturate<char>(-30000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
328 char_value = vpMath::saturate<char>(uint_value);
329 if (char_value != SCHAR_MAX) {
330 std::cerr <<
"Fail: vpMath::saturate<char>(10000)=" << 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 != SCHAR_MAX) {
337 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1f)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
341 float_value = -10000.1f;
342 char_value = vpMath::saturate<char>(float_value);
343 if (char_value != (
char)SCHAR_MIN) {
344 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1f)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
348 double_value = 10000.1;
349 char_value = vpMath::saturate<char>(double_value);
350 if (char_value != SCHAR_MAX) {
351 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
355 double_value = -10000.1;
356 char_value = vpMath::saturate<char>(double_value);
357 if (char_value != (
char)SCHAR_MIN) {
358 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
361 std::cout <<
"vpMath::saturate<char>() is Ok !" << std::endl;
365 ushort_value = vpMath::saturate<unsigned short>(char_value);
366 if (ushort_value != 0) {
367 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-127)=" << ushort_value <<
" / should be 0" << std::endl;
371 short_value = -30000;
372 ushort_value = vpMath::saturate<unsigned short>(short_value);
373 if (ushort_value != 0) {
374 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-30000)=" << ushort_value <<
" / should be 0" << std::endl;
379 ushort_value = vpMath::saturate<unsigned short>(int_value);
380 if (ushort_value != USHRT_MAX) {
381 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
387 ushort_value = vpMath::saturate<unsigned short>(int_value);
388 if (ushort_value != 0) {
389 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-70000)=" << ushort_value <<
" / should be 0" << std::endl;
394 ushort_value = vpMath::saturate<unsigned short>(uint_value);
395 if (ushort_value != USHRT_MAX) {
396 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
401 float_value = 70000.1f;
402 ushort_value = vpMath::saturate<unsigned short>(float_value);
403 if (ushort_value != USHRT_MAX) {
404 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1f)=" << ushort_value <<
" / should be " << USHRT_MAX
409 float_value = -10000.1f;
410 ushort_value = vpMath::saturate<unsigned short>(float_value);
411 if (ushort_value != 0) {
412 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1f)=" << ushort_value <<
" / should be 0" << std::endl;
416 double_value = 70000.1;
417 ushort_value = vpMath::saturate<unsigned short>(double_value);
418 if (ushort_value != USHRT_MAX) {
419 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1)=" << ushort_value <<
" / should be " << USHRT_MAX
424 double_value = -10000.1;
425 ushort_value = vpMath::saturate<unsigned short>(double_value);
426 if (ushort_value != 0) {
427 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1)=" << ushort_value <<
" / should be 0" << std::endl;
430 std::cout <<
"vpMath::saturate<unsigned short>() is Ok !" << std::endl;
433 ushort_value = 60000;
434 short_value = vpMath::saturate<short>(ushort_value);
435 if (short_value != SHRT_MAX) {
436 std::cerr <<
"Fail: vpMath::saturate<short>(60000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
441 short_value = vpMath::saturate<short>(int_value);
442 if (short_value != SHRT_MAX) {
443 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
448 short_value = vpMath::saturate<short>(int_value);
449 if (short_value != SHRT_MIN) {
450 std::cerr <<
"Fail: vpMath::saturate<short>(-70000)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
455 short_value = vpMath::saturate<short>(uint_value);
456 if (short_value != SHRT_MAX) {
457 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
461 float_value = 70000.1f;
462 short_value = vpMath::saturate<short>(float_value);
463 if (short_value != SHRT_MAX) {
464 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1f)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
468 float_value = -70000.1f;
469 short_value = vpMath::saturate<short>(float_value);
470 if (short_value != SHRT_MIN) {
471 std::cerr <<
"Fail: vpMath::saturate<short>(-70000.1f)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
475 double_value = 70000.1;
476 short_value = vpMath::saturate<short>(double_value);
477 if (short_value != SHRT_MAX) {
478 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
482 double_value = -70000.1;
483 short_value = vpMath::saturate<short>(double_value);
484 if (short_value != SHRT_MIN) {
485 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
488 std::cout <<
"vpMath::saturate<short>() is Ok !" << std::endl;
492 std::vector<double> vectorOfDoubles(10);
493 vectorOfDoubles[0] = 8.1472;
494 vectorOfDoubles[1] = 9.0579;
495 vectorOfDoubles[2] = 1.2699;
496 vectorOfDoubles[3] = 9.1338;
497 vectorOfDoubles[4] = 6.3236;
498 vectorOfDoubles[5] = 0.9754;
499 vectorOfDoubles[6] = 2.7850;
500 vectorOfDoubles[7] = 5.4688;
501 vectorOfDoubles[8] = 9.5751;
502 vectorOfDoubles[9] = 9.6489;
506 std::cerr <<
"Problem with vpMath::getMean()=" << res << std::endl;
509 std::cout <<
"vpMath::getMean() is Ok !" << std::endl;
513 std::cerr <<
"Problem with vpMath::getStdev()=" << res << std::endl;
519 std::cerr <<
"Problem with vpMath::getStdev() with Bessel correction=" << res << std::endl;
522 std::cout <<
"vpMath::getStdev() is Ok !" << std::endl;
526 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
531 vectorOfDoubles.push_back(1.5761);
534 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
537 std::cout <<
"vpMath::getMedian() is Ok !" << std::endl;
539 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)