[SOLVED] c++ comparison with tolerance

Status
Not open for further replies.

bmandl

Full Member level 4
Joined
Feb 26, 2010
Messages
210
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Slovenia
bmandl.edaboard.eu
Activity points
3,006
Hello,
I was wondering if there is some way in c++, to compare one integer value to another, but allow tolerance? Something like this:

if (val1 > (val2±x)){...}

else if (val1<(val2±x)){...}

Thank you for your answers!

- - - Updated - - -

Ok, I solved the problem this way:

if (val1 > val2 && abs(val1-val2)>x){...}

else if (val1 < val2 && abs(val1-val2)>x){...}
 

Looks like a method to generate dummy code.

The actually performed comparison is val1 > val2+x respectively val1 < val2 - x.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…