dec2bin is not precise with 64 bits numbers

Hi, I was trying to apply a left circular shift to a 64 bits number, when I notice that dec2bin does'nt work correctly, Have you any idea how to fix it? for example:
dec2bin(double(9223372036855037954))
ans =
1000000000000000000000000000000000000000000001000000000000000000
But the right anwser that I expect is:
1000000000000000000000000000000000000000000001000000000000000010

回答 (4 件)

Thomas
Thomas 2012 年 4 月 2 日

1 投票

For dec2bin to work correctly it should be nonnegative integer smaller than 2^52
It is mentioned int he documentation: http://www.mathworks.com/help/techdoc/ref/dec2bin.html
Walter Roberson
Walter Roberson 2012 年 4 月 2 日

0 投票

double(9223372036855037954) is not a 64 bit number: it is an IEEE 754 Double Precision Floating Point number.
Please see http://www.mathworks.com/matlabcentral/answers/13142-dec2bin and in particular the last comment I made (about 64 bit constants.)
suzi freunden
suzi freunden 2012 年 4 月 2 日

0 投票

thanks for the answers, I have another question: is there a function doing a correct bitshift for unsigned integer bigger than 2^52? I'm using R2009b

1 件のコメント

Jan
Jan 2012 年 5 月 13 日
BITSHIFT works correctly with UINT64 in 2009a. Only reading the value fails.

サインインしてコメントする。

John D'Errico
John D'Errico 2012 年 4 月 2 日

0 投票

So use a tool (vpi, found on the FEX) designed to handle large numbers.
>> vpi2bin(vpi('9223372036855037954'))
ans =
1000000000000000000000000000000000000000000001000000000000000010
Or for that matter, if you like bigger numbers...
>> vpi2bin(vpi('92233720368556346354667564688567567858565037954'))
ans =
1000000100111111001110010111100011111000101000010100010011111110011000001110001001010001100001110111110110100110000010110001111110011111100101011111110000010

3 件のコメント

suzi freunden
suzi freunden 2012 年 5 月 13 日
thanks a lot for your help, but I get this error when using vpi package:
D = vpi(2)^75 - 1;
??? Undefined function or method 'vpi' for input arguments of type 'double'.
vpi2bin(vpi(9223372036855037954))
??? Undefined function or method 'vpi' for input arguments of type 'double'.
Walter Roberson
Walter Roberson 2012 年 5 月 13 日
vpi is a MATLAB File Exchange (FEX) contribution. You need to download it and install it on your machine.
suzi freunden
suzi freunden 2012 年 5 月 13 日
To use the vpi (Variable Precision Integer) arithmetic tools, you will need to have the top level directory (VariablePrecisionIntegers) on your MATLAB search path.\
I just download it and change MATLAB search path there is no .exe or something like

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2012 年 4 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by