1 /*============================================================================== 2 Copyright (c) 2001-2010 Hartmut Kaiser 3 Copyright (c) 2010 Bryce Lelbach 4 5 Distributed under the Boost Software License, Version 1.0. (See accompanying 6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 ==============================================================================*/ 8 9 #if !defined(BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP) 10 #define BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP 11 12 #include <boost/version.hpp> 13 #include <boost/config/warning_disable.hpp> 14 #include <boost/detail/lightweight_test.hpp> 15 #include <boost/math/concepts/real_concept.hpp> 16 17 #include <boost/spirit/include/karma_char.hpp> 18 #include <boost/spirit/include/karma_numeric.hpp> 19 #include <boost/spirit/include/karma_generate.hpp> 20 #include <boost/spirit/include/karma_directive.hpp> 21 #include <boost/spirit/include/karma_phoenix_attributes.hpp> 22 23 #include <boost/spirit/include/phoenix_core.hpp> 24 #include <boost/spirit/include/phoenix_operator.hpp> 25 #include <boost/spirit/include/phoenix_statement.hpp> 26 27 #include <boost/limits.hpp> 28 #include "test.hpp" 29 30 using namespace spirit_test; 31 32 /////////////////////////////////////////////////////////////////////////////// 33 // policy for real_generator, which forces the scientific notation 34 template <typename T> 35 struct scientific_policy : boost::spirit::karma::real_policies<T> 36 { 37 // we want the numbers always to be in scientific format 38 typedef boost::spirit::karma::real_policies<T> base_type; floatfieldscientific_policy39 static int floatfield(T) { return base_type::fmtflags::scientific; } 40 }; 41 42 /////////////////////////////////////////////////////////////////////////////// 43 // policy for real_generator, which forces the fixed notation 44 template <typename T> 45 struct fixed_policy : boost::spirit::karma::real_policies<T> 46 { 47 typedef boost::spirit::karma::real_policies<T> base_type; 48 49 // we want the numbers always to be in scientific format floatfieldfixed_policy50 static int floatfield(T) { return base_type::fmtflags::fixed; } 51 }; 52 53 /////////////////////////////////////////////////////////////////////////////// 54 // policy for real_generator, which forces to output trailing zeros in the 55 // fractional part 56 template <typename T> 57 struct trailing_zeros_policy : boost::spirit::karma::real_policies<T> // 4 digits 58 { 59 // we want the numbers always to contain trailing zeros up to 4 digits in 60 // the fractional part trailing_zerostrailing_zeros_policy61 static bool trailing_zeros(T) { return true; } 62 63 // we want to generate up to 4 fractional digits precisiontrailing_zeros_policy64 static unsigned int precision(T) { return 4; } 65 }; 66 67 /////////////////////////////////////////////////////////////////////////////// 68 // policy for real_generator, which forces the sign to be generated 69 template <typename T> 70 struct signed_policy : boost::spirit::karma::real_policies<T> 71 { 72 // we want to always have a sign generated force_signsigned_policy73 static bool force_sign(T) 74 { 75 return true; 76 } 77 }; 78 79 /////////////////////////////////////////////////////////////////////////////// 80 // policy for real_generator, which forces to output trailing zeros in the 81 // fractional part 82 template <typename T> 83 struct bordercase_policy : boost::spirit::karma::real_policies<T> 84 { 85 // we want to generate up to the maximum significant amount of fractional 86 // digits precisionbordercase_policy87 static unsigned int precision(T) 88 { 89 return std::numeric_limits<T>::digits10 + 1; 90 } 91 }; 92 93 /////////////////////////////////////////////////////////////////////////////// 94 // policy for real_generator, which forces to output trailing zeros in the 95 // fractional part 96 template <typename T> 97 struct statefull_policy : boost::spirit::karma::real_policies<T> 98 { statefull_policystatefull_policy99 statefull_policy(int precision = 4, bool trailingzeros = false) 100 : precision_(precision), trailingzeros_(trailingzeros) 101 {} 102 103 // we want to generate up to the maximum significant amount of fractional 104 // digits precisionstatefull_policy105 unsigned int precision(T) const 106 { 107 return precision_; 108 } 109 trailing_zerosstatefull_policy110 bool trailing_zeros(T) const 111 { 112 return trailingzeros_; 113 } 114 115 int precision_; 116 bool trailingzeros_; 117 }; 118 119 #endif // BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP 120 121