/usr/include/boost
NameSizeModeActions
accumulators/-0755rm
algorithm/-0755rm
align/-0755rm
archive/-0755rm
asio/-0755rm
assert/-0755rm
assign/-0755rm
atomic/-0755rm
beast/-0755rm
bimap/-0755rm
bind/-0755rm
callable_traits/-0755rm
chrono/-0755rm
circular_buffer/-0755rm
compatibility/-0755rm
compute/-0755rm
concept/-0755rm
concept_check/-0755rm
config/-0755rm
container/-0755rm
container_hash/-0755rm
context/-0755rm
contract/-0755rm
convert/-0755rm
core/-0755rm
coroutine/-0755rm
coroutine2/-0755rm
date_time/-0755rm
detail/-0755rm
dll/-0755rm
dynamic_bitset/-0755rm
endian/-0755rm
exception/-0755rm
fiber/-0755rm
filesystem/-0755rm
flyweight/-0755rm
format/-0755rm
function/-0755rm
functional/-0755rm
function_types/-0755rm
fusion/-0755rm
geometry/-0755rm
gil/-0755rm
graph/-0755rm
hana/-0755rm
heap/-0755rm
histogram/-0755rm
hof/-0755rm
icl/-0755rm
integer/-0755rm
interprocess/-0755rm
intrusive/-0755rm
io/-0755rm
iostreams/-0755rm
iterator/-0755rm
lambda/-0755rm
lexical_cast/-0755rm
locale/-0755rm
local_function/-0755rm
lockfree/-0755rm
log/-0755rm
logic/-0755rm
math/-0755rm
metaparse/-0755rm
move/-0755rm
mp11/-0755rm
mpi/-0755rm
mpl/-0755rm
msm/-0755rm
multiprecision/-0755rm
multi_array/-0755rm
multi_index/-0755rm
nowide/-0755rm
numeric/-0755rm
optional/-0755rm
outcome/-0755rm
parameter/-0755rm
pending/-0755rm
phoenix/-0755rm
polygon/-0755rm
poly_collection/-0755rm
pool/-0755rm
predef/-0755rm
preprocessor/-0755rm
process/-0755rm
program_options/-0755rm
property_map/-0755rm
property_tree/-0755rm
proto/-0755rm
ptr_container/-0755rm
python/-0755rm
qvm/-0755rm
random/-0755rm
range/-0755rm
ratio/-0755rm
regex/-0755rm
safe_numerics/-0755rm
serialization/-0755rm
signals2/-0755rm
smart_ptr/-0755rm
sort/-0755rm
spirit/-0755rm
stacktrace/-0755rm
statechart/-0755rm
static_string/-0755rm
stl_interfaces/-0755rm
system/-0755rm
test/-0755rm
thread/-0755rm
timer/-0755rm
tti/-0755rm
tuple/-0755rm
typeof/-0755rm
type_erasure/-0755rm
type_index/-0755rm
type_traits/-0755rm
units/-0755rm
unordered/-0755rm
utility/-0755rm
uuid/-0755rm
variant/-0755rm
variant2/-0755rm
vmd/-0755rm
wave/-0755rm
winapi/-0755rm
xpressive/-0755rm
yap/-0755rm
align.hpp6330644editdlrm
aligned_storage.hpp6530644editdlrm
any.hpp94720644editdlrm
array.hpp158270644editdlrm
asio.hpp74720644editdlrm
assert.hpp26250644editdlrm
assign.hpp5920644editdlrm
atomic.hpp7540644editdlrm
beast.hpp5910644editdlrm
bimap.hpp3870644editdlrm
bind.hpp18090644editdlrm
blank.hpp19850644editdlrm
blank_fwd.hpp6180644editdlrm
callable_traits.hpp21380644editdlrm
call_traits.hpp6330644editdlrm
cast.hpp5840644editdlrm
cerrno.hpp41870644editdlrm
checked_delete.hpp4140644editdlrm
chrono.hpp7060644editdlrm
circular_buffer.hpp19460644editdlrm
circular_buffer_fwd.hpp11370644editdlrm
compressed_pair.hpp6530644editdlrm
compute.hpp15230644editdlrm
concept_archetype.hpp237250644editdlrm
concept_check.hpp319770644editdlrm
config.hpp22160644editdlrm
contract.hpp18420644editdlrm
contract_macro.hpp564970644editdlrm
convert.hpp74020644editdlrm
crc.hpp948830644editdlrm
cregex.hpp7210644editdlrm
cstdfloat.hpp26110644editdlrm
cstdint.hpp184970644editdlrm
cstdlib.hpp17390644editdlrm
current_function.hpp16810644editdlrm
cxx11_char_types.hpp41600644editdlrm
date_time.hpp5400644editdlrm
dll.hpp7120644editdlrm
dynamic_bitset.hpp5600644editdlrm
dynamic_bitset_fwd.hpp6750644editdlrm
enable_shared_from_this.hpp5250644editdlrm
endian.hpp3640644editdlrm
exception_ptr.hpp3960644editdlrm
filesystem.hpp8620644editdlrm
flyweight.hpp6040644editdlrm
foreach.hpp455510644editdlrm
foreach_fwd.hpp17020644editdlrm
format.hpp21790644editdlrm
function.hpp25240644editdlrm
functional.hpp201240644editdlrm
function_equal.hpp7910644editdlrm
function_output_iterator.hpp6300644editdlrm
generator_iterator.hpp20280644editdlrm
geometry.hpp7040644editdlrm
get_pointer.hpp22880644editdlrm
gil.hpp16970644editdlrm
hana.hpp68070644editdlrm
histogram.hpp13120644editdlrm
hof.hpp20090644editdlrm
implicit_cast.hpp8100644editdlrm
indirect_reference.hpp10360644editdlrm
integer.hpp115930644editdlrm
integer_fwd.hpp51810644editdlrm
integer_traits.hpp83190644editdlrm
intrusive_ptr.hpp4810644editdlrm
io_fwd.hpp18790644editdlrm
is_placeholder.hpp6340644editdlrm
iterator.hpp4590644editdlrm
iterator_adaptors.hpp4660644editdlrm
lexical_cast.hpp32750644editdlrm
limits.hpp61560644editdlrm
locale.hpp8870644editdlrm
local_function.hpp188580644editdlrm
make_default.hpp15100644editdlrm
make_shared.hpp4640644editdlrm
make_unique.hpp2930644editdlrm
math_fwd.hpp9750644editdlrm
memory_order.hpp24570644editdlrm
mem_fn.hpp5960644editdlrm
metaparse.hpp52810644editdlrm
mp11.hpp6410644editdlrm
mpi.hpp11010644editdlrm
multi_array.hpp180210644editdlrm
multi_index_container.hpp449830644editdlrm
multi_index_container_fwd.hpp37760644editdlrm
next_prior.hpp51270644editdlrm
noncopyable.hpp4020644editdlrm
nondet_random.hpp5320644editdlrm
none.hpp16090644editdlrm
none_t.hpp9530644editdlrm
non_type.hpp6190644editdlrm
operators.hpp365740644editdlrm
operators_v1.hpp377350644editdlrm
optional.hpp5280644editdlrm
outcome.hpp16990644editdlrm
parameter.hpp10820644editdlrm
phoenix.hpp4760644editdlrm
pointee.hpp18070644editdlrm
pointer_cast.hpp36880644editdlrm
pointer_to_other.hpp11970644editdlrm
polymorphic_cast.hpp52820644editdlrm
polymorphic_pointer_cast.hpp28290644editdlrm
predef.h6350644editdlrm
preprocessor.hpp4870644editdlrm
process.hpp13660644editdlrm
program_options.hpp8200644editdlrm
progress.hpp57340644editdlrm
python.hpp29500644editdlrm
random.hpp38540644editdlrm
range.hpp6030644editdlrm
ratio.hpp3490644editdlrm
rational.hpp397200644editdlrm
ref.hpp3700644editdlrm
regex.h21740644editdlrm
regex.hpp8230644editdlrm
regex_fwd.hpp6930644editdlrm
scoped_array.hpp5060644editdlrm
scoped_ptr.hpp4970644editdlrm
scope_exit.hpp547450644editdlrm
shared_array.hpp5360644editdlrm
shared_container_iterator.hpp21540644editdlrm
shared_ptr.hpp5250644editdlrm
signals2.hpp7520644editdlrm
smart_ptr.hpp7800644editdlrm
spirit.hpp11330644editdlrm
stacktrace.hpp4990644editdlrm
static_assert.hpp72130644editdlrm
static_string.hpp4230644editdlrm
swap.hpp3740644editdlrm
thread.hpp8390644editdlrm
throw_exception.hpp43280644editdlrm
timer.hpp27810644editdlrm
tokenizer.hpp27830644editdlrm
token_functions.hpp193490644editdlrm
token_iterator.hpp38100644editdlrm
type.hpp4810644editdlrm
type_index.hpp107570644editdlrm
type_traits.hpp74310644editdlrm
unordered_map.hpp5680644editdlrm
unordered_set.hpp5680644editdlrm
utility.hpp8800644editdlrm
variant.hpp8830644editdlrm
version.hpp11170644editdlrm
visit_each.hpp6900644editdlrm
wave.hpp9440644editdlrm
weak_ptr.hpp4620644editdlrm
Edit: /usr/include/boost/rational.hpp (39720B)
// Boost rational.hpp header file ------------------------------------------// // (C) Copyright Paul Moore 1999. Permission to copy, use, modify, sell and // distribute this software is granted provided this copyright notice appears // in all copies. This software is provided "as is" without express or // implied warranty, and with no claim as to its suitability for any purpose. // boostinspect:nolicense (don't complain about the lack of a Boost license) // (Paul Moore hasn't been in contact for years, so there's no way to change the // license.) // See http://www.boost.org/libs/rational for documentation. // Credits: // Thanks to the boost mailing list in general for useful comments. // Particular contributions included: // Andrew D Jewell, for reminding me to take care to avoid overflow // Ed Brey, for many comments, including picking up on some dreadful typos // Stephen Silver contributed the test suite and comments on user-defined // IntType // Nickolay Mladenov, for the implementation of operator+= // Revision History // 02 Sep 13 Remove unneeded forward declarations; tweak private helper // function (Daryle Walker) // 30 Aug 13 Improve exception safety of "assign"; start modernizing I/O code // (Daryle Walker) // 27 Aug 13 Add cross-version constructor template, plus some private helper // functions; add constructor to exception class to take custom // messages (Daryle Walker) // 25 Aug 13 Add constexpr qualification wherever possible (Daryle Walker) // 05 May 12 Reduced use of implicit gcd (Mario Lang) // 05 Nov 06 Change rational_cast to not depend on division between different // types (Daryle Walker) // 04 Nov 06 Off-load GCD and LCM to Boost.Integer; add some invariant checks; // add std::numeric_limits<> requirement to help GCD (Daryle Walker) // 31 Oct 06 Recoded both operator< to use round-to-negative-infinity // divisions; the rational-value version now uses continued fraction // expansion to avoid overflows, for bug #798357 (Daryle Walker) // 20 Oct 06 Fix operator bool_type for CW 8.3 (Joaquín M López Muñoz) // 18 Oct 06 Use EXPLICIT_TEMPLATE_TYPE helper macros from Boost.Config // (Joaquín M López Muñoz) // 27 Dec 05 Add Boolean conversion operator (Daryle Walker) // 28 Sep 02 Use _left versions of operators from operators.hpp // 05 Jul 01 Recode gcd(), avoiding std::swap (Helmut Zeisel) // 03 Mar 01 Workarounds for Intel C++ 5.0 (David Abrahams) // 05 Feb 01 Update operator>> to tighten up input syntax // 05 Feb 01 Final tidy up of gcd code prior to the new release // 27 Jan 01 Recode abs() without relying on abs(IntType) // 21 Jan 01 Include Nickolay Mladenov's operator+= algorithm, // tidy up a number of areas, use newer features of operators.hpp // (reduces space overhead to zero), add operator!, // introduce explicit mixed-mode arithmetic operations // 12 Jan 01 Include fixes to handle a user-defined IntType better // 19 Nov 00 Throw on divide by zero in operator /= (John (EBo) David) // 23 Jun 00 Incorporate changes from Mark Rodgers for Borland C++ // 22 Jun 00 Change _MSC_VER to BOOST_MSVC so other compilers are not // affected (Beman Dawes) // 6 Mar 00 Fix operator-= normalization, #include (Jens Maurer) // 14 Dec 99 Modifications based on comments from the boost list // 09 Dec 99 Initial Version (Paul Moore) #ifndef BOOST_RATIONAL_HPP #define BOOST_RATIONAL_HPP #include // for BOOST_NO_STDC_NAMESPACE, BOOST_MSVC, etc #ifndef BOOST_NO_IOSTREAM #include // for std::setw #include // for std::noskipws, streamsize #include // for std::istream #include // for std::ostream #include // for std::ostringstream #endif #include // for NULL #include // for std::domain_error #include // for std::string implicit constructor #include // for boost::addable etc #include // for std::abs #include // for boost::call_traits #include // for BOOST_WORKAROUND #include // for BOOST_ASSERT #include // for boost::integer::gcd, lcm #include // for std::numeric_limits #include // for BOOST_STATIC_ASSERT #include #include #include #include #include #include // Control whether depreciated GCD and LCM functions are included (default: yes) #ifndef BOOST_CONTROL_RATIONAL_HAS_GCD #define BOOST_CONTROL_RATIONAL_HAS_GCD 1 #endif namespace boost { #if BOOST_CONTROL_RATIONAL_HAS_GCD template IntType gcd(IntType n, IntType m) { // Defer to the version in Boost.Integer return integer::gcd( n, m ); } template IntType lcm(IntType n, IntType m) { // Defer to the version in Boost.Integer return integer::lcm( n, m ); } #endif // BOOST_CONTROL_RATIONAL_HAS_GCD namespace rational_detail{ template struct is_compatible_integer; template struct is_compatible_integer::value>::type> { BOOST_STATIC_CONSTANT(bool, value = ((std::numeric_limits::is_specialized && std::numeric_limits::is_integer && (std::numeric_limits::digits <= std::numeric_limits::digits) && (std::numeric_limits::radix == std::numeric_limits::radix) && ((std::numeric_limits::is_signed == false) || (std::numeric_limits::is_signed == true)) && is_convertible::value) || is_same::value) || (is_class::value && is_class::value && is_convertible::value)); }; template struct is_compatible_integer::value>::type> { BOOST_STATIC_CONSTANT(bool, value = false); }; template struct is_backward_compatible_integer; template struct is_backward_compatible_integer::value>::type> { BOOST_STATIC_CONSTANT(bool, value = (std::numeric_limits::is_specialized && std::numeric_limits::is_integer && !is_compatible_integer::value && (std::numeric_limits::radix == std::numeric_limits::radix) && is_convertible::value)); }; template struct is_backward_compatible_integer::value>::type> { BOOST_STATIC_CONSTANT(bool, value = false); }; } class bad_rational : public std::domain_error { public: explicit bad_rational() : std::domain_error("bad rational: zero denominator") {} explicit bad_rational( char const *what ) : std::domain_error( what ) {} }; template class rational { // Class-wide pre-conditions BOOST_STATIC_ASSERT( ::std::numeric_limits::is_specialized ); // Helper types typedef typename boost::call_traits::param_type param_type; struct helper { IntType parts[2]; }; typedef IntType (helper::* bool_type)[2]; public: // Component type typedef IntType int_type; BOOST_CONSTEXPR rational() : num(0), den(1) {} template //, typename enable_if_c::value>::type> BOOST_CONSTEXPR rational(const T& n, typename enable_if_c< rational_detail::is_compatible_integer::value >::type const* = 0) : num(n), den(1) {} template BOOST_CXX14_CONSTEXPR rational(const T& n, const U& d, typename enable_if_c< rational_detail::is_compatible_integer::value && rational_detail::is_compatible_integer::value >::type const* = 0) : num(n), den(d) { normalize(); } template < typename NewType > BOOST_CONSTEXPR explicit rational(rational const &r, typename enable_if_c::value>::type const* = 0) : num(r.numerator()), den(is_normalized(int_type(r.numerator()), int_type(r.denominator())) ? r.denominator() : (BOOST_THROW_EXCEPTION(bad_rational("bad rational: denormalized conversion")), 0)){} template < typename NewType > BOOST_CONSTEXPR explicit rational(rational const &r, typename disable_if_c::value>::type const* = 0) : num(r.numerator()), den(is_normalized(int_type(r.numerator()), int_type(r.denominator())) && is_safe_narrowing_conversion(r.denominator()) && is_safe_narrowing_conversion(r.numerator()) ? r.denominator() : (BOOST_THROW_EXCEPTION(bad_rational("bad rational: denormalized conversion")), 0)){} // Default copy constructor and assignment are fine // Add assignment from IntType template BOOST_CXX14_CONSTEXPR typename enable_if_c< rational_detail::is_compatible_integer::value, rational & >::type operator=(const T& n) { return assign(static_cast(n), static_cast(1)); } // Assign in place template BOOST_CXX14_CONSTEXPR typename enable_if_c< rational_detail::is_compatible_integer::value && rational_detail::is_compatible_integer::value, rational & >::type assign(const T& n, const U& d) { return *this = rational(static_cast(n), static_cast(d)); } // // The following overloads should probably *not* be provided - // but are provided for backwards compatibity reasons only. // These allow for construction/assignment from types that // are wider than IntType only if there is an implicit // conversion from T to IntType, they will throw a bad_rational // if the conversion results in loss of precision or undefined behaviour. // template //, typename enable_if_c::value>::type> BOOST_CXX14_CONSTEXPR rational(const T& n, typename enable_if_c< rational_detail::is_backward_compatible_integer::value >::type const* = 0) { assign(n, static_cast(1)); } template BOOST_CXX14_CONSTEXPR rational(const T& n, const U& d, typename enable_if_c< (!rational_detail::is_compatible_integer::value || !rational_detail::is_compatible_integer::value) && std::numeric_limits::is_specialized && std::numeric_limits::is_integer && (std::numeric_limits::radix == std::numeric_limits::radix) && is_convertible::value && std::numeric_limits::is_specialized && std::numeric_limits::is_integer && (std::numeric_limits::radix == std::numeric_limits::radix) && is_convertible::value >::type const* = 0) { assign(n, d); } template BOOST_CXX14_CONSTEXPR typename enable_if_c< std::numeric_limits::is_specialized && std::numeric_limits::is_integer && !rational_detail::is_compatible_integer::value && (std::numeric_limits::radix == std::numeric_limits::radix) && is_convertible::value, rational & >::type operator=(const T& n) { return assign(n, static_cast(1)); } template BOOST_CXX14_CONSTEXPR typename enable_if_c< (!rational_detail::is_compatible_integer::value || !rational_detail::is_compatible_integer::value) && std::numeric_limits::is_specialized && std::numeric_limits::is_integer && (std::numeric_limits::radix == std::numeric_limits::radix) && is_convertible::value && std::numeric_limits::is_specialized && std::numeric_limits::is_integer && (std::numeric_limits::radix == std::numeric_limits::radix) && is_convertible::value, rational & >::type assign(const T& n, const U& d) { if(!is_safe_narrowing_conversion(n) || !is_safe_narrowing_conversion(d)) BOOST_THROW_EXCEPTION(bad_rational()); return *this = rational(static_cast(n), static_cast(d)); } // Access to representation BOOST_CONSTEXPR const IntType& numerator() const { return num; } BOOST_CONSTEXPR const IntType& denominator() const { return den; } // Arithmetic assignment operators BOOST_CXX14_CONSTEXPR rational& operator+= (const rational& r); BOOST_CXX14_CONSTEXPR rational& operator-= (const rational& r); BOOST_CXX14_CONSTEXPR rational& operator*= (const rational& r); BOOST_CXX14_CONSTEXPR rational& operator/= (const rational& r); template BOOST_CXX14_CONSTEXPR typename boost::enable_if_c::value, rational&>::type operator+= (const T& i) { num += i * den; return *this; } template BOOST_CXX14_CONSTEXPR typename boost::enable_if_c::value, rational&>::type operator-= (const T& i) { num -= i * den; return *this; } template BOOST_CXX14_CONSTEXPR typename boost::enable_if_c::value, rational&>::type operator*= (const T& i) { // Avoid overflow and preserve normalization IntType gcd = integer::gcd(static_cast(i), den); num *= i / gcd; den /= gcd; return *this; } template BOOST_CXX14_CONSTEXPR typename boost::enable_if_c::value, rational&>::type operator/= (const T& i) { // Avoid repeated construction IntType const zero(0); if(i == zero) BOOST_THROW_EXCEPTION(bad_rational()); if(num == zero) return *this; // Avoid overflow and preserve normalization IntType const gcd = integer::gcd(num, static_cast(i)); num /= gcd; den *= i / gcd; if(den < zero) { num = -num; den = -den; } return *this; } // Increment and decrement BOOST_CXX14_CONSTEXPR const rational& operator++() { num += den; return *this; } BOOST_CXX14_CONSTEXPR const rational& operator--() { num -= den; return *this; } BOOST_CXX14_CONSTEXPR rational operator++(int) { rational t(*this); ++(*this); return t; } BOOST_CXX14_CONSTEXPR rational operator--(int) { rational t(*this); --(*this); return t; } // Operator not BOOST_CONSTEXPR bool operator!() const { return !num; } // Boolean conversion #if BOOST_WORKAROUND(__MWERKS__,<=0x3003) // The "ISO C++ Template Parser" option in CW 8.3 chokes on the // following, hence we selectively disable that option for the // offending memfun. #pragma parse_mfunc_templ off #endif BOOST_CONSTEXPR operator bool_type() const { return operator !() ? 0 : &helper::parts; } #if BOOST_WORKAROUND(__MWERKS__,<=0x3003) #pragma parse_mfunc_templ reset #endif // Comparison operators BOOST_CXX14_CONSTEXPR bool operator< (const rational& r) const; BOOST_CXX14_CONSTEXPR bool operator> (const rational& r) const { return r < *this; } BOOST_CONSTEXPR bool operator== (const rational& r) const; template BOOST_CXX14_CONSTEXPR typename boost::enable_if_c::value, bool>::type operator< (const T& i) const { // Avoid repeated construction int_type const zero(0); // Break value into mixed-fraction form, w/ always-nonnegative remainder BOOST_ASSERT(this->den > zero); int_type q = this->num / this->den, r = this->num % this->den; while(r < zero) { r += this->den; --q; } // Compare with just the quotient, since the remainder always bumps the // value up. [Since q = floor(n/d), and if n/d < i then q < i, if n/d == i // then q == i, if n/d == i + r/d then q == i, and if n/d >= i + 1 then // q >= i + 1 > i; therefore n/d < i iff q < i.] return q < i; } template BOOST_CXX14_CONSTEXPR typename boost::enable_if_c::value, bool>::type operator>(const T& i) const { return operator==(i) ? false : !operator<(i); } template BOOST_CONSTEXPR typename boost::enable_if_c::value, bool>::type operator== (const T& i) const { return ((den == IntType(1)) && (num == i)); } private: // Implementation - numerator and denominator (normalized). // Other possibilities - separate whole-part, or sign, fields? IntType num; IntType den; // Helper functions static BOOST_CONSTEXPR int_type inner_gcd( param_type a, param_type b, int_type const &zero = int_type(0) ) { return b == zero ? a : inner_gcd(b, a % b, zero); } static BOOST_CONSTEXPR int_type inner_abs( param_type x, int_type const &zero = int_type(0) ) { return x < zero ? -x : +x; } // Representation note: Fractions are kept in normalized form at all // times. normalized form is defined as gcd(num,den) == 1 and den > 0. // In particular, note that the implementation of abs() below relies // on den always being positive. BOOST_CXX14_CONSTEXPR bool test_invariant() const; BOOST_CXX14_CONSTEXPR void normalize(); static BOOST_CONSTEXPR bool is_normalized( param_type n, param_type d, int_type const &zero = int_type(0), int_type const &one = int_type(1) ) { return d > zero && ( n != zero || d == one ) && inner_abs( inner_gcd(n, d, zero), zero ) == one; } // // Conversion checks: // // (1) From an unsigned type with more digits than IntType: // template BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits::digits > std::numeric_limits::digits) && (std::numeric_limits::is_signed == false), bool>::type is_safe_narrowing_conversion(const T& val) { return val < (T(1) << std::numeric_limits::digits); } // // (2) From a signed type with more digits than IntType, and IntType also signed: // template BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits::digits > std::numeric_limits::digits) && (std::numeric_limits::is_signed == true) && (std::numeric_limits::is_signed == true), bool>::type is_safe_narrowing_conversion(const T& val) { // Note that this check assumes IntType has a 2's complement representation, // we don't want to try to convert a std::numeric_limits::min() to // a T because that conversion may not be allowed (this happens when IntType // is from Boost.Multiprecision). return (val < (T(1) << std::numeric_limits::digits)) && (val >= -(T(1) << std::numeric_limits::digits)); } // // (3) From a signed type with more digits than IntType, and IntType unsigned: // template BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits::digits > std::numeric_limits::digits) && (std::numeric_limits::is_signed == true) && (std::numeric_limits::is_signed == false), bool>::type is_safe_narrowing_conversion(const T& val) { return (val < (T(1) << std::numeric_limits::digits)) && (val >= 0); } // // (4) From a signed type with fewer digits than IntType, and IntType unsigned: // template BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits::digits <= std::numeric_limits::digits) && (std::numeric_limits::is_signed == true) && (std::numeric_limits::is_signed == false), bool>::type is_safe_narrowing_conversion(const T& val) { return val >= 0; } // // (5) From an unsigned type with fewer digits than IntType, and IntType signed: // template BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits::digits <= std::numeric_limits::digits) && (std::numeric_limits::is_signed == false) && (std::numeric_limits::is_signed == true), bool>::type is_safe_narrowing_conversion(const T&) { return true; } // // (6) From an unsigned type with fewer digits than IntType, and IntType unsigned: // template BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits::digits <= std::numeric_limits::digits) && (std::numeric_limits::is_signed == false) && (std::numeric_limits::is_signed == false), bool>::type is_safe_narrowing_conversion(const T&) { return true; } // // (7) From an signed type with fewer digits than IntType, and IntType signed: // template BOOST_CONSTEXPR static typename boost::enable_if_c<(std::numeric_limits::digits <= std::numeric_limits::digits) && (std::numeric_limits::is_signed == true) && (std::numeric_limits::is_signed == true), bool>::type is_safe_narrowing_conversion(const T&) { return true; } }; // Unary plus and minus template BOOST_CONSTEXPR inline rational operator+ (const rational& r) { return r; } template BOOST_CXX14_CONSTEXPR inline rational operator- (const rational& r) { return rational(static_cast(-r.numerator()), r.denominator()); } // Arithmetic assignment operators template BOOST_CXX14_CONSTEXPR rational& rational::operator+= (const rational& r) { // This calculation avoids overflow, and minimises the number of expensive // calculations. Thanks to Nickolay Mladenov for this algorithm. // // Proof: // We have to compute a/b + c/d, where gcd(a,b)=1 and gcd(b,c)=1. // Let g = gcd(b,d), and b = b1*g, d=d1*g. Then gcd(b1,d1)=1 // // The result is (a*d1 + c*b1) / (b1*d1*g). // Now we have to normalize this ratio. // Let's assume h | gcd((a*d1 + c*b1), (b1*d1*g)), and h > 1 // If h | b1 then gcd(h,d1)=1 and hence h|(a*d1+c*b1) => h|a. // But since gcd(a,b1)=1 we have h=1. // Similarly h|d1 leads to h=1. // So we have that h | gcd((a*d1 + c*b1) , (b1*d1*g)) => h|g // Finally we have gcd((a*d1 + c*b1), (b1*d1*g)) = gcd((a*d1 + c*b1), g) // Which proves that instead of normalizing the result, it is better to // divide num and den by gcd((a*d1 + c*b1), g) // Protect against self-modification IntType r_num = r.num; IntType r_den = r.den; IntType g = integer::gcd(den, r_den); den /= g; // = b1 from the calculations above num = num * (r_den / g) + r_num * den; g = integer::gcd(num, g); num /= g; den *= r_den/g; return *this; } template BOOST_CXX14_CONSTEXPR rational& rational::operator-= (const rational& r) { // Protect against self-modification IntType r_num = r.num; IntType r_den = r.den; // This calculation avoids overflow, and minimises the number of expensive // calculations. It corresponds exactly to the += case above IntType g = integer::gcd(den, r_den); den /= g; num = num * (r_den / g) - r_num * den; g = integer::gcd(num, g); num /= g; den *= r_den/g; return *this; } template BOOST_CXX14_CONSTEXPR rational& rational::operator*= (const rational& r) { // Protect against self-modification IntType r_num = r.num; IntType r_den = r.den; // Avoid overflow and preserve normalization IntType gcd1 = integer::gcd(num, r_den); IntType gcd2 = integer::gcd(r_num, den); num = (num/gcd1) * (r_num/gcd2); den = (den/gcd2) * (r_den/gcd1); return *this; } template BOOST_CXX14_CONSTEXPR rational& rational::operator/= (const rational& r) { // Protect against self-modification IntType r_num = r.num; IntType r_den = r.den; // Avoid repeated construction IntType zero(0); // Trap division by zero if (r_num == zero) BOOST_THROW_EXCEPTION(bad_rational()); if (num == zero) return *this; // Avoid overflow and preserve normalization IntType gcd1 = integer::gcd(num, r_num); IntType gcd2 = integer::gcd(r_den, den); num = (num/gcd1) * (r_den/gcd2); den = (den/gcd2) * (r_num/gcd1); if (den < zero) { num = -num; den = -den; } return *this; } // // Non-member operators: previously these were provided by Boost.Operator, but these had a number of // drawbacks, most notably, that in order to allow inter-operability with IntType code such as this: // // rational r(3); // assert(r == 3.5); // compiles and passes!! // // Happens to be allowed as well :-( // // There are three possible cases for each operator: // 1) rational op rational. // 2) rational op integer // 3) integer op rational // Cases (1) and (2) are folded into the one function. // template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value || is_same, Arg>::value, rational >::type operator + (const rational& a, const Arg& b) { rational t(a); return t += b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, rational >::type operator + (const Arg& b, const rational& a) { rational t(a); return t += b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value || is_same, Arg>::value, rational >::type operator - (const rational& a, const Arg& b) { rational t(a); return t -= b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, rational >::type operator - (const Arg& b, const rational& a) { rational t(a); return -(t -= b); } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value || is_same, Arg>::value, rational >::type operator * (const rational& a, const Arg& b) { rational t(a); return t *= b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, rational >::type operator * (const Arg& b, const rational& a) { rational t(a); return t *= b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value || is_same, Arg>::value, rational >::type operator / (const rational& a, const Arg& b) { rational t(a); return t /= b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, rational >::type operator / (const Arg& b, const rational& a) { rational t(b); return t /= a; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value || is_same, Arg>::value, bool>::type operator <= (const rational& a, const Arg& b) { return !(a > b); } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, bool>::type operator <= (const Arg& b, const rational& a) { return a >= b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value || is_same, Arg>::value, bool>::type operator >= (const rational& a, const Arg& b) { return !(a < b); } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, bool>::type operator >= (const Arg& b, const rational& a) { return a <= b; } template BOOST_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value || is_same, Arg>::value, bool>::type operator != (const rational& a, const Arg& b) { return !(a == b); } template BOOST_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, bool>::type operator != (const Arg& b, const rational& a) { return !(b == a); } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, bool>::type operator < (const Arg& b, const rational& a) { return a > b; } template BOOST_CXX14_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, bool>::type operator > (const Arg& b, const rational& a) { return a < b; } template BOOST_CONSTEXPR inline typename boost::enable_if_c < rational_detail::is_compatible_integer::value, bool>::type operator == (const Arg& b, const rational& a) { return a == b; } // Comparison operators template BOOST_CXX14_CONSTEXPR bool rational::operator< (const rational& r) const { // Avoid repeated construction int_type const zero( 0 ); // This should really be a class-wide invariant. The reason for these // checks is that for 2's complement systems, INT_MIN has no corresponding // positive, so negating it during normalization keeps it INT_MIN, which // is bad for later calculations that assume a positive denominator. BOOST_ASSERT( this->den > zero ); BOOST_ASSERT( r.den > zero ); // Determine relative order by expanding each value to its simple continued // fraction representation using the Euclidian GCD algorithm. struct { int_type n, d, q, r; } ts = { this->num, this->den, static_cast(this->num / this->den), static_cast(this->num % this->den) }, rs = { r.num, r.den, static_cast(r.num / r.den), static_cast(r.num % r.den) }; unsigned reverse = 0u; // Normalize negative moduli by repeatedly adding the (positive) denominator // and decrementing the quotient. Later cycles should have all positive // values, so this only has to be done for the first cycle. (The rules of // C++ require a nonnegative quotient & remainder for a nonnegative dividend // & positive divisor.) while ( ts.r < zero ) { ts.r += ts.d; --ts.q; } while ( rs.r < zero ) { rs.r += rs.d; --rs.q; } // Loop through and compare each variable's continued-fraction components for ( ;; ) { // The quotients of the current cycle are the continued-fraction // components. Comparing two c.f. is comparing their sequences, // stopping at the first difference. if ( ts.q != rs.q ) { // Since reciprocation changes the relative order of two variables, // and c.f. use reciprocals, the less/greater-than test reverses // after each index. (Start w/ non-reversed @ whole-number place.) return reverse ? ts.q > rs.q : ts.q < rs.q; } // Prepare the next cycle reverse ^= 1u; if ( (ts.r == zero) || (rs.r == zero) ) { // At least one variable's c.f. expansion has ended break; } ts.n = ts.d; ts.d = ts.r; ts.q = ts.n / ts.d; ts.r = ts.n % ts.d; rs.n = rs.d; rs.d = rs.r; rs.q = rs.n / rs.d; rs.r = rs.n % rs.d; } // Compare infinity-valued components for otherwise equal sequences if ( ts.r == rs.r ) { // Both remainders are zero, so the next (and subsequent) c.f. // components for both sequences are infinity. Therefore, the sequences // and their corresponding values are equal. return false; } else { #ifdef BOOST_MSVC #pragma warning(push) #pragma warning(disable:4800) #endif // Exactly one of the remainders is zero, so all following c.f. // components of that variable are infinity, while the other variable // has a finite next c.f. component. So that other variable has the // lesser value (modulo the reversal flag!). return ( ts.r != zero ) != static_cast( reverse ); #ifdef BOOST_MSVC #pragma warning(pop) #endif } } template BOOST_CONSTEXPR inline bool rational::operator== (const rational& r) const { return ((num == r.num) && (den == r.den)); } // Invariant check template BOOST_CXX14_CONSTEXPR inline bool rational::test_invariant() const { return ( this->den > int_type(0) ) && ( integer::gcd(this->num, this->den) == int_type(1) ); } // Normalisation template BOOST_CXX14_CONSTEXPR void rational::normalize() { // Avoid repeated construction IntType zero(0); if (den == zero) BOOST_THROW_EXCEPTION(bad_rational()); // Handle the case of zero separately, to avoid division by zero if (num == zero) { den = IntType(1); return; } IntType g = integer::gcd(num, den); num /= g; den /= g; if (den < -(std::numeric_limits::max)()) { BOOST_THROW_EXCEPTION(bad_rational("bad rational: non-zero singular denominator")); } // Ensure that the denominator is positive if (den < zero) { num = -num; den = -den; } BOOST_ASSERT( this->test_invariant() ); } #ifndef BOOST_NO_IOSTREAM namespace detail { // A utility class to reset the format flags for an istream at end // of scope, even in case of exceptions struct resetter { resetter(std::istream& is) : is_(is), f_(is.flags()) {} ~resetter() { is_.flags(f_); } std::istream& is_; std::istream::fmtflags f_; // old GNU c++ lib has no ios_base }; } // Input and output template std::istream& operator>> (std::istream& is, rational& r) { using std::ios; IntType n = IntType(0), d = IntType(1); char c = 0; detail::resetter sentry(is); if ( is >> n ) { if ( is.get(c) ) { if ( c == '/' ) { if ( is >> std::noskipws >> d ) try { r.assign( n, d ); } catch ( bad_rational & ) { // normalization fail try { is.setstate(ios::failbit); } catch ( ... ) {} // don't throw ios_base::failure... if ( is.exceptions() & ios::failbit ) throw; // ...but the original exception instead // ELSE: suppress the exception, use just error flags } } else is.setstate( ios::failbit ); } } return is; } // Add manipulators for output format? template std::ostream& operator<< (std::ostream& os, const rational& r) { // The slash directly precedes the denominator, which has no prefixes. std::ostringstream ss; ss.copyfmt( os ); ss.tie( NULL ); ss.exceptions( std::ios::goodbit ); ss.width( 0 ); ss << std::noshowpos << std::noshowbase << '/' << r.denominator(); // The numerator holds the showpos, internal, and showbase flags. std::string const tail = ss.str(); std::streamsize const w = os.width() - static_cast( tail.size() ); ss.clear(); ss.str( "" ); ss.flags( os.flags() ); ss << std::setw( w < 0 || (os.flags() & std::ios::adjustfield) != std::ios::internal ? 0 : w ) << r.numerator(); return os << ss.str() + tail; } #endif // BOOST_NO_IOSTREAM // Type conversion template BOOST_CONSTEXPR inline T rational_cast(const rational& src) { return static_cast(src.numerator())/static_cast(src.denominator()); } // Do not use any abs() defined on IntType - it isn't worth it, given the // difficulties involved (Koenig lookup required, there may not *be* an abs() // defined, etc etc). template BOOST_CXX14_CONSTEXPR inline rational abs(const rational& r) { return r.numerator() >= IntType(0)? r: -r; } namespace integer { template struct gcd_evaluator< rational > { typedef rational result_type, first_argument_type, second_argument_type; result_type operator() ( first_argument_type const &a , second_argument_type const &b ) const { return result_type(integer::gcd(a.numerator(), b.numerator()), integer::lcm(a.denominator(), b.denominator())); } }; template struct lcm_evaluator< rational > { typedef rational result_type, first_argument_type, second_argument_type; result_type operator() ( first_argument_type const &a , second_argument_type const &b ) const { return result_type(integer::lcm(a.numerator(), b.numerator()), integer::gcd(a.denominator(), b.denominator())); } }; } // namespace integer } // namespace boost #endif // BOOST_RATIONAL_HPP