gtc_ulp.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/gtc/gtc_ulp.cpp
  28. /// @date 2011-04-26 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtc/ulp.hpp>
  32. #include <limits>
  33. int test_ulp_float_dist()
  34. {
  35. int Error = 0;
  36. float A = 1.0f;
  37. float B = glm::next_float(A);
  38. Error += A != B ? 0 : 1;
  39. float C = glm::prev_float(B);
  40. Error += A == C ? 0 : 1;
  41. int D = glm::float_distance(A, B);
  42. Error += D == 1 ? 0 : 1;
  43. int E = glm::float_distance(A, C);
  44. Error += E == 0 ? 0 : 1;
  45. return Error;
  46. }
  47. int test_ulp_float_step()
  48. {
  49. int Error = 0;
  50. float A = 1.0f;
  51. for(int i = 10; i < 1000; i *= 10)
  52. {
  53. float B = glm::next_float(A, i);
  54. Error += A != B ? 0 : 1;
  55. float C = glm::prev_float(B, i);
  56. Error += A == C ? 0 : 1;
  57. int D = glm::float_distance(A, B);
  58. Error += D == i ? 0 : 1;
  59. int E = glm::float_distance(A, C);
  60. Error += E == 0 ? 0 : 1;
  61. }
  62. return Error;
  63. }
  64. int test_ulp_double_dist()
  65. {
  66. int Error = 0;
  67. double A = 1.0;
  68. double B = glm::next_float(A);
  69. Error += A != B ? 0 : 1;
  70. double C = glm::prev_float(B);
  71. Error += A == C ? 0 : 1;
  72. int D = glm::float_distance(A, B);
  73. Error += D == 1 ? 0 : 1;
  74. int E = glm::float_distance(A, C);
  75. Error += E == 0 ? 0 : 1;
  76. return Error;
  77. }
  78. int test_ulp_double_step()
  79. {
  80. int Error = 0;
  81. double A = 1.0;
  82. for(int i = 10; i < 1000; i *= 10)
  83. {
  84. double B = glm::next_float(A, i);
  85. Error += A != B ? 0 : 1;
  86. double C = glm::prev_float(B, i);
  87. Error += A == C ? 0 : 1;
  88. int D = glm::float_distance(A, B);
  89. Error += D == i ? 0 : 1;
  90. int E = glm::float_distance(A, C);
  91. Error += E == 0 ? 0 : 1;
  92. }
  93. return Error;
  94. }
  95. int main()
  96. {
  97. int Error = 0;
  98. Error += test_ulp_float_dist();
  99. Error += test_ulp_float_step();
  100. Error += test_ulp_double_dist();
  101. Error += test_ulp_double_step();
  102. return Error;
  103. }