gtx_ulp.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2011-04-26
  5. // Updated : 2011-04-26
  6. // Licence : This source is under MIT licence
  7. // File : test/gtx/ulp.cpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <glm/glm.hpp>
  10. #include <glm/gtx/ulp.hpp>
  11. #include <iostream>
  12. #include <limits>
  13. int test_ulp_float_dist()
  14. {
  15. int Error = 0;
  16. float A = 1.0f;
  17. float B = glm::next_float(A);
  18. Error += A != B ? 0 : 1;
  19. float C = glm::prev_float(B);
  20. Error += A == C ? 0 : 1;
  21. int D = glm::float_distance(A, B);
  22. Error += D == 1 ? 0 : 1;
  23. int E = glm::float_distance(A, C);
  24. Error += E == 0 ? 0 : 1;
  25. return Error;
  26. }
  27. int test_ulp_float_step()
  28. {
  29. int Error = 0;
  30. float A = 1.0f;
  31. for(int i = 10; i < 1000; i *= 10)
  32. {
  33. float B = glm::next_float(A, i);
  34. Error += A != B ? 0 : 1;
  35. float C = glm::prev_float(B, i);
  36. Error += A == C ? 0 : 1;
  37. int D = glm::float_distance(A, B);
  38. Error += D == i ? 0 : 1;
  39. int E = glm::float_distance(A, C);
  40. Error += E == 0 ? 0 : 1;
  41. }
  42. return Error;
  43. }
  44. int test_ulp_double_dist()
  45. {
  46. int Error = 0;
  47. double A = 1.0;
  48. double B = glm::next_float(A);
  49. Error += A != B ? 0 : 1;
  50. double C = glm::prev_float(B);
  51. Error += A == C ? 0 : 1;
  52. int D = glm::float_distance(A, B);
  53. Error += D == 1 ? 0 : 1;
  54. int E = glm::float_distance(A, C);
  55. Error += E == 0 ? 0 : 1;
  56. return Error;
  57. }
  58. int test_ulp_double_step()
  59. {
  60. int Error = 0;
  61. double A = 1.0;
  62. for(int i = 10; i < 1000; i *= 10)
  63. {
  64. double B = glm::next_float(A, i);
  65. Error += A != B ? 0 : 1;
  66. double C = glm::prev_float(B, i);
  67. Error += A == C ? 0 : 1;
  68. int D = glm::float_distance(A, B);
  69. Error += D == i ? 0 : 1;
  70. int E = glm::float_distance(A, C);
  71. Error += E == 0 ? 0 : 1;
  72. }
  73. return Error;
  74. }
  75. int main()
  76. {
  77. int Error = 0;
  78. Error += test_ulp_float_dist();
  79. Error += test_ulp_float_step();
  80. Error += test_ulp_double_dist();
  81. Error += test_ulp_double_step();
  82. return Error;
  83. }