literal.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_DETAIL_LITERAL_HPP
  11. #define BOOST_COMPUTE_DETAIL_LITERAL_HPP
  12. #include <iomanip>
  13. #include <limits>
  14. #include <sstream>
  15. #include <boost/type_traits/is_same.hpp>
  16. #include <boost/compute/types/fundamental.hpp>
  17. namespace boost {
  18. namespace compute {
  19. namespace detail {
  20. template<class T>
  21. std::string make_literal(T x)
  22. {
  23. std::stringstream s;
  24. s << std::setprecision(
  25. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  26. std::numeric_limits<T>::max_digits10
  27. #else
  28. // We don't have max_digits10, so add 3 other digits (this is what is required for
  29. // float, and is one more than required for double).
  30. 3 + std::numeric_limits<T>::digits10
  31. #endif
  32. )
  33. << std::scientific
  34. << x;
  35. if(boost::is_same<T, float>::value || boost::is_same<T, float_>::value){
  36. s << "f";
  37. }
  38. return s.str();
  39. }
  40. } // end detail namespace
  41. } // end compute namespace
  42. } // end boost namespace
  43. #endif // BOOST_COMPUTE_DETAIL_LITERAL_HPP