Precision issues when checking for a whole number

조회 수: 16 (최근 30일)
Hugo  Braz
Hugo Braz 2016년 3월 5일
댓글: Hugo Braz 2016년 3월 5일
Good evening to all,
Part of my homework is checking if the number entered by the user is a whole number. I've managed to do that already, but my question is related to precision.
What I thought on using:
if (round(num) ~= num) OR
if (mod(num, 1) == num)
end
Both options worked well when I entered a number with 15 or less decimal places. Is there any way for them to work when entering a number with more than 15 decimal places?
Thank you in advance!
  댓글 수: 2
Stephen23
Stephen23 2016년 3월 5일
MATLAB uses IEEE standard floating-point numbers, which have a specified and known precision. How do you expect to get more precision out of them than they can actually store?
Hugo  Braz
Hugo Braz 2016년 3월 5일
Thank you all! I've started a MatLab course this semester and I didn't really pay much attention in these details.

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

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 5일
No. By the time you reach +1 or -1 as you go further away from 0, the distance between representable numbers has reached 2.22044604925031e-16 . The only whole number that can have 16 digits of precision is 0 itself.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 3월 5일
편집: Roger Stafford 2016년 3월 5일
Actually x>=0 and either round(x)==x, floor(x)==x, or ceil(x)==x are always valid tests for whether x is a whole number as stored in matlab's memory, regardless of how many decimals may have been used to enter x into the memory. However inputing a number into the memory may require a conversion from decimal to binary and can very well change a number from a non-integer to an integer. It is also quite possible to have a matlab whole number which requires more than 16 decimal digits to express exactly, such as 1152921504606846976. Hence, the question of precision only applies to the entry process, not to the above tests.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by