MATLAB Smallest Integer in floating point number system

16 views (last 30 days)
Hmm!
Hmm! on 26 Jan 2021
Edited: James Tursa on 27 Jan 2021
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.

Accepted Answer

James Tursa
James Tursa on 26 Jan 2021
Edited: James Tursa on 27 Jan 2021
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 Comments
Walter Roberson
Walter Roberson on 27 Jan 2021
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

Sign in to comment.

More Answers (1)

Rik
Rik on 26 Jan 2021
Edited: Rik on 26 Jan 2021
You are indeed correct:
realmin,log2(realmin)
ans = 2.2251e-308
ans = -1022
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
ans = -1.7977e+308
  8 Comments
Steven Lord
Steven Lord on 27 Jan 2021
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
x = 2.2251e-308
y = eps(0)
y = 4.9000e-324
y < x
ans = logical
1

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!