📄 bigfloat.h
字号:
/**************************************************************************** * Core Library Version 1.7, August 2004 * Copyright (c) 1995-2004 Exact Computation Project * All rights reserved. * * This file is part of CORE (http://cs.nyu.edu/exact/core/); you may * redistribute it under the terms of the Q Public License version 1.0. * See the file LICENSE.QPL distributed with CORE. * * Licensees holding a valid commercial license may use this file in * accordance with the commercial license agreement provided with the * software. * * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * * File: BigFloat.h * Synopsis: * An implementation of BigFloat numbers with error bounds. * * Written by * Chee Yap <yap@cs.nyu.edu> * Chen Li <chenli@cs.nyu.edu> * Zilin Du <zilin@cs.nyu.edu> * * WWW URL: http://cs.nyu.edu/exact/ * Email: exact@cs.nyu.edu * * $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.3-branch/Core/include/CGAL/CORE/BigFloat.h $ * $Id: BigFloat.h 37563 2007-03-27 13:28:17Z afabri $ ***************************************************************************/#ifndef _CORE_BIGFLOAT_H_#define _CORE_BIGFLOAT_H_#include <CGAL/CORE/BigFloatRep.h>CORE_BEGIN_NAMESPACEclass Expr;/// \class BigFloat BigFloat.h/// \brief BigFloat is a class of Float-Point number with error bounds.typedef RCImpl<BigFloatRep> RCBigFloat;class BigFloat : public RCBigFloat {public: /// \name Constructors and Destructor //@{ /// default constructor BigFloat() : RCBigFloat(new BigFloatRep()) {} /// constructor for <tt>short</tt> BigFloat(short i) : RCBigFloat(new BigFloatRep(i)) {} /// constructor for <tt>float</tt> BigFloat(float i) : RCBigFloat(new BigFloatRep(i)) {} /// constructor for <tt>int</tt> BigFloat(int i) : RCBigFloat(new BigFloatRep(i)) {} /// constructor for <tt>long</tt> BigFloat(long l) : RCBigFloat(new BigFloatRep(l)) {} /// constructor for <tt>double</tt> BigFloat(double d) : RCBigFloat(new BigFloatRep(d)) {} /// constructor for <tt>const char* </tt>(default base = 10) BigFloat(const char* s) : RCBigFloat(new BigFloatRep(s)) {} /// constructor for <tt>std::string</tt>(default base = 10) BigFloat(const std::string& s) : RCBigFloat(new BigFloatRep(s)) {} /// constructor for <tt>int</tt> and <tt>long</tt> // This is a hack because in Sturm, we need to approximate any // coefficient type NT to a BigFloat, and it would complain if we // do not have this method explicitly: BigFloat(int& i, const extLong& /*r*/, const extLong& /*a*/) : RCBigFloat(new BigFloatRep(i)) {} BigFloat(long& x, const extLong& /*r*/, const extLong& /*a*/) : RCBigFloat(new BigFloatRep(x)) {} /// constructor from <tt>BigInt</tt>, error and exponent values BigFloat(const BigInt& I, unsigned long er, long ex) : RCBigFloat(new BigFloatRep(I, er, ex)) {} /// constructor from <tt>BigInt</tt>, exponent values BigFloat(const BigInt& I, long ex) : RCBigFloat(new BigFloatRep(I, ex)) {} BigFloat(const BigInt& I) : RCBigFloat(new BigFloatRep(I)) {} /// constructor for <tt>BigRat</tt> BigFloat(const BigRat& R, const extLong& r = defRelPrec, const extLong& a = defAbsPrec) : RCBigFloat(new BigFloatRep()) { rep->approx(R, r, a); } // REMARK: it is somewhat against our principles to have BigFloat // know about Expr, but BigFloat has a special role in our system! // =============================== /// constructor for <tt>Expr</tt> explicit BigFloat(const Expr& E, const extLong& r = defRelPrec, const extLong& a = defAbsPrec); //Dummy explicit BigFloat(const BigFloat& E, const extLong& , const extLong&): RCBigFloat(E) { rep->incRef(); } /// constructor for <tt>BigFloatRep</tt> explicit BigFloat(BigFloatRep* r) : RCBigFloat(new BigFloatRep()) { rep = r; } //@} /// \name Copy-Assignment-Destructor //@{ /// copy constructor BigFloat(const BigFloat& rhs) : RCBigFloat(rhs) { rep->incRef(); } /// assignment operator BigFloat& operator=(const BigFloat& rhs) { if (this != &rhs) { rep->decRef(); rep = rhs.rep; rep->incRef(); } return *this; } /// destructor ~BigFloat() { rep->decRef(); } //@} /// \name Compound Assignment Operators //@{ /// operator+= BigFloat& operator+= (const BigFloat& x) { BigFloat z; z.rep->add(*rep, *x.rep); *this = z; return *this; } /// operator-= BigFloat& operator-= (const BigFloat& x) { BigFloat z; z.rep->sub(*rep, *x.rep); *this = z; return *this; } /// operator*= BigFloat& operator*= (const BigFloat& x) { BigFloat z; z.rep->mul(*rep, *x.rep); *this = z; return *this; } /// operator/= BigFloat& operator/= (const BigFloat& x) { BigFloat z; z.rep->div(*rep, *x.rep, defBFdivRelPrec); *this = z; return *this; } //@} /// \name Unary Minus Operator //@{ /// unary plus BigFloat operator+() const { return BigFloat(*this); } /// unary minus BigFloat operator-() const { return BigFloat(-rep->m, rep->err, rep->exp); } //@} /// \name String Conversion Functions //@{ /// set value from <tt>const char*</tt> (base = 10) void fromString(const char* s, const extLong& p=defBigFloatInputDigits) { rep->fromString(s, p); } /// convert to <tt>std::string</tt> (base = 10) std::string toString(long prec=defBigFloatOutputDigits, bool sci=false) const { return rep->toString(prec, sci); } std::string str() const { return toString(); } //@} /// \name Conversion Functions //@{ /// return int value int intValue() const { return static_cast<int>(rep->toLong()); } /// return long value long longValue() const { long l = rep->toLong(); if ((l == LONG_MAX) || (l == LONG_MIN)) return l; // return the overflown value. if ((sign() < 0) && (cmp(BigFloat(l)) != 0)) { // a negative value not exactly rounded. l--; // rounded to floor. } return l; } /// return float value float floatValue() const { return static_cast<float>(rep->toDouble()); } /// return double value double doubleValue() const { return rep->toDouble(); } /// return BigInt value BigInt BigIntValue() const { return rep->toBigInt(); } /// return BigRat value BigRat BigRatValue() const { return rep->BigRatize(); } //@} /// \name Helper Functions //@{ /// Has Exact Division static bool hasExactDivision() { return false; } //CONSTANTS /// return BigFloat(0) static const BigFloat& getZero(); /// return BigFloat(1) static const BigFloat& getOne(); /// sign function /** \note This is only the sign of the mantissa, it can be taken to be the sign of the BigFloat only if !(isZeroIn()). */ int sign() const { assert((err() == 0 && m() == 0) || !(isZeroIn())); return rep->signM(); } /// check whether contains zero /** \return true if contains zero, otherwise false */ bool isZeroIn() const { return rep->isZeroIn(); } /// absolute value function BigFloat abs() const { return (sign()>0) ? +(*this) : -(*this); } /// comparison function int cmp(const BigFloat& x) const { return rep->compareMExp(*x.rep); } /// get mantissa const BigInt& m() const { return rep->m; } /// get error bits unsigned long err() const { return rep->err; } /// get exponent long exp() const { return rep->exp; } /// check whether err == 0 /** \return true if err == 0, otherwise false */ bool isExact() const { return rep->err == 0; } /// set err to 0 /** \return an exact BigFloat, see Tutorial for why this is useful! */ BigFloat& makeExact() { makeCopy(); rep->err =0; return *this; } /// set err to 0, but first add err to the mantissa (m) /** \return the ceiling exact BigFloat, variant of makeExact */ BigFloat& makeCeilExact() { makeCopy(); rep->m += rep->err; rep->err =0; return *this; } /// set err to 0, but subtract err from the mantissa (m) /** \return the floor exact BigFloat, variant of makeExact */ BigFloat& makeFloorExact() { makeCopy(); rep->m -= rep->err; rep->err =0; return *this; } /// set err to 1 /** \return an inexact BigFloat, see Tutorial for why this is useful! */ BigFloat& makeInexact() { makeCopy(); rep->err =1; return *this; } /// return lower bound of Most Significant Bit extLong lMSB() const { return rep->lMSB(); } /// return upper bound of Most Significant Bit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -