00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027 #ifndef SHFRACTION_HPP
00028 #define SHFRACTION_HPP
00029
00030 #include <limits>
00031 #include "ShUtility.hpp"
00032
00033 namespace SH {
00034
00038 template<typename T> struct ShFractionLongType { typedef T type; };
00039
00040
00041
00042
00043
00044 template<> struct ShFractionLongType<int> { typedef long long type; };
00045 template<> struct ShFractionLongType<short> { typedef int type; };
00046 template<> struct ShFractionLongType<char> { typedef short type; };
00047 template<> struct ShFractionLongType<unsigned int> { typedef unsigned long long type; };
00048 template<> struct ShFractionLongType<unsigned short> { typedef unsigned int type; };
00049 template<> struct ShFractionLongType<unsigned char> { typedef unsigned short type; };
00050
00051 template<typename T> struct ShFractionSignedLongType { typedef T type; };
00052
00053
00054
00055 template<> struct ShFractionSignedLongType<int> { typedef long long type; };
00056 template<> struct ShFractionSignedLongType<short> { typedef int type; };
00057 template<> struct ShFractionSignedLongType<char> { typedef short type; };
00058 template<> struct ShFractionSignedLongType<unsigned int> { typedef long long type; };
00059 template<> struct ShFractionSignedLongType<unsigned short> { typedef int type; };
00060 template<> struct ShFractionSignedLongType<unsigned char> { typedef short type; };
00061
00062
00063
00064
00065
00066
00067
00068
00085 template<typename T >
00086 struct ShFraction {
00087
00088
00089
00090 typedef typename ShFractionLongType<T>::type LongType;
00091 typedef typename ShFractionSignedLongType<T>::type SignedLongType;
00092
00093
00094
00095 typedef LongType CompType;
00096
00097
00098
00099 static const bool is_signed = std::numeric_limits<T>::is_signed;
00100 static const int BITS = sizeof(T) * 8;
00101 static const T ONE;
00102 static const T MAX;
00103 static const T MIN;
00104
00105 T m_val;
00106
00108 ShFraction();
00109
00111 ShFraction(double value);
00112
00114 static ShFraction make_fraction(CompType value);
00115
00117 static ShFraction make_fraction_signed(SignedLongType value);
00118
00119 template<typename T2>
00120 ShFraction(const ShFraction<T2> &other);
00121
00123 operator double() const;
00124 T& val();
00125 T val() const;
00126
00127
00129 ShFraction& operator=(double value);
00130 ShFraction& operator=(const ShFraction& other);
00131 ShFraction& operator+=(double value);
00132 ShFraction& operator+=(const ShFraction& other);
00133 ShFraction& operator-=(double value);
00134 ShFraction& operator-=(const ShFraction& other);
00135 ShFraction& operator*=(double value);
00136 ShFraction& operator*=(const ShFraction& other);
00137 ShFraction& operator/=(double value);
00138 ShFraction& operator/=(const ShFraction& other);
00139
00142 ShFraction& operator%=(double value);
00143 ShFraction& operator%=(const ShFraction& other);
00144
00145
00149 ShFraction operator-() const;
00150
00152 template<typename TT>
00153 friend std::ostream& operator<<(std::ostream& out, const ShFraction<TT> &value);
00154
00155
00157 template<typename TT>
00158 friend std::istream& operator>>(std::istream& out, ShFraction<TT> &value);
00159
00160 private:
00161
00162 double get_double() const;
00163
00164
00165 static T clamp_val(double value);
00166
00167
00168
00169 static T clamp_val(CompType temp);
00170
00171
00172 static T clamp_val_signed(SignedLongType temp);
00173 };
00174
00175 template<typename T>
00176 const T ShFraction<T>::ONE = std::numeric_limits<T>::max();
00177
00178 template<typename T>
00179 const T ShFraction<T>::MAX = ShFraction<T>::ONE;
00180
00181 template<typename T>
00182 const T ShFraction<T>::MIN = is_signed ? -ShFraction<T>::ONE : 0;
00183
00185 template<typename T>
00186 ShFraction<T> operator+(const ShFraction<T> &a, const ShFraction<T> &b);
00187
00188 template<typename T>
00189 ShFraction<T> operator-(const ShFraction<T> &a, const ShFraction<T> &b);
00190
00191 template<typename T>
00192 ShFraction<T> operator*(const ShFraction<T> &a, const ShFraction<T> &b);
00193
00194 template<typename T>
00195 ShFraction<T> operator/(const ShFraction<T> &a, const ShFraction<T> &b);
00196
00197 template<typename T>
00198 ShFraction<T> operator%(const ShFraction<T> &a, const ShFraction<T> &b);
00199
00200 template<typename T>
00201 ShFraction<T> cbrt(const ShFraction<T> &a);
00202
00203 template<typename T>
00204 ShFraction<T> exp(const ShFraction<T> &a);
00205
00206 template<typename T>
00207 ShFraction<T> exp2(const ShFraction<T> &a);
00208
00209 template<typename T>
00210 ShFraction<T> exp10(const ShFraction<T> &a);
00211
00212 template<typename T>
00213 ShFraction<T> log(const ShFraction<T> &a);
00214
00215 template<typename T>
00216 ShFraction<T> log2(const ShFraction<T> &a);
00217
00218 template<typename T>
00219 ShFraction<T> log10(const ShFraction<T> &a);
00220
00221 template<typename T>
00222 ShFraction<T> frac(const ShFraction<T> &a);
00223
00224 template<typename T>
00225 ShFraction<T> fmod(const ShFraction<T> &a, const ShFraction<T> &b);
00226
00227 template<typename T>
00228 ShFraction<T> pow(const ShFraction<T> &a, const ShFraction<T> &b);
00229
00230 template<typename T>
00231 ShFraction<T> rcp(const ShFraction<T> &a);
00232
00233 template<typename T>
00234 ShFraction<T> rsq(const ShFraction<T> &a);
00235
00236 template<typename T>
00237 ShFraction<T> sgn(const ShFraction<T> &a);
00238
00239 template<typename T>
00240 ShFraction<T> sqrt(const ShFraction<T> &a);
00241
00243 template<typename T>
00244 ShFraction<T> acos(const ShFraction<T> &a);
00245
00246 template<typename T>
00247 ShFraction<T> asin(const ShFraction<T> &a);
00248
00249 template<typename T>
00250 ShFraction<T> atan(const ShFraction<T> &a);
00251
00252 template<typename T>
00253 ShFraction<T> atan2(const ShFraction<T> &a, const ShFraction<T> &b);
00254
00255 template<typename T>
00256 ShFraction<T> cos(const ShFraction<T> &a);
00257 template<typename T>
00258 ShFraction<T> sin(const ShFraction<T> &a);
00259
00260 template<typename T>
00261 ShFraction<T> tan(const ShFraction<T> &a);
00262
00264 template<typename T>
00265 bool operator<(const ShFraction<T> &a, const ShFraction<T> &b);
00266
00267 template<typename T>
00268 bool operator<=(const ShFraction<T> &a, const ShFraction<T> &b);
00269
00270 template<typename T>
00271 bool operator>(const ShFraction<T> &a, const ShFraction<T> &b);
00272
00273 template<typename T>
00274 bool operator>=(const ShFraction<T> &a, const ShFraction<T> &b);
00275
00276 template<typename T>
00277 bool operator==(const ShFraction<T> &a, const ShFraction<T> &b);
00278
00279 template<typename T>
00280 bool operator!=(const ShFraction<T> &a, const ShFraction<T> &b);
00281
00283 template<typename T>
00284 ShFraction<T> min(const ShFraction<T> &a, const ShFraction<T> &b);
00285
00286 template<typename T>
00287 ShFraction<T> max(const ShFraction<T> &a, const ShFraction<T> &b);
00288
00289 template<typename T>
00290 ShFraction<T> floor(const ShFraction<T> &a);
00291
00292 template<typename T>
00293 ShFraction<T> ceil(const ShFraction<T> &a);
00294
00295 template<typename T>
00296 ShFraction<T> rnd(const ShFraction<T> &a);
00297
00298 template<typename T>
00299 ShFraction<T> abs(const ShFraction<T> &a);
00300
00302 template<typename T>
00303 ShFraction<T> cond(const ShFraction<T> &a, const ShFraction<T> &b, const ShFraction<T> &c);
00304
00305 template<typename T>
00306 ShFraction<T> lerp(const ShFraction<T> &a, const ShFraction<T> &b, const ShFraction<T> &c);
00307
00308 typedef ShFraction<int> ShFracInt;
00309 typedef ShFraction<short> ShFracShort;
00310 typedef ShFraction<char> ShFracByte;
00311
00312 typedef ShFraction<unsigned int> ShFracUInt;
00313 typedef ShFraction<unsigned short> ShFracUShort;
00314 typedef ShFraction<unsigned char> ShFracUByte;
00315
00316 }
00317
00318
00319 #include "ShFractionImpl.hpp"
00320
00321 #endif