# comparing floating point numbers for equality

113 Ansichten (letzte 30 Tage)
Alexandra am 28 Jan. 2023
Beantwortet: Image Analyst am 28 Jan. 2023
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?
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
cdawg am 28 Jan. 2023
Assuming this is homework and you can't use a built in function...
You 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 (when do you return true and when do you return false?)
You might also want to experiment in the command line or a blank script (getting errors within a function can be frustrating because it doesn't give you any output).
dpb am 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...

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Image Analyst am 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.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by