Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:49

0001 //  boost/chrono/round.hpp  ------------------------------------------------------------//
0002 
0003 //  (C) Copyright Howard Hinnant
0004 //  Copyright 2011 Vicente J. Botet Escriba
0005 
0006 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
0007 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 //  See http://www.boost.org/libs/chrono for documentation.
0010 
0011 #ifndef BOOST_CHRONO_ROUND_HPP
0012 #define BOOST_CHRONO_ROUND_HPP
0013 
0014 #include <boost/chrono/duration.hpp>
0015 #include <boost/chrono/duration.hpp>
0016 //#include <boost/chrono/typeof/boost/chrono/chrono.hpp>
0017 
0018 namespace boost
0019 {
0020   namespace chrono
0021   {
0022 
0023     /**
0024      * rounds to nearest, to even on tie
0025      */
0026     template <class To, class Rep, class Period>
0027     To round(const duration<Rep, Period>& d)
0028     {
0029         typedef typename common_type<To, duration<Rep, Period> >::type  result_type;
0030         result_type diff0;
0031         result_type diff1;
0032 
0033         To t0 = duration_cast<To>(d);
0034         To t1 = t0;
0035         if (t0>d) {
0036           --t1;
0037           diff0 = t0 - d;
0038           diff1 = d - t1;
0039         } else {
0040           ++t1;
0041           diff0 = d - t0;
0042           diff1 = t1 - d;
0043         }
0044 
0045         if (diff0 == diff1)
0046         {
0047             if (t0.count() & 1)
0048                 return t1;
0049             return t0;
0050         }
0051         else if (diff0 < diff1)
0052             return t0;
0053         return t1;
0054     }
0055 
0056   } // namespace chrono
0057 } // namespace boost
0058 
0059 #endif