Wm4TRational.h

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.0 (2006/06/28)
00016 
00017 #ifndef WM4TRATIONAL_H
00018 #define WM4TRATIONAL_H
00019 
00020 #include "Wm4FoundationLIB.h"
00021 #include "Wm4TInteger.h"
00022 
00023 namespace Wm4
00024 {
00025 
00026 // N is the number of 32-bit words per TInteger numerator/denominator
00027 template <int N>
00028 class TRational
00029 {
00030 public:
00031     // construction
00032     TRational ();  // default rational is 0/1
00033     TRational (const TRational& rkR);
00034     TRational (const TInteger<N>& rkNumer);
00035     TRational (const TInteger<N>& rkNumer, const TInteger<N>& rkDenom);
00036 
00037     // construction converters
00038     TRational (int iNumer);
00039     TRational (int iNumer, int iDenom);
00040     TRational (float fValue);
00041     TRational (double dValue);
00042 
00043     // member access
00044     TInteger<N>& Numer ();
00045     TInteger<N>& Denom ();
00046     const TInteger<N>& Numer () const;
00047     const TInteger<N>& Denom () const;
00048 
00049     // assignment
00050     TRational& operator= (const TRational& rkR);
00051 
00052     // comparison
00053     bool operator== (const TRational& rkR) const;
00054     bool operator!= (const TRational& rkR) const;
00055     bool operator<= (const TRational& rkR) const;
00056     bool operator<  (const TRational& rkR) const;
00057     bool operator>= (const TRational& rkR) const;
00058     bool operator>  (const TRational& rkR) const;
00059 
00060     // arithmetic operations
00061     TRational operator+ (const TRational& rkR) const;
00062     TRational operator- (const TRational& rkR) const;
00063     TRational operator* (const TRational& rkR) const;
00064     TRational operator/ (const TRational& rkR) const;
00065     TRational operator- () const;
00066 
00067     // arithmetic updates
00068     TRational& operator+= (const TRational& rkR);
00069     TRational& operator-= (const TRational& rkR);
00070     TRational& operator*= (const TRational& rkR);
00071     TRational& operator/= (const TRational& rkR);
00072 
00073     // conversions to float and double
00074     void ConvertTo (float& rfValue) const;
00075     void ConvertTo (double& rdValue) const;
00076 
00077     // compute the absolute value of the rational number
00078     TRational Abs () const;
00079 
00080 private:
00081     // miscellaneous utilities
00082     void EliminatePowersOfTwo ();
00083 
00084     static void GetPositiveFloat (const TInteger<N>& rkDenom,
00085         TInteger<N>& rkQuo, TInteger<N>& rkRem, int iBlock,
00086         unsigned int& ruiExponent, unsigned int& ruiMantissa);
00087 
00088     static void GetPositiveDouble (const TInteger<N>& rkDenom,
00089         TInteger<N>& rkQuo, TInteger<N>& rkRem, int iBlock,
00090         unsigned int& ruiExponent, unsigned int& ruiMantissaHi,
00091         unsigned int& ruiMantissaLo);
00092 
00093     TInteger<N> m_kNumer, m_kDenom;
00094 };
00095 
00096 template <int N>
00097 TRational<N> operator+ (const TInteger<N>& rkI, const TRational<N>& rkR);
00098 
00099 template <int N>
00100 TRational<N> operator- (const TInteger<N>& rkI, const TRational<N>& rkR);
00101 
00102 template <int N>
00103 TRational<N> operator* (const TInteger<N>& rkI, const TRational<N>& rkR);
00104 
00105 template <int N>
00106 TRational<N> operator/ (const TInteger<N>& rkI, const TRational<N>& rkR);
00107 
00108 }
00109 
00110 #include "Wm4TRational.inl"
00111 
00112 #endif

Generated on Wed Nov 23 19:01:09 2011 for FreeCAD by  doxygen 1.6.1