|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r55594 - in trunk/boost/spirit/home/karma/numeric: . detail
From: hartmut.kaiser_at_[hidden]
Date: 2009-08-14 18:27:53
Author: hkaiser
Date: 2009-08-14 18:27:52 EDT (Fri, 14 Aug 2009)
New Revision: 55594
URL: http://svn.boost.org/trac/boost/changeset/55594
Log:
Spirit: factored out floating point conversion into a separate header
Added:
trunk/boost/spirit/home/karma/numeric/detail/real_utils.hpp (contents, props changed)
Text files modified:
trunk/boost/spirit/home/karma/numeric/detail/numeric_utils.hpp | 221 ---------------------------------------
trunk/boost/spirit/home/karma/numeric/real.hpp | 2
2 files changed, 4 insertions(+), 219 deletions(-)
Modified: trunk/boost/spirit/home/karma/numeric/detail/numeric_utils.hpp
==============================================================================
--- trunk/boost/spirit/home/karma/numeric/detail/numeric_utils.hpp (original)
+++ trunk/boost/spirit/home/karma/numeric/detail/numeric_utils.hpp 2009-08-14 18:27:52 EDT (Fri, 14 Aug 2009)
@@ -10,6 +10,7 @@
#pragma once
#endif
+#include <boost/config.hpp>
#include <boost/config/no_tr1/cmath.hpp>
#include <limits>
@@ -39,8 +40,8 @@
#error "Please set the BOOST_KARMA_NUMERICS_LOOP_UNROLL to a non-negative value!"
#endif
-namespace boost { namespace spirit { namespace karma {
-
+namespace boost { namespace spirit { namespace karma
+{
namespace detail
{
///////////////////////////////////////////////////////////////////////
@@ -590,222 +591,6 @@
}
};
- ///////////////////////////////////////////////////////////////////////////
- // These are helper functions for the real policies allowing to generate
- // a single character and a string
- ///////////////////////////////////////////////////////////////////////////
- template <typename CharEncoding = unused_type, typename Tag = unused_type>
- struct char_inserter
- {
- template <typename OutputIterator, typename Char>
- static bool call(OutputIterator& sink, Char c)
- {
- return detail::generate_to(sink, c, CharEncoding(), Tag());
- }
- };
-
- template <typename CharEncoding = unused_type, typename Tag = unused_type>
- struct string_inserter
- {
- template <typename OutputIterator, typename String>
- static bool call(OutputIterator& sink, String str)
- {
- return detail::string_generate(sink, str, CharEncoding(), Tag());
- }
- };
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // The real_inserter template takes care of the floating point number to
- // string conversion. The Policies template parameter is used to allow
- // customization of the formatting process
- //
- ///////////////////////////////////////////////////////////////////////////
- template <typename T>
- struct real_policies;
-
- template <typename T
- , typename Policies = real_policies<T>
- , typename CharEncoding = unused_type
- , typename Tag = unused_type>
- struct real_inserter
- {
- template <typename OutputIterator>
- static bool
- call (OutputIterator& sink, float n, Policies const& p = Policies())
- {
- int fpclass = (math::fpclassify)(n);
- if ((int)FP_NAN == fpclass) {
- return Policies::template nan<CharEncoding, Tag>(
- sink, n, p.force_sign(n));
- }
- else if ((int)FP_INFINITE == fpclass) {
- return Policies::template inf<CharEncoding, Tag>(
- sink, n, p.force_sign(n));
- }
- return p.template call<real_inserter>(sink, n, p);
- }
-
- template <typename OutputIterator>
- static bool
- call (OutputIterator& sink, double n, Policies const& p = Policies())
- {
- int fpclass = (math::fpclassify)(n);
- if ((int)FP_NAN == fpclass) {
- return Policies::template nan<CharEncoding, Tag>(
- sink, n, p.force_sign(n));
- }
- else if ((int)FP_INFINITE == fpclass) {
- return Policies::template inf<CharEncoding, Tag>(
- sink, n, p.force_sign(n));
- }
- return p.template call<real_inserter>(sink, n, p);
- }
-
- template <typename OutputIterator>
- static bool
- call (OutputIterator& sink, long double n, Policies const& p = Policies())
- {
- int fpclass = (math::fpclassify)(n);
- if ((int)FP_NAN == fpclass) {
- return Policies::template nan<CharEncoding, Tag>(
- sink, n, p.force_sign(n));
- }
- else if ((int)FP_INFINITE == fpclass) {
- return Policies::template inf<CharEncoding, Tag>(
- sink, n, p.force_sign(n));
- }
- return p.template call<real_inserter>(sink, n, p);
- }
-
- template <typename OutputIterator, typename U>
- static bool
- call (OutputIterator& sink, U n, Policies const& p = Policies())
- {
- // we have no means of testing whether the number is normalized if
- // the type is not float, double or long double
- return p.template call<real_inserter>(sink, T(n), p);
- }
-
-#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
-# pragma warning(push)
-# pragma warning(disable: 4100) // 'p': unreferenced formal parameter
-# pragma warning(disable: 4127) // conditional expression is constant
-# pragma warning(disable: 4267) // conversion from 'size_t' to 'unsigned int', possible loss of data
-#endif
- ///////////////////////////////////////////////////////////////////////
- // This is the workhorse behind the real generator
- ///////////////////////////////////////////////////////////////////////
- template <typename OutputIterator, typename U>
- static bool
- call_n (OutputIterator& sink, U n, Policies const& p)
- {
- // prepare sign and get output format
- bool force_sign = p.force_sign(n);
- bool sign_val = false;
- int flags = p.floatfield(n);
- if (detail::is_negative(n))
- {
- n = -n;
- sign_val = true;
- }
-
- // The scientific representation requires the normalization of the
- // value to convert.
-
- // get correct precision for generated number
- unsigned precision = p.precision(n);
- if (std::numeric_limits<U>::digits10)
- {
- // limit generated precision to digits10, if defined
- precision = (std::min)(precision,
- (unsigned)std::numeric_limits<U>::digits10 + 1);
- }
-
- // allow for ADL to find the correct overloads for log10 et.al.
- using namespace std;
-
- U dim = 0;
- if (0 == (Policies::fmtflags::fixed & flags) && !detail::is_zero(n))
- {
- dim = log10(n);
- if (dim > 0)
- n /= spirit::detail::pow10<U>(detail::truncate_to_long::call(dim));
- else if (n < 1.)
- n *= spirit::detail::pow10<U>(detail::truncate_to_long::call(-dim) + 1);
- }
-
- // prepare numbers (sign, integer and fraction part)
- U integer_part;
- U precexp = spirit::detail::pow10<U>(precision);
- U fractional_part = modf(n, &integer_part);
-
- fractional_part = floor(fractional_part * precexp + U(0.5));
- if (fractional_part >= precexp)
- {
- fractional_part = floor(fractional_part - precexp);
- integer_part += 1; // handle rounding overflow
- }
-
- // if trailing zeros are to be omitted, normalize the precision and
- // fractional part
- U long_int_part = floor(integer_part);
- U long_frac_part = fractional_part;
- unsigned prec = precision;
- if (!p.trailing_zeros(n))
- {
- U frac_part_floor = long_frac_part;
- if (0 != long_frac_part) {
- // remove the trailing zeros
- while (0 != prec &&
- 0 == detail::remainder<10>::call(long_frac_part))
- {
- long_frac_part = detail::divide<10>::call(long_frac_part);
- --prec;
- }
- }
- else {
- // if the fractional part is zero, we don't need to output
- // any additional digits
- prec = 0;
- }
-
- if (precision != prec)
- {
- long_frac_part = frac_part_floor /
- spirit::detail::pow10<U>(precision-prec);
- }
- }
-
- // call the actual generating functions to output the different parts
- if (sign_val && detail::is_zero(long_int_part) &&
- detail::is_zero(long_frac_part))
- {
- sign_val = false; // result is zero, no sign please
- }
-
- // generate integer part
- bool r = p.integer_part(sink, long_int_part, sign_val, force_sign);
-
- // generate decimal point
- r = r && p.dot(sink, long_frac_part, precision);
-
- // generate fractional part with the desired precision
- r = r && p.fraction_part(sink, long_frac_part, prec, precision);
-
- if (r && 0 == (Policies::fmtflags::fixed & flags)) {
- return p.template exponent<CharEncoding, Tag>(sink,
- detail::truncate_to_long::call(dim >= 0 ? dim : dim - 1));
- }
- return r;
- }
-
-#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
-# pragma warning(pop)
-#endif
-
- };
-
}}}
#endif
Added: trunk/boost/spirit/home/karma/numeric/detail/real_utils.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/spirit/home/karma/numeric/detail/real_utils.hpp 2009-08-14 18:27:52 EDT (Fri, 14 Aug 2009)
@@ -0,0 +1,247 @@
+// Copyright (c) 2001-2009 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(BOOST_SPIRIT_KARMA_REAL_UTILS_FEB_23_2007_0841PM)
+#define BOOST_SPIRIT_KARMA_REAL_UTILS_FEB_23_2007_0841PM
+
+#if defined(_MSC_VER)
+#pragma once
+#endif
+
+#include <boost/config.hpp>
+#include <boost/config/no_tr1/cmath.hpp>
+#include <boost/detail/workaround.hpp>
+#include <limits>
+
+#include <boost/spirit/home/support/char_class.hpp>
+#include <boost/spirit/home/support/unused.hpp>
+#include <boost/spirit/home/support/detail/pow10.hpp>
+#include <boost/spirit/home/support/detail/sign.hpp>
+#include <boost/spirit/home/karma/detail/generate_to.hpp>
+#include <boost/spirit/home/karma/detail/string_generate.hpp>
+#include <boost/spirit/home/karma/numeric/detail/numeric_utils.hpp>
+
+namespace boost { namespace spirit { namespace karma
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // These are helper functions for the real policies allowing to generate
+ // a single character and a string
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename CharEncoding = unused_type, typename Tag = unused_type>
+ struct char_inserter
+ {
+ template <typename OutputIterator, typename Char>
+ static bool call(OutputIterator& sink, Char c)
+ {
+ return detail::generate_to(sink, c, CharEncoding(), Tag());
+ }
+ };
+
+ template <typename CharEncoding = unused_type, typename Tag = unused_type>
+ struct string_inserter
+ {
+ template <typename OutputIterator, typename String>
+ static bool call(OutputIterator& sink, String str)
+ {
+ return detail::string_generate(sink, str, CharEncoding(), Tag());
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ //
+ // The real_inserter template takes care of the floating point number to
+ // string conversion. The Policies template parameter is used to allow
+ // customization of the formatting process
+ //
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename T>
+ struct real_policies;
+
+ template <typename T
+ , typename Policies = real_policies<T>
+ , typename CharEncoding = unused_type
+ , typename Tag = unused_type>
+ struct real_inserter
+ {
+ template <typename OutputIterator>
+ static bool
+ call (OutputIterator& sink, float n, Policies const& p = Policies())
+ {
+ int fpclass = (math::fpclassify)(n);
+ if ((int)FP_NAN == fpclass) {
+ return Policies::template nan<CharEncoding, Tag>(
+ sink, n, p.force_sign(n));
+ }
+ else if ((int)FP_INFINITE == fpclass) {
+ return Policies::template inf<CharEncoding, Tag>(
+ sink, n, p.force_sign(n));
+ }
+ return p.template call<real_inserter>(sink, n, p);
+ }
+
+ template <typename OutputIterator>
+ static bool
+ call (OutputIterator& sink, double n, Policies const& p = Policies())
+ {
+ int fpclass = (math::fpclassify)(n);
+ if ((int)FP_NAN == fpclass) {
+ return Policies::template nan<CharEncoding, Tag>(
+ sink, n, p.force_sign(n));
+ }
+ else if ((int)FP_INFINITE == fpclass) {
+ return Policies::template inf<CharEncoding, Tag>(
+ sink, n, p.force_sign(n));
+ }
+ return p.template call<real_inserter>(sink, n, p);
+ }
+
+ template <typename OutputIterator>
+ static bool
+ call (OutputIterator& sink, long double n, Policies const& p = Policies())
+ {
+ int fpclass = (math::fpclassify)(n);
+ if ((int)FP_NAN == fpclass) {
+ return Policies::template nan<CharEncoding, Tag>(
+ sink, n, p.force_sign(n));
+ }
+ else if ((int)FP_INFINITE == fpclass) {
+ return Policies::template inf<CharEncoding, Tag>(
+ sink, n, p.force_sign(n));
+ }
+ return p.template call<real_inserter>(sink, n, p);
+ }
+
+ template <typename OutputIterator, typename U>
+ static bool
+ call (OutputIterator& sink, U n, Policies const& p = Policies())
+ {
+ // we have no means of testing whether the number is normalized if
+ // the type is not float, double or long double
+ return p.template call<real_inserter>(sink, T(n), p);
+ }
+
+#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
+# pragma warning(push)
+# pragma warning(disable: 4100) // 'p': unreferenced formal parameter
+# pragma warning(disable: 4127) // conditional expression is constant
+# pragma warning(disable: 4267) // conversion from 'size_t' to 'unsigned int', possible loss of data
+#endif
+ ///////////////////////////////////////////////////////////////////////
+ // This is the workhorse behind the real generator
+ ///////////////////////////////////////////////////////////////////////
+ template <typename OutputIterator, typename U>
+ static bool
+ call_n (OutputIterator& sink, U n, Policies const& p)
+ {
+ // prepare sign and get output format
+ bool force_sign = p.force_sign(n);
+ bool sign_val = false;
+ int flags = p.floatfield(n);
+ if (detail::is_negative(n))
+ {
+ n = -n;
+ sign_val = true;
+ }
+
+ // The scientific representation requires the normalization of the
+ // value to convert.
+
+ // get correct precision for generated number
+ unsigned precision = p.precision(n);
+ if (std::numeric_limits<U>::digits10)
+ {
+ // limit generated precision to digits10, if defined
+ precision = (std::min)(precision,
+ (unsigned)std::numeric_limits<U>::digits10 + 1);
+ }
+
+ // allow for ADL to find the correct overloads for log10 et.al.
+ using namespace std;
+
+ U dim = 0;
+ if (0 == (Policies::fmtflags::fixed & flags) && !detail::is_zero(n))
+ {
+ dim = log10(n);
+ if (dim > 0)
+ n /= spirit::detail::pow10<U>(detail::truncate_to_long::call(dim));
+ else if (n < 1.)
+ n *= spirit::detail::pow10<U>(detail::truncate_to_long::call(-dim) + 1);
+ }
+
+ // prepare numbers (sign, integer and fraction part)
+ U integer_part;
+ U precexp = spirit::detail::pow10<U>(precision);
+ U fractional_part = modf(n, &integer_part);
+
+ fractional_part = floor(fractional_part * precexp + U(0.5));
+ if (fractional_part >= precexp)
+ {
+ fractional_part = floor(fractional_part - precexp);
+ integer_part += 1; // handle rounding overflow
+ }
+
+ // if trailing zeros are to be omitted, normalize the precision and
+ // fractional part
+ U long_int_part = floor(integer_part);
+ U long_frac_part = fractional_part;
+ unsigned prec = precision;
+ if (!p.trailing_zeros(n))
+ {
+ U frac_part_floor = long_frac_part;
+ if (0 != long_frac_part) {
+ // remove the trailing zeros
+ while (0 != prec &&
+ 0 == detail::remainder<10>::call(long_frac_part))
+ {
+ long_frac_part = detail::divide<10>::call(long_frac_part);
+ --prec;
+ }
+ }
+ else {
+ // if the fractional part is zero, we don't need to output
+ // any additional digits
+ prec = 0;
+ }
+
+ if (precision != prec)
+ {
+ long_frac_part = frac_part_floor /
+ spirit::detail::pow10<U>(precision-prec);
+ }
+ }
+
+ // call the actual generating functions to output the different parts
+ if (sign_val && detail::is_zero(long_int_part) &&
+ detail::is_zero(long_frac_part))
+ {
+ sign_val = false; // result is zero, no sign please
+ }
+
+ // generate integer part
+ bool r = p.integer_part(sink, long_int_part, sign_val, force_sign);
+
+ // generate decimal point
+ r = r && p.dot(sink, long_frac_part, precision);
+
+ // generate fractional part with the desired precision
+ r = r && p.fraction_part(sink, long_frac_part, prec, precision);
+
+ if (r && 0 == (Policies::fmtflags::fixed & flags)) {
+ return p.template exponent<CharEncoding, Tag>(sink,
+ detail::truncate_to_long::call(dim >= 0 ? dim : dim - 1));
+ }
+ return r;
+ }
+
+#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
+# pragma warning(pop)
+#endif
+
+ };
+
+}}}
+
+#endif
+
Modified: trunk/boost/spirit/home/karma/numeric/real.hpp
==============================================================================
--- trunk/boost/spirit/home/karma/numeric/real.hpp (original)
+++ trunk/boost/spirit/home/karma/numeric/real.hpp 2009-08-14 18:27:52 EDT (Fri, 14 Aug 2009)
@@ -23,7 +23,7 @@
#include <boost/spirit/home/karma/detail/get_casetag.hpp>
#include <boost/spirit/home/karma/domain.hpp>
#include <boost/spirit/home/karma/numeric/real_policies.hpp>
-#include <boost/spirit/home/karma/numeric/detail/numeric_utils.hpp>
+#include <boost/spirit/home/karma/numeric/detail/real_utils.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/value_at.hpp>
#include <boost/fusion/include/vector.hpp>
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk