Matlab is not recognizing that two floats are equal although Workspace shows they are

```
% calculate_line_lengths
line_lengths = calculate_line_lengths(plane_x,plane_y, rect_corners);
% send the above two answers to herons_formula, get 4 areas
four_areas = herons_formula(edge_lengths, line_lengths);
sum_four_areas = sum(four_areas);
if sum_four_areas == rect_area
in_rect = true;
elseif sum_four_areas < rect_area
in_rect = false;
end
```
My code falls into the elseif when it should be going into the if statement as they are equal.
Why?

6 件のコメント

Stephen23
Stephen23 2022 年 2 月 18 日
"why"
Because the values are not equal.
Question: if the values differ for example in the 10th fractional digit, would you see that difference when you simply look at the values displayed to 4 fractional digits? (hint: no, you would not).
Nobutaka Kim
Nobutaka Kim 2022 年 2 月 18 日
is there something like an np.isclose() from Python?
Walter Roberson
Walter Roberson 2022 年 2 月 18 日
ismembertol
Stephen23
Stephen23 2022 年 2 月 18 日
編集済み: Stephen23 2022 年 2 月 18 日
Where A and B are the two values you wish to compare, and tol is a suitably chosen tolerance value:
abs(A-B)<tol
@Walter Roberson: Until today, the method ismembertol has implemented the tolerance, did never match my needs in real code:
abs(u-v) <= tol*max(abs([A(:);B(:)]))
'DataScale' is not a solution also.

サインインしてコメントする。

回答 (1 件)

Prateek Rai
Prateek Rai 2022 年 2 月 24 日

0 投票

You can refer to following MATLAB Answers on Why two equal numbers are not equal? to get more idea on why MATLAB is not recognizing that two floats are equal although workspace shows that they are equal.

カテゴリ

製品

リリース

R2021b

タグ

質問済み:

2022 年 2 月 18 日

回答済み:

2022 年 2 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by