MATLAB Smallest Integer in floating point number system
조회 수: 19 (최근 30일)
이전 댓글 표시
I am interested in finding the smallest integer in MATLAB, say y, that is not representable in 64 bit floating point format so that the floating point number is not equal to y i.e., fl(y) is not y.
My insticts tells me that could be the underflow level given as 2^-1022 and its corresponding floating point number is 1.00...2^{-1022}
I don't whether this is correct. Help me find the number please.
댓글 수: 0
채택된 답변
James Tursa
2021년 1월 26일
편집: James Tursa
2021년 1월 27일
IEEE double has a 52-bit mantissa, plus there is a hidden bit, so effectively 53-bit mantissa. So you can exactly represent all integers up to 53 binary bits in IEEE double exactly. So 53 1's would be the largest integer with all 1's. Add one to that and you get a 54-bit integer that is a 1 followed by 53 0's, which because of the trailing 0's is also exactly representable in IEEE double (i.e., the last 0 is not representable, but because it is a 0 it doesn't affect the value). Add one more to that and you get a 54-bit integer that is a 1 followed by 52 0's followed by a 1. This is not representable exactly in IEEE double because of the 53-bit mantissa limitation. So the smallest integer number that cannot be represented exactly in IEEE double is the 54-bit binary integer 100...001, which is decimal
2^53+1
E.g.,
>> num2strexact(2^53,'fixed')
ans =
'9007199254740992'
>> num2strexact(2^53+1,'fixed')
ans =
'9007199254740992' <-- IEEE Double can't handle that extra 1
>> uint64(2^53)
ans =
uint64
9007199254740992
>> uint64(2^53)+1
ans =
uint64
9007199254740993 <-- uint64 has full 64 bits available for value
>> dec2bin(2^53)
ans =
'100000000000000000000000000000000000000000000000000000'
>> dec2bin(uint64(2^53)+1)
ans =
'100000000000000000000000000000000000000000000000000001'
Another clue is to use eps( ) to find the value of the least significant bit:
>> eps(2^53)
ans =
2
Since this value is greater than 1, that tells you that we are beyond the precision of IEEE Double to handle neighboring integers exactly.
댓글 수: 4
Walter Roberson
2021년 1월 27일
Your question is about how to find the integer. flintmax() is specifically designed as returning exactly that integer. So the answer is to call flintmax()
As to why that particular integer: see Jame's explanation. I might perhaps have written things slightly differently, but same conclusion.
If this is a homework question, then they are expecting that you will keep taking powers of 2 until you find one where 2^x + 1 - 2^x == 0
Is there any better way than dec2bin to compute the binary of 1+2^53?
"better" is not well defined, especially since 1+2^53 and dec2bin() of that character vector will not in fact return the number, since both of them operate in double precision and the point of the exercise is that the number you seek is not representable in double precision. You need to work in int64 or uint64
추가 답변 (1개)
Rik
2021년 1월 26일
편집: Rik
2021년 1월 26일
You are indeed correct:
realmin,log2(realmin)
Unless you actually mean integer, in which case the smallest integer is 0.
If you mean the lowest number of an IEEE double: that would be
-realmax
댓글 수: 8
Steven Lord
2021년 1월 27일
Anything between 0 and realmin can't be written as a double.
Incorrect. realmin is the smallest normalized positive value, but there are smaller subnormal positive values.
x = realmin
y = eps(0)
y < x
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!