FloatTools.h

Go to the documentation of this file.
00001 #ifndef FLOATTOOLS_H_
00002 #define FLOATTOOLS_H_
00003 
00004 /*  greebo: this contains some handy (?) functions for manipulating float variables
00005  */
00006 
00007 #include "lrint.h"
00008 
00009 #include <iostream>
00010 #include <cmath>
00011 #include <float.h>
00012 #include <algorithm>
00013 
00014 // =========================================================================================
00015 
00017 template<typename Element, typename OtherElement>
00018 inline bool float_equal_epsilon (const Element& self, const OtherElement& other, const Element& epsilon)
00019 {
00020     return fabs(other - self) < epsilon;
00021 }
00022 
00024 template<typename Element>
00025 inline Element float_mid (const Element& self, const Element& other)
00026 {
00027     return Element((self + other) * 0.5);
00028 }
00029 
00031 template<typename Element>
00032 inline int float_to_integer (const Element& f)
00033 {
00034     return lrint(f);
00035 }
00036 
00038 template<typename Element, typename OtherElement>
00039 inline Element float_snapped (const Element& f, const OtherElement& snap)
00040 {
00041     return Element(float_to_integer(f / snap) * snap);
00042 }
00043 
00045 template<typename Element>
00046 inline bool float_is_integer (const Element& f)
00047 {
00048     return f == Element(float_to_integer(f));
00049 }
00050 
00053 template<typename Element, typename ModulusElement>
00054 inline Element float_mod_range (const Element& self, const ModulusElement& modulus)
00055 {
00056     return Element((self < 0.0) ? self + modulus : self);
00057 }
00058 
00060 template<typename Element, typename ModulusElement>
00061 inline Element float_mod (const Element& self, const ModulusElement& modulus)
00062 {
00063     return float_mod_range(Element(fmod(static_cast<double> (self), static_cast<double> (modulus))), modulus);
00064 }
00065 
00066 #endif /*FLOATTOOLS_H_*/

Generated by  doxygen 1.6.2