Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Values. 1

Status
Not open for further replies.

TotalInsanity

Programmer
Oct 18, 2006
20
GB
Hello again all.

Some more beautiful advice needed please from all of you professional coders :O)

If I have three double values: 3.55, 3.47, 2.24.

I want to take the first double value of 3.55 and find which of the other two values: 3.47, 2.24 is the closest match to the first given value.

I understand this in English, lol, that 3.47 would come out but to compare the values using c++ code, how would I do that?

Any help would be vastly appreciated!

Thanks in Advance.

 
1) Say you have read in the values into x, a, b. I'll just assume you know how to do that.
2) Find the difference
Code:
diffa = abs(x - a);
diffb = abs(x - b);
3) The closer one is the one with the smaller difference
Code:
closer = diffa < diffb ? diffa: diffb;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top