Howard wrote:
"Ben Pope" <be***************@gmail.com> wrote in message
news:43**********************@taz.nntpserver.com.. . ke*********@gmail.com wrote: Thanks. Can you please tell me how should I compare float instead?
<in Father Jacks voice>
feq!
What's "feq" ??? (And who's Father Jack?)
Hmm, I thought feq was for floating point equals. It's not, my
subconscious made that up.
Father Jack is one of the characters from Father Ted, a UK Sitcom, he
shouts "Feck", "Arse", "Drink" and "Girls", whilst drunk (which is all
the time).
Anyway, apologies for the tangent, it's all based on a fictitious
function called feq.
Apologies, I'll create one to redeem myself!
#include <iostream>
#include <cmath>
#include <limits>
template<class T>
bool feq(T val1, T val2) {
return (fabs(val1 - val2) <= std::numeric_limits<T>::epsilon());
}
int main() {
float float1 = 1.2;
float float2 = 1.200000001;
double double1 = 1.2;
double double2 = 1.200000001;
std::cout << (feq(float1, float2) ? "equal\n" : "not equal\n");
std::cout << (feq(double1, double2) ? "equal\n" : "not equal\n");
return 0;
}
/me runs for cover
Ben Pope
--
I'm not just a number. To many, I'm known as a string...