00001 #ifndef FLOAT_PLUS_CLASS 00002 #define FLOAT_PLUS_CLASS 00003 00004 /*****************************************************************************\ 00005 * * 00006 * Name : floating point extension class * 00007 * Author : Chris Koeritz * 00008 * * 00009 ******************************************************************************* 00010 * Copyright (c) 1993-$now By Author. This program is free software; you can * 00011 * redistribute it and/or modify it under the terms of the GNU General Public * 00012 * License as published by the Free Software Foundation; either version 2 of * 00013 * the License or (at your option) any later version. This is online at: * 00014 * http://www.fsf.org/copyleft/gpl.html * 00015 * Please send any updates to: fred@gruntose.com * 00016 \*****************************************************************************/ 00017 00018 #include <basis/definitions.h> 00019 00021 00028 template <class contents> 00029 class float_plus 00030 { 00031 public: 00032 #define DEFAULT_DELTA 0.0001 00033 00038 00039 float_plus(contents init = 0.0, contents delta = DEFAULT_DELTA) 00040 : _value(init), _delta(delta) {} 00041 00043 float_plus(const float_plus &to_copy) 00044 : _value(to_copy._value), _delta(to_copy._delta) {} 00045 00047 float_plus &operator = (const float_plus &cp) 00048 { _value = cp._value; _delta = cp._delta; return *this; } 00049 00050 inline contents value() const { return _value; } 00052 inline operator contents () const { return _value; } 00054 00055 inline contents delta() const { return _delta; } 00057 inline void delta(contents new_delta) { _delta = new_delta; } 00059 00060 bool operator == (const float_plus &to_compare) { return d_eq(to_compare); } 00062 bool operator != (const float_plus &to_compare) { return !d_eq(to_compare); } 00064 bool operator < (const float_plus &to_compare) 00065 { return !d_eq(to_compare) && (_value < to_compare); } 00067 bool operator > (const float_plus &to_compare) 00068 { return !d_eq(to_compare) && (_value > to_compare); } 00070 bool operator <= (const float_plus &to_compare) 00071 { return _value < to_compare || d_eq(to_compare); } 00073 bool operator >= (const float_plus &to_compare) 00074 { return _value > to_compare || d_eq(to_compare); } 00076 00077 private: 00078 contents _value; 00079 contents _delta; 00080 00082 bool d_eq(contents to_compare) { 00083 contents diff = absolute_value(_value - to_compare); 00084 return diff < absolute_value(_delta); 00085 } 00086 }; 00087 00088 #endif 00089
1.5.1