// Copyright Aleksey Gurtovoy 2003-2004 // // 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) // // See http://www.boost.org/libs/mpl for documentation. // $Id: numeric_ops.cpp 49268 2008-10-11 06:26:17Z agurtovoy $ // $Date: 2008-10-10 23:26:17 -0700 (Fri, 10 Oct 2008) $ // $Revision: 49268 $ #include #include #include #include #include #include struct complex_tag : int_<10> {}; template< typename Re, typename Im > struct complex { typedef complex_tag tag; typedef complex type; typedef Re real; typedef Im imag; }; template< typename C > struct real : C::real {}; template< typename C > struct imag : C::imag {}; namespace boost { namespace mpl { template<> struct BOOST_MPL_AUX_NUMERIC_CAST< integral_c_tag,complex_tag > { template< typename N > struct apply : complex< N, integral_c< typename N::value_type, 0 > > { }; }; template<> struct plus_impl< complex_tag,complex_tag > { template< typename N1, typename N2 > struct apply : complex< plus< typename N1::real, typename N2::real > , plus< typename N1::imag, typename N2::imag > > { }; }; template<> struct times_impl< complex_tag,complex_tag > { template< typename N1, typename N2 > struct apply : complex< minus< times< typename N1::real, typename N2::real > , times< typename N1::imag, typename N2::imag > > , plus< times< typename N1::real, typename N2::imag > , times< typename N1::imag, typename N2::real > > > { }; }; template<> struct equal_to_impl< complex_tag,complex_tag > { template< typename N1, typename N2 > struct apply : and_< equal_to< typename N1::real, typename N2::real > , equal_to< typename N1::imag, typename N2::imag > > { }; }; }} typedef int_<2> i; typedef complex< int_<5>, int_<-1> > c1; typedef complex< int_<-5>, int_<1> > c2; MPL_TEST_CASE() { typedef plus::type r1; MPL_ASSERT_RELATION( real::value, ==, 0 ); MPL_ASSERT_RELATION( imag::value, ==, 0 ); typedef plus::type r2; MPL_ASSERT_RELATION( real::value, ==, 10 ); MPL_ASSERT_RELATION( imag::value, ==, -2 ); typedef plus::type r3; MPL_ASSERT_RELATION( real::value, ==, -10 ); MPL_ASSERT_RELATION( imag::value, ==, 2 ); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) typedef plus::type r4; MPL_ASSERT_RELATION( real::value, ==, 7 ); MPL_ASSERT_RELATION( imag::value, ==, -1 ); typedef plus::type r5; MPL_ASSERT_RELATION( real::value, ==, -3 ); MPL_ASSERT_RELATION( imag::value, ==, 1 ); #endif } MPL_TEST_CASE() { typedef times::type r1; MPL_ASSERT_RELATION( real::value, ==, -24 ); MPL_ASSERT_RELATION( imag::value, ==, 10 ); typedef times::type r2; MPL_ASSERT_RELATION( real::value, ==, 24 ); MPL_ASSERT_RELATION( imag::value, ==, -10 ); typedef times::type r3; MPL_ASSERT_RELATION( real::value, ==, 24 ); MPL_ASSERT_RELATION( imag::value, ==, -10 ); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) typedef times::type r4; MPL_ASSERT_RELATION( real::value, ==, 10 ); MPL_ASSERT_RELATION( imag::value, ==, -2 ); typedef times::type r5; MPL_ASSERT_RELATION( real::value, ==, -10 ); MPL_ASSERT_RELATION( imag::value, ==, 2 ); #endif } MPL_TEST_CASE() { MPL_ASSERT(( equal_to )); MPL_ASSERT(( equal_to )); MPL_ASSERT_NOT(( equal_to )); MPL_ASSERT(( equal_to, long_<-1> > > )); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) MPL_ASSERT_NOT(( equal_to )); MPL_ASSERT_NOT(( equal_to )); #endif }