rr.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 720 行 · 第 1/2 页
HPP
720 行
// Copyright John Maddock 2007.// Use, modification and distribution are subject to 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)#include <boost/config.hpp>#include <boost/limits.hpp>#include <boost/math/tools/real_cast.hpp>#include <boost/math/tools/precision.hpp>#include <boost/math/constants/constants.hpp>#include <boost/math/tools/roots.hpp>#include <boost/math/special_functions/fpclassify.hpp>#include <ostream>#include <istream>#include <boost/config/no_tr1/cmath.hpp>#include <NTL/RR.h>#ifndef BOOST_MATH_NTL_RR_HPP#define BOOST_MATH_NTL_RR_HPPnamespace boost{ namespace math{namespace ntl{class RR;RR ldexp(RR r, int exp);RR frexp(RR r, int* exp);class RR{public: // Constructors: RR() {} RR(const ::NTL::RR& c) : m_value(c){} RR(char c) { m_value = c; }#ifndef BOOST_NO_INTRINSIC_WCHAR_T RR(wchar_t c) { m_value = c; }#endif RR(unsigned char c) { m_value = c; } RR(signed char c) { m_value = c; } RR(unsigned short c) { m_value = c; } RR(short c) { m_value = c; } RR(unsigned int c) { assign_large_int(c); } RR(int c) { assign_large_int(c); } RR(unsigned long c) { assign_large_int(c); } RR(long c) { assign_large_int(c); }#ifdef BOOST_HAS_LONG_LONG RR(boost::ulong_long_type c) { assign_large_int(c); } RR(boost::long_long_type c) { assign_large_int(c); }#endif RR(float c) { m_value = c; } RR(double c) { m_value = c; } RR(long double c) { assign_large_real(c); } // Assignment: RR& operator=(char c) { m_value = c; return *this; } RR& operator=(unsigned char c) { m_value = c; return *this; } RR& operator=(signed char c) { m_value = c; return *this; }#ifndef BOOST_NO_INTRINSIC_WCHAR_T RR& operator=(wchar_t c) { m_value = c; return *this; }#endif RR& operator=(short c) { m_value = c; return *this; } RR& operator=(unsigned short c) { m_value = c; return *this; } RR& operator=(int c) { assign_large_int(c); return *this; } RR& operator=(unsigned int c) { assign_large_int(c); return *this; } RR& operator=(long c) { assign_large_int(c); return *this; } RR& operator=(unsigned long c) { assign_large_int(c); return *this; }#ifdef BOOST_HAS_LONG_LONG RR& operator=(boost::long_long_type c) { assign_large_int(c); return *this; } RR& operator=(boost::ulong_long_type c) { assign_large_int(c); return *this; }#endif RR& operator=(float c) { m_value = c; return *this; } RR& operator=(double c) { m_value = c; return *this; } RR& operator=(long double c) { assign_large_real(c); return *this; } // Access: NTL::RR& value(){ return m_value; } NTL::RR const& value()const{ return m_value; } // Member arithmetic: RR& operator+=(const RR& other) { m_value += other.value(); return *this; } RR& operator-=(const RR& other) { m_value -= other.value(); return *this; } RR& operator*=(const RR& other) { m_value *= other.value(); return *this; } RR& operator/=(const RR& other) { m_value /= other.value(); return *this; } RR operator-()const { return -m_value; } RR const& operator+()const { return *this; } // RR compatibity: const ::NTL::ZZ& mantissa() const { return m_value.mantissa(); } long exponent() const { return m_value.exponent(); } static void SetPrecision(long p) { ::NTL::RR::SetPrecision(p); } static long precision() { return ::NTL::RR::precision(); } static void SetOutputPrecision(long p) { ::NTL::RR::SetOutputPrecision(p); } static long OutputPrecision() { return ::NTL::RR::OutputPrecision(); }private: ::NTL::RR m_value; template <class V> void assign_large_real(const V& a) { using std::frexp; using std::ldexp; using std::floor; if (a == 0) { clear(m_value); return; } if (a == 1) { NTL::set(m_value); return; } if (!(boost::math::isfinite)(a)) { throw std::overflow_error("Cannot construct an instance of NTL::RR with an infinite value."); } int e; long double f, term; ::NTL::RR t; clear(m_value); f = frexp(a, &e); while(f) { // extract 30 bits from f: f = ldexp(f, 30); term = floor(f); e -= 30; conv(t.x, (int)term); t.e = e; m_value += t; f -= term; } } template <class V> void assign_large_int(V a) {#ifdef BOOST_MSVC#pragma warning(push)#pragma warning(disable:4146)#endif clear(m_value); int exp = 0; NTL::RR t; bool neg = a < V(0) ? true : false; if(neg) a = -a; while(a) { t = static_cast<double>(a & 0xffff); m_value += ldexp(RR(t), exp).value(); a >>= 16; exp += 16; } if(neg) m_value = -m_value;#ifdef BOOST_MSVC#pragma warning(pop)#endif }};// Non-member arithmetic:inline RR operator+(const RR& a, const RR& b){ RR result(a); result += b; return result;}inline RR operator-(const RR& a, const RR& b){ RR result(a); result -= b; return result;}inline RR operator*(const RR& a, const RR& b){ RR result(a); result *= b; return result;}inline RR operator/(const RR& a, const RR& b){ RR result(a); result /= b; return result;}// Comparison:inline bool operator == (const RR& a, const RR& b){ return a.value() == b.value() ? true : false; }inline bool operator != (const RR& a, const RR& b){ return a.value() != b.value() ? true : false;}inline bool operator < (const RR& a, const RR& b){ return a.value() < b.value() ? true : false; }inline bool operator <= (const RR& a, const RR& b){ return a.value() <= b.value() ? true : false; }inline bool operator > (const RR& a, const RR& b){ return a.value() > b.value() ? true : false; }inline bool operator >= (const RR& a, const RR& b){ return a.value() >= b.value() ? true : false; }#if 0// Non-member mixed compare:template <class T>inline bool operator == (const T& a, const RR& b){ return a == b.value();}template <class T>inline bool operator != (const T& a, const RR& b){ return a != b.value();}template <class T>inline bool operator < (const T& a, const RR& b){ return a < b.value();}template <class T>inline bool operator > (const T& a, const RR& b){ return a > b.value();}template <class T>inline bool operator <= (const T& a, const RR& b){ return a <= b.value();}template <class T>inline bool operator >= (const T& a, const RR& b){ return a >= b.value();}#endif // Non-member mixed compare:// Non-member functions:/*inline RR acos(RR a){ return ::NTL::acos(a.value()); }*/inline RR cos(RR a){ return ::NTL::cos(a.value()); }/*inline RR asin(RR a){ return ::NTL::asin(a.value()); }inline RR atan(RR a){ return ::NTL::atan(a.value()); }inline RR atan2(RR a, RR b){ return ::NTL::atan2(a.value(), b.value()); }*/inline RR ceil(RR a){ return ::NTL::ceil(a.value()); }/*inline RR fmod(RR a, RR b){ return ::NTL::fmod(a.value(), b.value()); }inline RR cosh(RR a){ return ::NTL::cosh(a.value()); }*/inline RR exp(RR a){ return ::NTL::exp(a.value()); }inline RR fabs(RR a){ return ::NTL::fabs(a.value()); }inline RR abs(RR a){ return ::NTL::abs(a.value()); }inline RR floor(RR a){ return ::NTL::floor(a.value()); }/*inline RR modf(RR a, RR* ipart){ ::NTL::RR ip; RR result = modf(a.value(), &ip); *ipart = ip; return result;}inline RR frexp(RR a, int* expon){ return ::NTL::frexp(a.value(), expon); }inline RR ldexp(RR a, int expon){ return ::NTL::ldexp(a.value(), expon); }*/inline RR log(RR a){ return ::NTL::log(a.value()); }inline RR log10(RR a){ return ::NTL::log10(a.value()); }/*inline RR tan(RR a){ return ::NTL::tan(a.value()); }*/inline RR pow(RR a, RR b){ return ::NTL::pow(a.value(), b.value()); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?