64 bit binary number representation in matlab without rounding off
古いコメントを表示
I have a 64 bits binary number having 1's and 0's and i need this number in all 64 bits for further processing. However, i am getting this number in exponential form after rounding i.e. 1.01010101010101e+41. Can anyone please tell me how to get full 64 bits without losing precision ? Thanks in advance.
採用された回答
その他の回答 (3 件)
KALYAN ACHARJYA
2019 年 7 月 20 日
編集済み: KALYAN ACHARJYA
2019 年 7 月 20 日
num=1.01010101010101e+41;
format long g; % Put the format, its just change the display pattern
bin_num=dec2bin(num);
Result:
bin_num =
'10010100011010111100011011101001001110100101101001011000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
Please do the change as per your requirements
2 件のコメント
Note that as per its documentation, dec2bin cannot convert accurately any number greater than flintmax (because it uses double representation internally), so it's not suitable for 64-bit integer. It fails miserably for e.g.
>> dec2bin(intmax('uint64'))
ans =
'10000000000000000000000000000000000000000000000000000000000000000'
when the answer should be
ans =
'1111111111111111111111111111111111111111111111111111111111111111'
Nits
2019 年 7 月 21 日
Bruno Luong
2019 年 7 月 20 日
編集済み: Bruno Luong
2019 年 7 月 20 日
Assuming you have your binary in string of length 64 such as
b=repmat('0',1,64);
b([1 end])='1',
You can convert to uint64 integer class by
num = typecast(flip(uint32(bin2dec(reshape(b,32,2)'))),'uint64')
If you binary string has less than 64 chars you might need first to pad it with '0' in the head before casting
b = [repmat('0',1,64-length(b)) b]
2 件のコメント
Bruno Luong
2019 年 7 月 20 日
編集済み: Bruno Luong
2019 年 7 月 20 日
for num of class 'uint64' conversion to binary can be done like this
b = reshape(flip(dec2bin(typecast(num,'uint32'),32)),1,[])
This avoid the issue of inaccuracy when dec2bin applies directly on num > flintmax.
Nits
2019 年 7 月 20 日
John D'Errico
2019 年 7 月 21 日
編集済み: John D'Errico
2019 年 7 月 21 日
Maybe you are looking for something like this. I wrote a little utility recently that fully extracts the binary form of any numeric class.
num = 108.308
num =
108.308
>> B = num2bin(num)
B =
struct with fields:
Class: 'double'
Sign: 1
Exponent: 6
Mantissa: '11011000100111011011001000101101000011100101011000001'
BinaryExpansion: [6 5 3 2 -2 -5 -6 -7 -9 -10 -12 -13 -16 -20 -22 -23 -25 -30 -31 -32 -35 -37 -39 -40 -46]
BiSci: '1.1011000100111011011001000101101000011100101011000001 B6'
BiCimal: '1101100.0100111011011001000101101000011100101011000001'
>> sum(2.^B.BinaryExpansion)
ans =
108.308
>> B.Sign*sum(2.^B.BinaryExpansion) == num
ans =
logical
1
So B contains the bits that are in num, and shows the binary form of num in several ways, thus in a scientific binary form, and a binary form with a decimal point, and as a list of powers of 2, that can then be summed to recover num.
It thus allows you to easily recover the original number in num, and do so in a way that should be exact. It even understands what denormal numbers are.
I'll probably play with the names of the fields before I post it on the file exchange, but I've attached it to this answer.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!