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 22 #include <boost/limits.hpp> 23 #include "test.hpp" 24 25 using namespace spirit_test; 26 27 /////////////////////////////////////////////////////////////////////////////// 28 // policy for real_generator, which forces the scientific notation 29 template <typename T> 30 struct scientific_policy : boost::spirit::karma::real_policies<T> 31 { 32 // we want the numbers always to be in scientific format 33 typedef boost::spirit::karma::real_policies<T> base_type; floatfieldscientific_policy34 static int floatfield(T) { return base_type::fmtflags::scientific; } 35 }; 36 37 /////////////////////////////////////////////////////////////////////////////// 38 // policy for real_generator, which forces the fixed notation 39 template <typename T> 40 struct fixed_policy : boost::spirit::karma::real_policies<T> 41 { 42 typedef boost::spirit::karma::real_policies<T> base_type; 43 44 // we want the numbers always to be in fixed format floatfieldfixed_policy45 static int floatfield(T) { return base_type::fmtflags::fixed; } 46 }; 47 48 /////////////////////////////////////////////////////////////////////////////// 49 // policy for real_generator, which forces to output trailing zeros in the 50 // fractional part 51 template <typename T> 52 struct trailing_zeros_policy : boost::spirit::karma::real_policies<T> // 4 digits 53 { 54 // we want the numbers always to contain trailing zeros up to 4 digits in 55 // the fractional part trailing_zerostrailing_zeros_policy56 static bool trailing_zeros(T) { return true; } 57 58 // we want to generate up to 4 fractional digits precisiontrailing_zeros_policy59 static unsigned int precision(T) { return 4; } 60 }; 61 62 /////////////////////////////////////////////////////////////////////////////// 63 // policy for real_generator, which forces the sign to be generated 64 template <typename T> 65 struct signed_policy : boost::spirit::karma::real_policies<T> 66 { 67 // we want to always have a sign generated force_signsigned_policy68 static bool force_sign(T) 69 { 70 return true; 71 } 72 }; 73 74 /////////////////////////////////////////////////////////////////////////////// 75 // policy for real_generator, which forces to output trailing zeros in the 76 // fractional part 77 template <typename T> 78 struct bordercase_policy : boost::spirit::karma::real_policies<T> 79 { 80 // we want to generate up to the maximum significant amount of fractional 81 // digits precisionbordercase_policy82 static unsigned int precision(T) 83 { 84 return std::numeric_limits<T>::digits10 + 1; 85 } 86 }; 87 88 /////////////////////////////////////////////////////////////////////////////// 89 // policy for real_generator, which forces to output trailing zeros in the 90 // fractional part 91 template <typename T> 92 struct statefull_policy : boost::spirit::karma::real_policies<T> 93 { statefull_policystatefull_policy94 statefull_policy(int precision = 4, bool trailingzeros = false) 95 : precision_(precision), trailingzeros_(trailingzeros) 96 {} 97 98 // we want to generate up to the maximum significant amount of fractional 99 // digits precisionstatefull_policy100 unsigned int precision(T) const 101 { 102 return precision_; 103 } 104 trailing_zerosstatefull_policy105 bool trailing_zeros(T) const 106 { 107 return trailingzeros_; 108 } 109 110 int precision_; 111 bool trailingzeros_; 112 }; 113 114 #endif // BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP 115 116