glm/test/gtx/gtx-ulp.cpp

143 lines
3.7 KiB
C++
Raw Normal View History

2011-04-26 11:29:56 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
// OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2011-04-26
// Updated : 2011-04-26
// Licence : This source is under MIT licence
// File : test/gtx/ulp.cpp
///////////////////////////////////////////////////////////////////////////////////////////////////
#include <glm/glm.hpp>
#include <glm/gtx/ulp.hpp>
#include <iostream>
2011-05-03 23:54:35 +00:00
#include <limits>
namespace
{
template <typename T>
2011-05-04 09:21:25 +00:00
T next_float(T const & x);
2011-05-03 23:54:35 +00:00
#if(GLM_COMPILER & GLM_COMPILER_VC)
# include <cfloat>
2011-05-04 09:21:25 +00:00
# define GLM_NEXT_AFTER _nextafterf
#else
# include <cmath>
# define GLM_NEXT_AFTER nextafterf
#endif
GLM_FUNC_QUALIFIER float next_float(float const & x)
2011-05-03 23:54:35 +00:00
{
2011-05-04 09:21:25 +00:00
return GLM_NEXT_AFTER(x, std::numeric_limits<float>::max());
2011-05-03 23:54:35 +00:00
}
2011-05-04 09:21:25 +00:00
GLM_FUNC_QUALIFIER double next_float(double const & x)
2011-05-03 23:54:35 +00:00
{
2011-05-04 09:21:25 +00:00
return GLM_NEXT_AFTER(x, std::numeric_limits<double>::max());
2011-05-03 23:54:35 +00:00
}
2011-05-04 09:21:25 +00:00
GLM_FUNC_QUALIFIER float prev_float(float const & x)
2011-05-03 23:54:35 +00:00
{
2011-05-04 09:21:25 +00:00
return GLM_NEXT_AFTER(x, std::numeric_limits<float>::min());
2011-05-03 23:54:35 +00:00
}
2011-05-04 09:21:25 +00:00
GLM_FUNC_QUALIFIER double prev_float(double const & x)
2011-05-03 23:54:35 +00:00
{
2011-05-04 09:21:25 +00:00
return GLM_NEXT_AFTER(x, std::numeric_limits<double>::min());
2011-05-03 23:54:35 +00:00
}
template <typename T>
2011-05-04 09:21:25 +00:00
GLM_FUNC_QUALIFIER T next_float(T const & x, std::size_t const & ulps)
2011-05-03 23:54:35 +00:00
{
T temp = x;
2011-05-04 09:21:25 +00:00
for(std::size_t i = 0; i < ulps; ++i)
temp = next_float(temp);
2011-05-03 23:54:35 +00:00
return temp;
}
template <typename T>
2011-05-04 09:21:25 +00:00
GLM_FUNC_QUALIFIER T prev_float(T const & x, std::size_t const & ulps)
2011-05-03 23:54:35 +00:00
{
T temp = x;
2011-05-04 09:21:25 +00:00
for(std::size_t i = 0; i < ulps; ++i)
temp = prev_float(temp);
return temp;
}
template <typename T>
GLM_FUNC_QUALIFIER std::size_t float_distance(T const & x, T const & y)
{
std::size_t ulp = 0;
if(x < y)
{
T temp = x;
while(temp != y && ulp < std::numeric_limits<std::size_t>::max())
{
++ulp;
temp = next_float(temp);
}
}
else if(y < x)
{
T temp = y;
while(temp != x && ulp < std::numeric_limits<std::size_t>::max())
{
++ulp;
temp = next_float(temp);
}
}
else // ==
{
}
2011-05-03 23:54:35 +00:00
return ulp;
}
}//namespace
2011-04-26 11:29:56 +00:00
int test_ulp_float()
{
2011-05-03 23:54:35 +00:00
std::cout.precision(std::numeric_limits<double>::digits10 + 1);
float W = 1.0f;
2011-05-04 09:21:25 +00:00
float X = next_float(W);
double Y = 1.0;
double Z = next_float(Y);
2011-05-03 23:54:35 +00:00
bool TestX = W != X;
2011-05-04 09:21:25 +00:00
bool TestZ = Y != Z;
std::cout << "1.0f, Next: " << float_distance(W, X)<< std::endl;
std::cout << "1.0f, Next(1000): " << float_distance(W, next_float(W, 1000)) << std::endl;
std::cout << "1.0f, Next: " << float_distance(X, W)<< std::endl;
std::cout << "1.0f, Next(1000): " << float_distance(next_float(W, 1000), W) << std::endl;
std::cout << "1.0, Next: " << float_distance(Y, Z) << std::endl;
std::cout << "1.0, Next(1000): " << float_distance(next_float(Y, 1000), Y) << std::endl;
2011-05-03 23:54:35 +00:00
std::cout << Z << " 0.01, 0.011" << std::endl;
2011-05-04 09:21:25 +00:00
std::cout << " 1.0, Next(1000000): " << next_float(Y, 1000000)<< std::endl;
2011-04-26 13:46:30 +00:00
2011-04-26 13:24:45 +00:00
std::size_t A = glm::ulp(0.01, 0.02);
std::size_t B = glm::ulp(glm::vec2(0.01), glm::vec2(0.02));
std::size_t C = glm::ulp(glm::vec3(0.01), glm::vec3(0.02));
std::size_t D = glm::ulp(glm::vec4(0.01), glm::vec4(0.02));
2011-04-26 11:29:56 +00:00
std::cout << "glm::ulp test: " << A << std::endl;
std::cout << "glm::ulp test: " << B << std::endl;
std::cout << "glm::ulp test: " << C << std::endl;
std::cout << "glm::ulp test: " << D << std::endl;
return 0;
}
int main()
{
2011-04-26 13:46:30 +00:00
std::cout << "Test 76" << std::endl;
2011-04-26 11:29:56 +00:00
test_ulp_float();
}