필터 지우기
필터 지우기

How to fix the digits for the summation?

조회 수: 1 (최근 30일)
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2019년 4월 10일
댓글: Star Strider 2019년 4월 10일
Dear All, I have a code that finds the percentage of each case from the total population, I have 10 cases and the summation of their percentages must be 100% or 0 for example, but the problem is that I am still getting a summation of 100.01 or 99.99 or 99.98, what is the reason here and how can I fix it please, Notice: I rounded the results into 2 digits before I calculate the summation.
  댓글 수: 2
Adam
Adam 2019년 4월 10일
It is not possible to store every floating point number precisely so there are errors that accumulate when they are summed (or other operations) which is why you will not get exactly 100 as your answer.
Read up about floating point arithmetic to understand further.
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2019년 4월 10일
Thank you for your help, I will try it now

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2019년 4월 10일
See the documentation section on Floating-Point Numbers (link). You are most likely seeing the result of floating-point approximation error. Also, rounding the numbers first loses precision, and likely results in the inaccuracies you get. The solution is not to round the intermediate values, only the results, and the only if necessary.
  댓글 수: 4
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2019년 4월 10일
Yes I got it now, Thank you for your help
Star Strider
Star Strider 2019년 4월 10일
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by