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<float>::quiet_NaN())) {
98 std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::quiet_NaN())
99 <<
" / should be true" << std::endl;
103 if (!
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())) {
104 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())
105 <<
" / should be true" << std::endl;
109 if (!
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())) {
110 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())
111 <<
" / should be true" << std::endl;
115 if (
vpMath::isNaN(std::numeric_limits<double>::infinity())) {
116 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<double>::infinity())
117 <<
" / should be false" << std::endl;
122 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<float>::infinity())
123 <<
" / should be false" << std::endl;
127 if (
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())) {
128 std::cerr <<
"Fail: IsNaN(1.0/epsilon)=" <<
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())
129 <<
" / should be false" << std::endl;
133 if (!
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())) {
134 std::cerr <<
"Fail: IsNaN(infinity - infinity)=" 135 <<
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())
136 <<
" / should be true" << std::endl;
140 float a = 0.0f, b = 0.0f;
142 std::cerr <<
"Fail: IsNaN(0.0f/0.0f)=" <<
vpMath::isNaN(a / b) <<
" / should be true" << std::endl;
145 std::cout <<
"vpMath::isNaN is Ok !" << std::endl;
148 #if !defined(VISP_HAVE_FUNC__FINITE) 152 std::cerr <<
"Fail: vpMath::isInf(NAN)=" <<
vpMath::isInf(NAN) <<
" / should be false" << std::endl;
158 std::cerr <<
"Fail: vpMath::isInf(1.0/0.0)=" <<
vpMath::isInf(1.0 / a) <<
" / should be true" << std::endl;
163 std::cerr <<
"Fail: vpMath::isInf(0.0)=" <<
vpMath::isInf(0.0) <<
" / should be false" << std::endl;
168 std::cerr <<
"Fail: vpMath::isInf(exp(800.))=" <<
vpMath::isInf(exp(800.)) <<
" / should be true" << std::endl;
173 std::cerr <<
"Fail: vpMath::isInf(DBL_MIN/2.0)=" <<
vpMath::isInf(DBL_MIN / 2.0) <<
" / should be false" 177 std::cout <<
"vpMath::isInf is Ok !" << std::endl;
181 std::cerr <<
"Fail: vpMath::round(2.3)=" <<
vpMath::round(2.3) <<
" / should be 2" << std::endl;
186 std::cerr <<
"Fail: vpMath::round(3.8)=" <<
vpMath::round(3.8) <<
" / should be 4" << std::endl;
191 std::cerr <<
"Fail: vpMath::round(5.5)=" <<
vpMath::round(5.5) <<
" / should be 6" << std::endl;
196 std::cerr <<
"Fail: vpMath::round(-2.3)=" <<
vpMath::round(-2.3) <<
" / should be -2" << std::endl;
201 std::cerr <<
"Fail: vpMath::round(-3.8)=" <<
vpMath::round(-3.8) <<
" / should be -4" << std::endl;
206 std::cerr <<
"Fail: vpMath::round(-5.5)=" <<
vpMath::round(-5.5) <<
" / should be -6" << std::endl;
211 std::cerr <<
"Fail: vpMath::round(0.0)=" <<
vpMath::round(0.0) <<
" / should be 0" << std::endl;
214 std::cout <<
"vpMath::round is Ok !" << std::endl;
218 char char_value = -127;
219 unsigned char uchar_value = vpMath::saturate<unsigned char>(char_value);
220 if (uchar_value != 0) {
221 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-127)=" << uchar_value <<
" / should be 0" << std::endl;
225 unsigned short ushort_value = 60000;
226 uchar_value = vpMath::saturate<unsigned char>(ushort_value);
227 if (uchar_value != UCHAR_MAX) {
228 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(60000)=" << uchar_value <<
" / should be " << UCHAR_MAX
233 int int_value = 70000;
234 uchar_value = vpMath::saturate<unsigned char>(int_value);
235 if (uchar_value != UCHAR_MAX) {
236 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(70000)=" << uchar_value <<
" / should be " << UCHAR_MAX
242 uchar_value = vpMath::saturate<unsigned char>(int_value);
243 if (uchar_value != 0) {
244 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-70000)=" << uchar_value <<
" / should be 0" << std::endl;
248 short short_value = 30000;
249 uchar_value = vpMath::saturate<unsigned char>(short_value);
250 if (uchar_value != UCHAR_MAX) {
251 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(30000)=" << uchar_value <<
" / should be " << UCHAR_MAX
256 short_value = -30000;
257 uchar_value = vpMath::saturate<unsigned char>(short_value);
258 if (uchar_value != 0) {
259 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-30000)=" << uchar_value <<
" / should be 0" << std::endl;
263 unsigned int uint_value = 10000;
264 uchar_value = vpMath::saturate<unsigned char>(uint_value);
265 if (uchar_value != UCHAR_MAX) {
266 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000)=" << uchar_value <<
" / should be " << UCHAR_MAX
271 float float_value = 10000.1f;
272 uchar_value = vpMath::saturate<unsigned char>(float_value);
273 if (uchar_value != UCHAR_MAX) {
274 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.1f)=" << uchar_value <<
" / should be " << UCHAR_MAX
279 float_value = -10000.1f;
280 uchar_value = vpMath::saturate<unsigned char>(float_value);
281 if (uchar_value != 0) {
282 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.1f)=" << uchar_value <<
" / should be 0" << std::endl;
286 double double_value = 10000.1;
287 uchar_value = vpMath::saturate<unsigned char>(double_value);
288 if (uchar_value != UCHAR_MAX) {
289 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.0)=" << uchar_value <<
" / should be " << UCHAR_MAX
294 double_value = -10000.1;
295 uchar_value = vpMath::saturate<unsigned char>(double_value);
296 if (uchar_value != 0) {
297 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.0)=" << uchar_value <<
" / should be 0" << std::endl;
300 std::cout <<
"vpMath::saturate<unsigned char>() is Ok !" << std::endl;
304 char_value = vpMath::saturate<char>(uchar_value);
305 if (char_value != SCHAR_MAX) {
306 std::cerr <<
"Fail: vpMath::saturate<char>(255)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
310 ushort_value = 60000;
311 char_value = vpMath::saturate<char>(ushort_value);
312 if (char_value != SCHAR_MAX) {
313 std::cerr <<
"Fail: vpMath::saturate<char>(60000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
318 char_value = vpMath::saturate<char>(int_value);
319 if (char_value != SCHAR_MAX) {
320 std::cerr <<
"Fail: vpMath::saturate<char>(70000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
325 char_value = vpMath::saturate<char>(int_value);
326 if (char_value != (
char)SCHAR_MIN) {
327 std::cerr <<
"Fail: vpMath::saturate<char>(-70000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
332 char_value = vpMath::saturate<char>(short_value);
333 if (char_value != SCHAR_MAX) {
334 std::cerr <<
"Fail: vpMath::saturate<char>(30000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
338 short_value = -30000;
339 char_value = vpMath::saturate<char>(short_value);
340 if (char_value != (
char)SCHAR_MIN) {
341 std::cerr <<
"Fail: vpMath::saturate<char>(-30000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
346 char_value = vpMath::saturate<char>(uint_value);
347 if (char_value != SCHAR_MAX) {
348 std::cerr <<
"Fail: vpMath::saturate<char>(10000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
352 float_value = 10000.1f;
353 char_value = vpMath::saturate<char>(float_value);
354 if (char_value != SCHAR_MAX) {
355 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1f)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
359 float_value = -10000.1f;
360 char_value = vpMath::saturate<char>(float_value);
361 if (char_value != (
char)SCHAR_MIN) {
362 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1f)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
366 double_value = 10000.1;
367 char_value = vpMath::saturate<char>(double_value);
368 if (char_value != SCHAR_MAX) {
369 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
373 double_value = -10000.1;
374 char_value = vpMath::saturate<char>(double_value);
375 if (char_value != (
char)SCHAR_MIN) {
376 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
379 std::cout <<
"vpMath::saturate<char>() is Ok !" << std::endl;
383 ushort_value = vpMath::saturate<unsigned short>(char_value);
384 if (ushort_value != 0) {
385 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-127)=" << ushort_value <<
" / should be 0" << std::endl;
389 short_value = -30000;
390 ushort_value = vpMath::saturate<unsigned short>(short_value);
391 if (ushort_value != 0) {
392 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-30000)=" << ushort_value <<
" / should be 0" << std::endl;
397 ushort_value = vpMath::saturate<unsigned short>(int_value);
398 if (ushort_value != USHRT_MAX) {
399 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
405 ushort_value = vpMath::saturate<unsigned short>(int_value);
406 if (ushort_value != 0) {
407 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-70000)=" << ushort_value <<
" / should be 0" << std::endl;
412 ushort_value = vpMath::saturate<unsigned short>(uint_value);
413 if (ushort_value != USHRT_MAX) {
414 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
419 float_value = 70000.1f;
420 ushort_value = vpMath::saturate<unsigned short>(float_value);
421 if (ushort_value != USHRT_MAX) {
422 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1f)=" << ushort_value <<
" / should be " << USHRT_MAX
427 float_value = -10000.1f;
428 ushort_value = vpMath::saturate<unsigned short>(float_value);
429 if (ushort_value != 0) {
430 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1f)=" << ushort_value <<
" / should be 0" << std::endl;
434 double_value = 70000.1;
435 ushort_value = vpMath::saturate<unsigned short>(double_value);
436 if (ushort_value != USHRT_MAX) {
437 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1)=" << ushort_value <<
" / should be " << USHRT_MAX
442 double_value = -10000.1;
443 ushort_value = vpMath::saturate<unsigned short>(double_value);
444 if (ushort_value != 0) {
445 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1)=" << ushort_value <<
" / should be 0" << std::endl;
448 std::cout <<
"vpMath::saturate<unsigned short>() is Ok !" << std::endl;
451 ushort_value = 60000;
452 short_value = vpMath::saturate<short>(ushort_value);
453 if (short_value != SHRT_MAX) {
454 std::cerr <<
"Fail: vpMath::saturate<short>(60000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
459 short_value = vpMath::saturate<short>(int_value);
460 if (short_value != SHRT_MAX) {
461 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
466 short_value = vpMath::saturate<short>(int_value);
467 if (short_value != SHRT_MIN) {
468 std::cerr <<
"Fail: vpMath::saturate<short>(-70000)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
473 short_value = vpMath::saturate<short>(uint_value);
474 if (short_value != SHRT_MAX) {
475 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
479 float_value = 70000.1f;
480 short_value = vpMath::saturate<short>(float_value);
481 if (short_value != SHRT_MAX) {
482 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1f)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
486 float_value = -70000.1f;
487 short_value = vpMath::saturate<short>(float_value);
488 if (short_value != SHRT_MIN) {
489 std::cerr <<
"Fail: vpMath::saturate<short>(-70000.1f)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
493 double_value = 70000.1;
494 short_value = vpMath::saturate<short>(double_value);
495 if (short_value != SHRT_MAX) {
496 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
500 double_value = -70000.1;
501 short_value = vpMath::saturate<short>(double_value);
502 if (short_value != SHRT_MIN) {
503 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
506 std::cout <<
"vpMath::saturate<short>() is Ok !" << std::endl;
510 std::vector<double> vectorOfDoubles(10);
511 vectorOfDoubles[0] = 8.1472;
512 vectorOfDoubles[1] = 9.0579;
513 vectorOfDoubles[2] = 1.2699;
514 vectorOfDoubles[3] = 9.1338;
515 vectorOfDoubles[4] = 6.3236;
516 vectorOfDoubles[5] = 0.9754;
517 vectorOfDoubles[6] = 2.7850;
518 vectorOfDoubles[7] = 5.4688;
519 vectorOfDoubles[8] = 9.5751;
520 vectorOfDoubles[9] = 9.6489;
524 std::cerr <<
"Problem with vpMath::getMean()=" << res << std::endl;
527 std::cout <<
"vpMath::getMean() is Ok !" << std::endl;
531 std::cerr <<
"Problem with vpMath::getStdev()=" << res << std::endl;
537 std::cerr <<
"Problem with vpMath::getStdev() with Bessel correction=" << res << std::endl;
540 std::cout <<
"vpMath::getStdev() is Ok !" << std::endl;
544 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
549 vectorOfDoubles.push_back(1.5761);
552 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
555 std::cout <<
"vpMath::getMedian() is Ok !" << std::endl;
557 std::cout <<
"OK !" << std::endl;
static double getMedian(const std::vector< double > &v)
static bool equal(double x, double y, double s=0.001)
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
static double getMean(const std::vector< double > &v)
static bool isNaN(double value)
static int round(double x)
static bool isInf(double value)