From 7702c85592ebd47e398fa8a32e4e824da48d783e Mon Sep 17 00:00:00 2001 From: "Vicente J. Botet Escriba" Date: Sat, 9 Nov 2013 11:47:43 +0000 Subject: [PATCH] Ratio: Added ratio binary prefixes and ratio_power. [SVN r86594] --- include/boost/ratio/detail/ratio_io.hpp | 302 +++++++++++++++++++++ include/boost/ratio/ratio.hpp | 18 ++ include/boost/ratio/ratio_fwd.hpp | 12 + include/boost/ratio/ratio_io.hpp | 257 ++++++++++++++++++ test/Jamfile.v2 | 1 + test/ratio_arithmetic/ratio_power_pass.cpp | 60 ++++ test/ratio_io/ratio_io_pass.cpp | 18 +- 7 files changed, 667 insertions(+), 1 deletion(-) create mode 100644 test/ratio_arithmetic/ratio_power_pass.cpp diff --git a/include/boost/ratio/detail/ratio_io.hpp b/include/boost/ratio/detail/ratio_io.hpp index 2b17f50..0bc4fd8 100644 --- a/include/boost/ratio/detail/ratio_io.hpp +++ b/include/boost/ratio/detail/ratio_io.hpp @@ -1040,6 +1040,308 @@ struct ratio_string #endif #endif +#ifdef BOOST_RATIO_EXTENSIONS + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string short_name() {return std::string("Ki");} + static std::string long_name() {return std::string("kibi");} + static std::string symbol() {return short_name();} + static std::string prefix() {return long_name();} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string short_name() {return std::u16string(u"Ki");} + static std::u16string long_name() {return std::u16string(u"kibi");} + static std::u16string symbol() {return short_name();} + static std::u16string prefix() {return long_name();} +}; + +template <> +struct ratio_string +{ + static std::u32string short_name() {return std::u32string(U"Ki");} + static std::u32string long_name() {return std::u32string(U"kibi");} + static std::u32string symbol() {return short_name();} + static std::u32string prefix() {return long_name();} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring short_name() {return std::wstring(L"Ki");} + static std::wstring long_name() {return std::wstring(L"kibi");} + static std::wstring symbol() {return short_name();} + static std::wstring prefix() {return long_name();} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string short_name() {return std::string("Mi");} + static std::string long_name() {return std::string("mebi");} + static std::string symbol() {return short_name();} + static std::string prefix() {return long_name();} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string short_name() {return std::u16string(u"Mi");} + static std::u16string long_name() {return std::u16string(u"mebi");} + static std::u16string symbol() {return short_name();} + static std::u16string prefix() {return long_name();} +}; + +template <> +struct ratio_string +{ + static std::u32string short_name() {return std::u32string(U"Mi");} + static std::u32string long_name() {return std::u32string(U"mebi");} + static std::u32string symbol() {return short_name();} + static std::u32string prefix() {return long_name();} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring short_name() {return std::wstring(L"Mi");} + static std::wstring long_name() {return std::wstring(L"mebi");} + static std::wstring symbol() {return short_name();} + static std::wstring prefix() {return long_name();} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string short_name() {return std::string("Gi");} + static std::string long_name() {return std::string("gibi");} + static std::string symbol() {return short_name();} + static std::string prefix() {return long_name();} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string short_name() {return std::u16string(u"Gi");} + static std::u16string long_name() {return std::u16string(u"gibi");} + static std::u16string symbol() {return short_name();} + static std::u16string prefix() {return long_name();} +}; + +template <> +struct ratio_string +{ + static std::u32string short_name() {return std::u32string(U"Gi");} + static std::u32string long_name() {return std::u32string(U"gibi");} + static std::u32string symbol() {return short_name();} + static std::u32string prefix() {return long_name();} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring short_name() {return std::wstring(L"Gi");} + static std::wstring long_name() {return std::wstring(L"gibi");} + static std::wstring symbol() {return short_name();} + static std::wstring prefix() {return long_name();} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string short_name() {return std::string("Ti");} + static std::string long_name() {return std::string("tebi");} + static std::string symbol() {return short_name();} + static std::string prefix() {return long_name();} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string short_name() {return std::u16string(u"Ti");} + static std::u16string long_name() {return std::u16string(u"tebi");} + static std::u16string symbol() {return short_name();} + static std::u16string prefix() {return long_name();} +}; + +template <> +struct ratio_string +{ + static std::u32string short_name() {return std::u32string(U"Ti");} + static std::u32string long_name() {return std::u32string(U"tebi");} + static std::u32string symbol() {return short_name();} + static std::u32string prefix() {return long_name();} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring short_name() {return std::wstring(L"Ti");} + static std::wstring long_name() {return std::wstring(L"tebi");} + static std::wstring symbol() {return short_name();} + static std::wstring prefix() {return long_name();} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string short_name() {return std::string("Pi");} + static std::string long_name() {return std::string("pebi");} + static std::string symbol() {return short_name();} + static std::string prefix() {return long_name();} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string short_name() {return std::u16string(u"Pi");} + static std::u16string long_name() {return std::u16string(u"pebi");} + static std::u16string symbol() {return short_name();} + static std::u16string prefix() {return long_name();} +}; + +template <> +struct ratio_string +{ + static std::u32string short_name() {return std::u32string(U"Pi");} + static std::u32string long_name() {return std::u32string(U"pebi");} + static std::u32string symbol() {return short_name();} + static std::u32string prefix() {return long_name();} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring short_name() {return std::wstring(L"Pi");} + static std::wstring long_name() {return std::wstring(L"pebi");} + static std::wstring symbol() {return short_name();} + static std::wstring prefix() {return long_name();} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string short_name() {return std::string("Ei");} + static std::string long_name() {return std::string("exbi");} + static std::string symbol() {return short_name();} + static std::string prefix() {return long_name();} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string short_name() {return std::u16string(u"Ei");} + static std::u16string long_name() {return std::u16string(u"exbi");} + static std::u16string symbol() {return short_name();} + static std::u16string prefix() {return long_name();} +}; + +template <> +struct ratio_string +{ + static std::u32string short_name() {return std::u32string(U"Ei");} + static std::u32string long_name() {return std::u32string(U"exbi");} + static std::u32string symbol() {return short_name();} + static std::u32string prefix() {return long_name();} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring short_name() {return std::wstring(L"Ei");} + static std::wstring long_name() {return std::wstring(L"exbi");} + static std::wstring symbol() {return short_name();} + static std::wstring prefix() {return long_name();} +}; +#endif +#endif +#endif } #endif // BOOST_RATIO_RATIO_IO_HPP diff --git a/include/boost/ratio/ratio.hpp b/include/boost/ratio/ratio.hpp index 546e8f0..f34193a 100644 --- a/include/boost/ratio/ratio.hpp +++ b/include/boost/ratio/ratio.hpp @@ -226,6 +226,24 @@ struct ratio_lcm : mpl::gcd_c::value>::type { }; + +template +struct ratio_power : + ratio_multiply< + typename ratio_power::type, + typename ratio_power::type, p/2>::type + >::type +{}; + +template +struct ratio_power : ratio<1>::type {}; + +template +struct ratio_power : R {}; + +template +struct ratio_power : ratio_divide, R>::type {}; + #endif } // namespace boost diff --git a/include/boost/ratio/ratio_fwd.hpp b/include/boost/ratio/ratio_fwd.hpp index 0882e0b..8d5eedc 100644 --- a/include/boost/ratio/ratio_fwd.hpp +++ b/include/boost/ratio/ratio_fwd.hpp @@ -78,6 +78,18 @@ typedef ratio< BOOST_RATIO_INTMAX_C(1000000000000), BOOST_RATIO_INTMAX_C(1) typedef ratio< BOOST_RATIO_INTMAX_C(1000000000000000), BOOST_RATIO_INTMAX_C(1)> peta; typedef ratio exa; +#ifdef BOOST_RATIO_EXTENSIONS + +#define BOOST_RATIO_1024 BOOST_RATIO_INTMAX_C(1024) + +typedef ratio< BOOST_RATIO_1024> kibi; +typedef ratio< BOOST_RATIO_1024*BOOST_RATIO_1024> mebi; +typedef ratio< BOOST_RATIO_1024*BOOST_RATIO_1024*BOOST_RATIO_1024> gibi; +typedef ratio< BOOST_RATIO_1024*BOOST_RATIO_1024*BOOST_RATIO_1024*BOOST_RATIO_1024> tebi; +typedef ratio< BOOST_RATIO_1024*BOOST_RATIO_1024*BOOST_RATIO_1024*BOOST_RATIO_1024*BOOST_RATIO_1024> pebi; +typedef ratio exbi; + +#endif } // namespace boost diff --git a/include/boost/ratio/ratio_io.hpp b/include/boost/ratio/ratio_io.hpp index b3c7272..208c782 100644 --- a/include/boost/ratio/ratio_io.hpp +++ b/include/boost/ratio/ratio_io.hpp @@ -45,6 +45,7 @@ struct ratio_string #include #ifdef BOOST_RATIO_HAS_STATIC_STRING +#error #include #include #endif @@ -818,6 +819,262 @@ struct ratio_string #endif #endif + +#ifdef BOOST_RATIO_EXTENSIONS + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string symbol() {return std::string("Ki");} + static std::string prefix() {return std::string("kibi");} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string symbol() {return std::u16string(u"Ki");} + static std::u16string prefix() {return std::u16string(u"kibi");} +}; + +template <> +struct ratio_string +{ + static std::u32string symbol() {return std::u32string(U"Ki");} + static std::u32string prefix() {return std::u32string(U"kibi");} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring symbol() {return std::wstring(L"Ki");} + static std::wstring prefix() {return std::wstring(L"kibi");} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string symbol() {return std::string("Mi");} + static std::string prefix() {return std::string("mebi");} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string symbol() {return std::u16string(u"Mi");} + static std::u16string prefix() {return std::u16string(u"mebi");} +}; + +template <> +struct ratio_string +{ + static std::u32string symbol() {return std::u32string(U"Mi");} + static std::u32string prefix() {return std::u32string(U"mebi");} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring symbol() {return std::wstring(L"Mi");} + static std::wstring prefix() {return std::wstring(L"mebi");} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string symbol() {return std::string("Gi");} + static std::string prefix() {return std::string("gibi");} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string symbol() {return std::u16string(u"Gi");} + static std::u16string prefix() {return std::u16string(u"gibi");} +}; + +template <> +struct ratio_string +{ + static std::u32string symbol() {return std::u32string(U"Gi");} + static std::u32string prefix() {return std::u32string(U"gibi");} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring symbol() {return std::wstring(L"Gi");} + static std::wstring prefix() {return std::wstring(L"gibi");} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string symbol() {return std::string("Ti");} + static std::string prefix() {return std::string("tebi");} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string symbol() {return std::u16string(u"Ti");} + static std::u16string prefix() {return std::u16string(u"tebi");} +}; + +template <> +struct ratio_string +{ + static std::u32string symbol() {return std::u32string(U"Ti");} + static std::u32string prefix() {return std::u32string(U"tebi");} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring symbol() {return std::wstring(L"Ti");} + static std::wstring prefix() {return std::wstring(L"tebi");} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string symbol() {return std::string("Pi");} + static std::string prefix() {return std::string("pebi");} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string symbol() {return std::u16string(u"Pi");} + static std::u16string prefix() {return std::u16string(u"pebi");} +}; + +template <> +struct ratio_string +{ + static std::u32string symbol() {return std::u32string(U"Pi");} + static std::u32string prefix() {return std::u32string(U"pebi");} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring symbol() {return std::wstring(L"Pi");} + static std::wstring prefix() {return std::wstring(L"pebi");} +}; +#endif +#endif + +#ifdef BOOST_RATIO_HAS_STATIC_STRING +template +struct ratio_string : + ratio_detail::ratio_string_static +{}; + +#else +template <> +struct ratio_string +{ + static std::string symbol() {return std::string("Ei");} + static std::string prefix() {return std::string("exbi");} +}; + +#if BOOST_RATIO_HAS_UNICODE_SUPPORT + +template <> +struct ratio_string +{ + static std::u16string symbol() {return std::u16string(u"Ei");} + static std::u16string prefix() {return std::u16string(u"exbi");} +}; + +template <> +struct ratio_string +{ + static std::u32string symbol() {return std::u32string(U"Ei");} + static std::u32string prefix() {return std::u32string(U"exbi");} +}; + +#endif + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct ratio_string +{ + static std::wstring symbol() {return std::wstring(L"Ei");} + static std::wstring prefix() {return std::wstring(L"exbi");} +}; +#endif +#endif +#endif + } #endif // BOOST_RATIO_PROVIDES_DEPRECATED_FEATURES_SINCE_V2_0_0 diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index dd47197..4361290 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -82,6 +82,7 @@ project [ compile-fail ratio_arithmetic/ratio_subtract_fail.cpp ] [ compile-fail ratio_arithmetic/ratio_multiply_fail.cpp ] [ compile-fail ratio_arithmetic/ratio_divide_fail.cpp ] + [ compile ratio_arithmetic/ratio_power_pass.cpp ] ; test-suite "ratio.comparison" diff --git a/test/ratio_arithmetic/ratio_power_pass.cpp b/test/ratio_arithmetic/ratio_power_pass.cpp new file mode 100644 index 0000000..8bfe41a --- /dev/null +++ b/test/ratio_arithmetic/ratio_power_pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// Adaptation to Boost of the libcxx +// Copyright 2010 Vicente J. Botet Escriba +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// test ratio_power + +#define BOOST_RATIO_EXTENSIONS +#include + +#if !defined(BOOST_NO_CXX11_STATIC_ASSERT) +#define NOTHING "" +#endif + +void test() +{ + { + typedef boost::ratio<1, 2> R1; + typedef boost::ratio_power R; + BOOST_RATIO_STATIC_ASSERT(R::num == 1 && R::den == 2, NOTHING, ()); + } + { + typedef boost::ratio<1, 2> R1; + typedef boost::ratio_power R; + BOOST_RATIO_STATIC_ASSERT(R::num == 2 && R::den == 1, NOTHING, ()); + } + { + typedef boost::ratio<1, 2> R1; + typedef boost::ratio_power R; + BOOST_RATIO_STATIC_ASSERT(R::num == 1 && R::den == 1, NOTHING, ()); + } + { + typedef boost::ratio<-1, 2> R1; + typedef boost::ratio_power R; + BOOST_RATIO_STATIC_ASSERT(R::num == 1 && R::den == 4, NOTHING, ()); + } + { + typedef boost::ratio<1, -2> R1; + typedef boost::ratio_power R; + BOOST_RATIO_STATIC_ASSERT(R::num == 1 && R::den == 4, NOTHING, ()); + } + { + typedef boost::ratio<2, 3> R1; + typedef boost::ratio_power R; + BOOST_RATIO_STATIC_ASSERT(R::num == 4 && R::den == 9, NOTHING, ()); + } + { + typedef boost::ratio<2, 3> R1; + typedef boost::ratio_power R; + BOOST_RATIO_STATIC_ASSERT(R::num == 9 && R::den == 4, NOTHING, ()); + } +} diff --git a/test/ratio_io/ratio_io_pass.cpp b/test/ratio_io/ratio_io_pass.cpp index 0c31c7d..bcff57c 100644 --- a/test/ratio_io/ratio_io_pass.cpp +++ b/test/ratio_io/ratio_io_pass.cpp @@ -13,6 +13,7 @@ // test ratio_add +#define BOOST_RATIO_EXTENSIONS #include #include #include @@ -148,7 +149,22 @@ int main() boost::ratio_string::symbol() == "E" )); } -// return 1; + { + BOOST_TEST(( + boost::ratio_string::prefix() == "kibi" + )); + BOOST_TEST(( + boost::ratio_string::symbol() == "Ki" + )); + } + { + BOOST_TEST(( + boost::ratio_string::prefix() == "gibi" + )); + BOOST_TEST(( + boost::ratio_string::symbol() == "Gi" + )); + } return boost::report_errors(); }