comparing floating point numbers for equality

137 views (last 30 days)
Assign matchFound with true if the difference between firstSample and secondSample is within thresholdValue.
And the code to call the function should be: IsMatch((4/5), 0.8, 0.0001)
I am having trouble understanding. I know I should at least have abs(firstSample-secondSample)
Can someone break it down for me?
dpb on 28 Jan 2023
"...are correct, abs(firstSample - secondSample) is a good start. Your function also returns one of two options: true or false. Try using an if statement to decipher between the two options"
This does NOT require an explicit if construct (and an optimum solution should not include one).
HINT: As @Caitlin O'Brien suggests, try some expressions at the command line and see what you get for given sets of values and the threshold. NOTA BENE: your expression so far doesn't make use of the threshold value; what part would it play in making the ultimate result? HINT SECOND: Think about how you would solve the problem with pencil and paper to write down the answer on a pop quiz...

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 28 Jan 2023
The function inside would be a simple one line statement like
something = abs(something - something) < something;
You should be able to figure it out what to replace something by. I mean lines 6 and 7 of your code practically spell it out explicitly for you. Or, like @dpb said, you can also use ismembertol in a single line of code with your function just being a simple wrapper for ismembertol.


Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by