floating_point_comparison.h
Go to the documentation of this file.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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_TOLERANCE_COMPARISON_H
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_TOLERANCE_COMPARISON_H
00047
00048 #include "num_common.h"
00049 #include "basic_ops.h"
00050
00051 namespace lass
00052 {
00053 namespace num
00054 {
00055
00056 template <typename T>
00057 bool almostEqual(T iA, T iB, T iRelativeTolerance)
00058 {
00059 return num::abs(iA - iB) < iRelativeTolerance * std::max(num::abs(iA), num::abs(iB));
00060 }
00061
00062 template <typename T>
00063 bool notAlmostEqual(T iA, T iB, T iRelativeTolerance)
00064 {
00065 return !almostEqual(iA, iB, iRelativeTolerance);
00066 }
00067
00068 template <typename T>
00069 bool almostLess(T iA, T iB, T iRelativeTolerance)
00070 {
00071 return iA < iB && !almostEqual(iA, iB, iRelativeTolerance);
00072 }
00073
00074 template <typename T>
00075 bool almostGreater(T iA, T iB, T iRelativeTolerance)
00076 {
00077 return iA > iB && !almostEqual(iA, iB, iRelativeTolerance);
00078 }
00079
00080 template <typename T>
00081 bool almostLessOrEqual(T iA, T iB, T iRelativeTolerance)
00082 {
00083 return iA <= iB || almostEqual(iA, iB, iRelativeTolerance);
00084 }
00085
00086 template <typename T>
00087 bool almostGreaterOrEqual(T iA, T iB, T iRelativeTolerance)
00088 {
00089 return iA >= iB || almostEqual(iA, iB, iRelativeTolerance);
00090 }
00091
00092 template <typename T>
00093 bool almostInOpenRange(T iA, T iMin, T iMax, T iRelativeTolerance)
00094 {
00095 return almostGreater(iA, iMin, iRelativeTolerance) &&
00096 almostLess(iA, iMax, iRelativeTolerance);
00097 }
00098
00099 template <typename T>
00100 bool almostInClosedRange(T iA, T iMin, T iMax, T iRelativeTolerance)
00101 {
00102 return almostGreaterOrEqual(iA, iMin, iRelativeTolerance) &&
00103 almostLessOrEqual(iA, iMax, iRelativeTolerance);
00104 }
00105
00106 }
00107
00108 }
00109
00110 #endif