Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Rational.hpp

Go to the documentation of this file.
00001 /*
00002     libglim: a C++ Free library render images using OpenGL
00003     Copyright (C) 2002 Nestal Wan
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU General Public License
00007     as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00026 #ifndef __PICINFO_RATIONAL_HEADER_INCLUDED__
00027 #define __PICINFO_RATIONAL_HEADER_INCLUDED__
00028 
00029 #include <ostream>
00030 
00031 namespace picinfo {
00032 
00038 template <typename T>
00039 struct Rational
00040 {
00041     T   numerator ;
00042     T   dominator ;
00043     
00044     operator double( ) const
00045     {
00046         return static_cast<double>( numerator ) /
00047                static_cast<double>( dominator ) ;
00048     }
00049     
00050     operator uint32_t( ) const
00051     {
00052         return numerator / dominator ;
00053     }
00054 
00055     operator int32_t( ) const
00056     {
00057         return numerator / dominator ;
00058     }
00059 
00060     operator uint16_t( ) const
00061     {
00062         return static_cast<uint16_t>(numerator / dominator) ;
00063     }
00064 
00065     operator int16_t( ) const
00066     {
00067         return static_cast<int16_t>(numerator / dominator) ;
00068     }
00069 
00070     operator int8_t( ) const
00071     {
00072         return static_cast<uint8_t>( numerator / dominator ) ;
00073     }
00074 
00075     operator uint8_t( ) const
00076     {
00077         return static_cast<uint8_t>( numerator / dominator ) ;
00078     }
00079     
00080     Rational( T num = 0, T dom = 1 ) : numerator( num ), dominator( dom )
00081     {
00082     }
00083 } ;
00084 
00085 template <typename T>
00086 std::ostream& operator<<( std::ostream& os, const Rational<T>& rat )
00087 {
00088     return os << rat.numerator << '/' << rat.dominator ;
00089 }
00090 
00091 } // end of namespace
00092 
00093 #endif

Generated on Fri Jan 13 23:38:12 2006 for picinfo by  doxygen 1.4.4