main( ) { Rational X; Rational Sum = 0; Rational Max = 0; int N = 0; cout << "Type as many rational numbers as you want\n"; while( cin >> X ) { cout << "Read " << X << '\n'; Sum += X; if( X > Max ) Max = X; N++; } cout << "Read " << N << " rationals\n"; if( Max > IntType( 0 ) ) cout << "Largest positive number is " << Max << '\n'; if( N > 0 ) cout << "Average is " << Sum / IntType( N ) << '\n'; return 0; }
inline IntType Round2(FloatType Number) { if ((Number - (FloatType)floor(double(Number))) < 0.5) return IntType(floor(double(Number))); else return IntType(ceil(double(Number))); }
inline SPROUT_CONSTEXPR IntType str_to_int_impl(CStrIterator str, int base, bool negative) { return *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('0') ? *sprout::next(str) == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('x') || *sprout::next(str) == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('X') ? sprout::detail::str_to_int_impl_1<IntType>( sprout::next(str, 2), base ? base : 16, IntType(), sprout::detail::char_to_int<IntType>(*sprout::next(str, 2), base ? base : 16), negative ) : sprout::detail::str_to_int_impl_1<IntType>( sprout::next(str), base ? base : 8, IntType(), sprout::detail::char_to_int<IntType>(*sprout::next(str), base ? base : 8), negative ) : sprout::detail::str_to_int_impl_1<IntType>( str, base ? base : 10, IntType(), sprout::detail::char_to_int<IntType>(*str, base ? base : 10), negative ) ; }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult64, IntType, int64_types) { typedef boost::random::const_mod<IntType, INT64_C(2147483563652738498)> const_mod_type; BOOST_CHECK_EQUAL((const_mod_type::mult(0, 0)), IntType(0)); BOOST_CHECK_EQUAL((const_mod_type::mult(0, 2147483562)), IntType(0)); BOOST_CHECK_EQUAL((const_mod_type::mult(2147483562, 0)), IntType(0)); BOOST_CHECK_EQUAL((const_mod_type::mult(2147483562, 2147483562)), IntType(INT64_C(316718521754730848))); BOOST_CHECK_EQUAL((const_mod_type::mult(1234567890, 1234657890)), IntType(INT64_C(1524268986129152100))); BOOST_CHECK_EQUAL((const_mod_type::mult(INT64_C(1234567890726352938), INT64_C(1234657890736453927))), IntType(INT64_C(88656187017794672))); }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult_add64, IntType, int64_types) { typedef boost::random::const_mod<IntType, INT64_C(2147483563652738498)> const_mod_type; BOOST_CHECK_EQUAL((const_mod_type::mult_add(0, 0, 0)), IntType(0)); BOOST_CHECK_EQUAL((const_mod_type::mult_add(0, 2147483562, 827364)), IntType(827364)); BOOST_CHECK_EQUAL((const_mod_type::mult_add(2147483562, 0, 827364)), IntType(827364)); BOOST_CHECK_EQUAL((const_mod_type::mult_add(2147483562, 2147483562, 2147483562)), IntType(INT64_C(316718523902214410))); BOOST_CHECK_EQUAL((const_mod_type::mult_add(1234567890, 1234657890, 1726384759)), IntType(INT64_C(1524268987855536859))); BOOST_CHECK_EQUAL((const_mod_type::mult_add(INT64_C(1234567890726352938), INT64_C(1234657890736453927), INT64_C(1726384759726488649))), IntType(INT64_C(1815040946744283321))); }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_invert64, IntType, int64_types) { typedef boost::random::const_mod<IntType, INT64_C(2147483563652738498)> const_mod_type; BOOST_CHECK_EQUAL((const_mod_type::invert(0)), IntType(0)); BOOST_CHECK_EQUAL((const_mod_type::mult_add(0, 0, 0)), IntType(0)); IntType inverse; inverse = const_mod_type::invert(INT64_C(7362947769)); BOOST_CHECK_EQUAL((const_mod_type::mult(INT64_C(7362947769), inverse)), IntType(1)); inverse = const_mod_type::invert(INT64_C(1263142436887493875)); BOOST_CHECK_EQUAL((const_mod_type::mult(INT64_C(1263142436887493875), inverse)), IntType(1)); }
inline SPROUT_CONSTEXPR sprout::rational<IntType> operator/(sprout::rational<IntType> const& lhs, sprout::rational<IntType> const& rhs) { return rhs.numerator() == IntType(0) ? throw sprout::bad_rational() : lhs.numerator() == IntType(0) ? lhs : sprout::detail::rational_div_impl( lhs, rhs, sprout::gcd(lhs.numerator(), rhs.numerator()), sprout::gcd(rhs.denominator(), lhs.denominator()) ) ; }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_invert8, IntType, int8_types) { for(int i = 1; i < 127; ++i) { IntType inverse = boost::random::const_mod<IntType, 127>::invert(IntType(i)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 127>::mult(IntType(i), inverse)), 1); } int modulus = (std::numeric_limits<IntType>::max)() + 1; for(int i = 1; i < modulus; i += 2) { IntType inverse = boost::random::const_mod<IntType, 0>::invert(IntType(i)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 0>::mult(IntType(i), inverse)), 1); } }
inline SPROUT_CONSTEXPR typename std::enable_if< std::is_unsigned<IntType>::value, IntType >::type ascii_to_int(CStrIterator str) { return sprout::ascii::isspace(*str) ? sprout::detail::ascii_to_int<IntType>(sprout::next(str)) : *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('+') ? sprout::detail::ascii_to_int_impl<IntType>(sprout::next(str), IntType(), false) : sprout::detail::ascii_to_int_impl<IntType>(str, IntType(), false) ; }
inline SPROUT_CONSTEXPR typename std::enable_if< sprout::is_unsigned<IntType>::value, IntType >::type ascii_to_int(NullTerminatedIterator const& str) { typedef typename std::iterator_traits<NullTerminatedIterator>::value_type value_type; return sprout::ascii::isspace(*str) ? sprout::detail::ascii_to_int<IntType>(sprout::next(str)) : *str == SPROUT_CHAR_LITERAL('+', value_type) ? sprout::detail::ascii_to_int_impl<IntType>(sprout::next(str), IntType(), false) : sprout::detail::ascii_to_int_impl<IntType>(str, IntType(), false) ; }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult8, IntType, int8_types) { for(int i = 0; i < 127; ++i) { for(int j = 0; j < 127; ++j) { BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 127>::mult(IntType(i), IntType(j))), i * j % 127); } } int modulus = (std::numeric_limits<IntType>::max)() + 1; for(int i = 0; i < modulus; ++i) { for(int j = 0; j < modulus; ++j) { BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 0>::mult(IntType(i), IntType(j))), i * j % modulus); } } }
inline SPROUT_CXX14_CONSTEXPR IntType seed_one_int(Sseq& seq) { typedef typename sprout::random::detail::seed_log<IntType, m>::type log_t; typedef typename sprout::random::detail::seed_k<IntType, m, log_t::value>::type k_t; sprout::array<std::uint_least32_t, log_t::value / 32 + 4> arr{{}}; seq.generate(arr.begin(), arr.begin() + (k_t::value + 3)); IntType s = 0; for (int j = 0; j < k_t::value; ++j) { IntType digit = sprout::random::detail::const_mod<IntType, m>::apply(IntType(arr[j + 3])); IntType mult = IntType(1) << 32 * j; s = sprout::random::detail::const_mod<IntType, m>::mult_add(mult, digit, s); } return s; }
inline SPROUT_CONSTEXPR IntType seed_one_int_impl(Array const& arr, IntType s, int j) { return j < k ? sprout::random::detail::seed_one_int_impl<IntType, m, k>( arr, sprout::random::detail::const_mod<IntType, m>::mult_add( IntType(1) << 32 * j, sprout::random::detail::const_mod<IntType, m>::apply(IntType(arr[j + 3])), s ), j + 1 ) : s ; }
void rational<IntType>::normalize() { // Avoid repeated construction IntType zero(0); if (den == zero) throw_exception( bad_rational() ); // Handle the case of zero separately, to avoid division by zero if (num == zero) { den = IntType(1); return; } IntType g = math::gcd(num, den); num /= g; den /= g; // Ensure that the denominator is positive if (den < zero) { num = -num; den = -den; } BOOST_ASSERT( this->test_invariant() ); }
inline SPROUT_CONSTEXPR Elem int_to_char(IntType val, int base) { return SPROUT_ASSERT(2 <= base && base <= 36), SPROUT_ASSERT(IntType(0) <= val && val < static_cast<IntType>(base)), val < 10 ? static_cast<Elem>('0') + val : static_cast<Elem>('a') + (val - 10) ; }
void rational<IntType>::normalize() { // Avoid repeated construction IntType zero(0); if (den == zero) throw bad_rational(); // Handle the case of zero separately, to avoid division by zero if (num == zero) { den = IntType(1); return; } IntType g = gcd<IntType>(num, den); num /= g; den /= g; // Ensure that the denominator is positive if (den < zero) { num = -num; den = -den; } }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult32, IntType, int32_types) { BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(0, 0)), IntType(0)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(0, 2147483562)), IntType(0)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(2147483562, 0)), IntType(0)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(2147483562, 2147483562)), IntType(1)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(1234567890, 1234657890)), IntType(813106682)); }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_add32, IntType, int32_types) { BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(0, 0)), IntType(0)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(0, 2147483562)), IntType(2147483562)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(2147483562, 0)), IntType(2147483562)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(2147483562, 2147483562)), IntType(2147483561)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(1234567890, 1234657890)), IntType(321742217)); }
inline SPROUT_CONSTEXPR sprout::rational<IntType> abs(sprout::rational<IntType> const& x) { return x.numerator() >= IntType(0) ? x : sprout::detail::rational_construct_access<IntType>::raw_construct( -x.numerator(), x.denominator() ) ; }
IntType IntType::deserialize(BinaryRefReader& brr) { auto type_code = brr.get_uint8_t(); if (type_code != INTTYPE_CODE) BtcUtils::throw_type_error(INTTYPE_CODE, type_code); return IntType(brr.get_var_int()); }
inline SPROUT_CONSTEXPR IntType get_one_int_impl(InputIterator first, InputIterator last, IntType s, int j) { return j < k ? first != last ? sprout::random::detail::get_one_int_impl<IntType, m, k>( sprout::next(first), last, sprout::random::detail::const_mod<IntType, m>::mult_add( IntType(1) << 32 * j, sprout::random::detail::const_mod<IntType, m>::apply(IntType(*first)), s ), j + 1 ) : throw std::invalid_argument("Not enough elements in call to seed.") : s ; }
bool rational<IntType>::operator> (param_type i) const { // Trap equality first if (num == i && den == IntType(1)) return false; // Otherwise, we can use operator< return !operator<(i); }
bool LessThan(Random& r, IntType p0, IntType c, IntType q, UniformInteger<IntType, bits>& j) { for (int k = 0;; ++k) { if (j. LessThanEqual(r, - p0, c)) return false; if (j.GreaterThanEqual(r, q - p0, c)) return true; p0 = (p0 << bits) - IntType(Digit(r,k)) * q; c <<= bits; } }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_invert32, IntType, int32_types) { BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::invert(0)), IntType(0)); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult_add(0, 0, 0)), IntType(0)); IntType inverse; inverse = boost::random::const_mod<IntType, 2147483563>::invert(2147483562); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(2147483562, inverse)), IntType(1)); inverse = boost::random::const_mod<IntType, 2147483563>::invert(1234567890); BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(1234567890, inverse)), IntType(1)); }
inline SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::rational<IntType>& rhs) { IntType n = IntType(0); IntType d = IntType(1); Elem c = 0; sprout::detail::io::ios_flags_saver saver(lhs); lhs >> n; c = lhs.get(); if (c != Elem('/')) { lhs.clear(std::istream::badbit); } lhs >> std::noskipws; lhs >> d; if (lhs) { rhs.assign(n, d); } return lhs; }
BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult_add8, IntType, int8_types) { for(int i = 0; i < 127; i += 5) { for(int j = 0; j < 127; j += 3) { for(int k = 0; k < 127; k += 3) { BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 127>::mult_add(IntType(i), IntType(j), IntType(k))), (i * j + k) % 127); } } } { int modulus = (std::numeric_limits<IntType>::max)() + 1; for(int i = 0; i < modulus; i += 5) { for(int j = 0; j < modulus; j += 3) { for(int k = 0; k < modulus; k += 3) { BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 0>::mult_add(IntType(i), IntType(j), IntType(k))), (i * j + k) % modulus); } } } } }
result_type operator()() { // calculating the range every time may seem wasteful. However, this // makes the information locally available for the optimizer. result_type range = max()-min()+1; int j = k*IntType(y-min())/range; // assert(0 <= j && j < k); y = v[j]; v[j] = _rng(); return y; }
inline SPROUT_CONSTEXPR bool rational_less_impl_2( sprout::rational<IntType> const& lhs, sprout::rational<IntType> const& rhs, IntType d1, IntType q1, IntType r1, IntType d2, IntType q2, IntType r2, unsigned reverse = 0 ) { return q1 != q2 ? reverse ? q1 > q2 : q1 < q2 : r1 == IntType(0) || r2 == IntType(0) ? r1 == r2 ? false : (r1 != IntType(0)) != static_cast<bool>(reverse ^ 1) : sprout::detail::rational_less_impl_2( lhs, rhs, r1, d1 / r1, d1 % r1, r2, d2 / r2, d2 % r2, reverse ^ 1 ) ; }
inline SPROUT_CONSTEXPR sprout::tuples::tuple<IntType, bool> ovalue_at(std::size_t i) { return i < 8 ? sprout::tuples::tuple<IntType, bool>( static_cast<IntType>(sprout::weed::detail::ovalues<void>::value[i]), true ) : sprout::tuples::tuple<IntType, bool>( IntType(), false ) ; }
inline SPROUT_CONSTEXPR bool rational_less_impl( sprout::rational<IntType> const& lhs, typename sprout::rational<IntType>::param_type rhs, IntType q, IntType r ) { return r < IntType(0) ? sprout::detail::rational_less_impl( lhs, rhs, r + lhs.denominator(), q - 1 ) : q < rhs ; }