フィルターのクリア

Floor/Fix rounds the result of a division (60.000) to 59

2 ビュー (過去 30 日間)
Levin Schaefer
Levin Schaefer 2022 年 4 月 7 日
コメント済み: DGM 2022 年 4 月 7 日
Hello,
I have the problem that Matlab rounds (using floor/fix) the result of my division, 33 / 0.55, towards 59 instead of 60. The code looks like this:
s = floor(a / b)
Where 'a' and 'b' come from a GUI and can be integers or decimals (but never negativ or zero). When I set a = 33 and b = 0.55 and run the program matlab produces as a intermediate result, of a / b, 60.000 (I checked it), which get afterwards rounded by floor (or fix) to 59.
The ultimate goal of the part of the code is to divide 'a' through 'b' and give an integer back, where the integer should be rounded down towards the next integer (1.7 to 1 or 4.9 to 4 etc.).
Thanks for the help!
  1 件のコメント
DGM
DGM 2022 年 4 月 7 日
It is rounded down to the next integer.
format short
33/0.55
ans = 60.0000
format long
33/0.55
ans =
59.999999999999993
It's just a matter of floating point error and display settings.

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

回答 (1 件)

Mohammed Hamaidi
Mohammed Hamaidi 2022 年 4 月 7 日
Hi
Just use:
s=round(a/b)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by