Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73024 - in sandbox/coerce: boost/coerce/detail libs/coerce/test
From: vexocide_at_[hidden]
Date: 2011-07-12 17:58:37


Author: vexocide
Date: 2011-07-12 17:58:37 EDT (Tue, 12 Jul 2011)
New Revision: 73024
URL: http://svn.boost.org/trac/boost/changeset/73024

Log:
Floating point precision is now calculated in a similar fashion to boost.lexical_cast
Added:
   sandbox/coerce/boost/coerce/detail/precision.hpp (contents, props changed)
Text files modified:
   sandbox/coerce/boost/coerce/detail/karma.hpp | 3 ++-
   sandbox/coerce/libs/coerce/test/floating_point.cpp | 26 +++++++++++++-------------
   2 files changed, 15 insertions(+), 14 deletions(-)

Modified: sandbox/coerce/boost/coerce/detail/karma.hpp
==============================================================================
--- sandbox/coerce/boost/coerce/detail/karma.hpp (original)
+++ sandbox/coerce/boost/coerce/detail/karma.hpp 2011-07-12 17:58:37 EDT (Tue, 12 Jul 2011)
@@ -11,6 +11,7 @@
 #pragma once
 #endif
 
+#include <boost/coerce/detail/precision.hpp>
 #include <boost/coerce/reserve.hpp>
 #include <boost/coerce/sequence.hpp>
 #include <boost/coerce/tag.hpp>
@@ -45,7 +46,7 @@
         : spirit::karma::real_policies<Source> {
         static inline unsigned
         precision(Source const &) {
- return std::numeric_limits<Source>::digits10 + 1;
+ return detail::precision<Source>::value;
         }
     };
 

Added: sandbox/coerce/boost/coerce/detail/precision.hpp
==============================================================================
--- (empty file)
+++ sandbox/coerce/boost/coerce/detail/precision.hpp 2011-07-12 17:58:37 EDT (Tue, 12 Jul 2011)
@@ -0,0 +1,45 @@
+// Copyright Jeroen Habraken 2011.
+//
+// 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)
+
+#ifndef BOOST_COERCE_DETAIL_PRECISION_HPP
+#define BOOST_COERCE_DETAIL_PRECISION_HPP
+
+#ifdef _MSC_VER
+#pragma once
+#endif
+
+#include <boost/config.hpp>
+#include <boost/limits.hpp>
+
+namespace boost { namespace coerce { namespace detail {
+
+ template <typename T>
+ struct precision {
+ typedef std::numeric_limits<T> limits;
+
+ BOOST_STATIC_CONSTANT(bool, is_specialized =
+ limits::is_specialized && !limits::is_exact);
+
+ BOOST_STATIC_CONSTANT(bool, is_specialized_binary =
+ is_specialized && limits::radix == 2 && limits::digits > 0);
+
+ BOOST_STATIC_CONSTANT(unsigned, precision_binary =
+ 2UL + limits::digits * 30103UL / 100000UL);
+
+ BOOST_STATIC_CONSTANT(bool, is_specialized_decimal =
+ is_specialized && limits::radix == 10 && limits::digits10 > 0);
+
+ BOOST_STATIC_CONSTANT(unsigned, precision_decimal =
+ limits::digits10 + 1U);
+
+ BOOST_STATIC_CONSTANT(unsigned, value =
+ is_specialized_binary ? precision_binary
+ : is_specialized_decimal ? precision_decimal : 6);
+ };
+
+} } } // namespace boost::coerce::detail
+
+#endif // BOOST_COERCE_DETAIL_PRECISION_HPP

Modified: sandbox/coerce/libs/coerce/test/floating_point.cpp
==============================================================================
--- sandbox/coerce/libs/coerce/test/floating_point.cpp (original)
+++ sandbox/coerce/libs/coerce/test/floating_point.cpp 2011-07-12 17:58:37 EDT (Tue, 12 Jul 2011)
@@ -26,12 +26,12 @@
             coerce::as<std::string>(static_cast<T>(-23)), "-23.0");
 
         BOOST_CHECK_EQUAL(
- coerce::as<std::string>(static_cast<T>(1.01828183L)), "1.01828183");
+ coerce::as<std::string>(static_cast<T>(1.0182818L)), "1.0182818");
 
         BOOST_CHECK_EQUAL(
- coerce::as<std::string>(static_cast<T>(1.0e23L)), "1.0e23");
+ coerce::as<std::string>(static_cast<T>(1.0e18L)), "1.0e18");
         BOOST_CHECK_EQUAL(
- coerce::as<std::string>(static_cast<T>(1.0e-23L)), "1.0e-23");
+ coerce::as<std::string>(static_cast<T>(1.0e-18L)), "1.0e-18");
 
         BOOST_CHECK_EQUAL(
             coerce::as<std::string>(std::numeric_limits<T>::quiet_NaN()),
@@ -75,25 +75,25 @@
         BOOST_CHECK_EQUAL(coerce::as<T>("-23.0"), -23);
 
         BOOST_CHECK_CLOSE_FRACTION(
- coerce::as<T>("1.01828183"),
- static_cast<T>(1.01828183L),
+ coerce::as<T>("1.0182818"),
+ static_cast<T>(1.0182818L),
             std::numeric_limits<T>::epsilon());
 
         BOOST_CHECK_CLOSE_FRACTION(
- coerce::as<T>("1.0e23"),
- static_cast<T>(1.0e23L),
+ coerce::as<T>("1.0e18"),
+ static_cast<T>(1.0e18L),
             std::numeric_limits<T>::epsilon());
         BOOST_CHECK_CLOSE_FRACTION(
- coerce::as<T>("1.0E23"),
- static_cast<T>(1.0e23L),
+ coerce::as<T>("1.0E18"),
+ static_cast<T>(1.0e18L),
             std::numeric_limits<T>::epsilon());
         BOOST_CHECK_CLOSE_FRACTION(
- coerce::as<T>("1.0e+23"),
- static_cast<T>(1.0e23L),
+ coerce::as<T>("1.0e+18"),
+ static_cast<T>(1.0e18L),
             std::numeric_limits<T>::epsilon());
         BOOST_CHECK_CLOSE_FRACTION(
- coerce::as<T>("1.0e-23"),
- static_cast<T>(1.0e-23L),
+ coerce::as<T>("1.0e-18"),
+ static_cast<T>(1.0e-18L),
             std::numeric_limits<T>::epsilon());
 
         BOOST_CHECK_EQUAL(math::fpclassify(coerce::as<T>("NaN")), FP_NAN);


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