Maximum decimal and binary values

11 Ansichten (letzte 30 Tage)
Mahdiyar
Mahdiyar am 22 Mai 2013
Hi
I use Matlab R2011b. I need to work with huge number, say greater than 10^19. I understood that Matlab cannot process the values greater than 2^53. For example, the decimal to binary conversion of A=(2^54) - 1 is not calculated properly. In order to understand what I am saying, you can run the following code and then check the value of B. B should be a matrix whose size is 1*54 and all the values should be one.
A = (2^54) - 1; B = de2bi(A, 'left-msb');
I want to know that whether there is any other command for decimal to binary conversion or not?
Regrds

Antworten (3)

David Sanchez
David Sanchez am 22 Mai 2013
de2bi is the only Matlab built-in function to perform the operation. If you need something different you have to code it yourself. Sorry.

Iain
Iain am 22 Mai 2013
The maximum integer value matlab can innately handle is 2^64 - 1 (uint64) (approx 8*10^18)
The maximum number matlab can handle is approx 10^307, but its precision is limited to about 15 digits.
You can convert a value to a different datatype using "typecast".
What are you actually trying to achieve?

José-Luis
José-Luis am 22 Mai 2013
This function should work
function [binStr sign biasExp frac] = my_fun(h)
ieee74 = '';
h = num2hex(h);
for ii = h
switch ii
case {'0'}
b = '0000';
case {'1'}
b = '0001';
case {'2'}
b = '0010';
case {'3'}
b = '0011';
case {'4'}
b = '0100';
case {'5'}
b = '0101';
case {'6'}
b = '0110';
case {'7'}
b = '0111';
case {'8'}
b = '1000';
case {'9'}
b = '1001';
case {'A', 'a'}
b = '1010';
case {'B', 'b'}
b = '1011';
case {'C', 'c'}
b = '1100';
case {'D', 'd'}
b = '1101';
case {'E', 'e'}
b = '1110';
case {'F', 'f'}
b = '1111';
end
ieee74 = [ieee74 b];
end
sign = ieee74(1);
biasExp = ieee74(2:12);
frac = ieee74(13:end);
expVal = bin2dec(biasExp) - 1023
binStr = repmat('0',1,1078);
binStr (538) = '1';
binStr (539:539+51) = frac;
if (expVal ~= 0)
binStr = circshift(binStr,[0 -expVal]);
end
binStr = insert_dot(binStr);
function [newStr] = insert_dot(oldStr)
newStr = [oldStr(1:538) '.' oldStr(539:end)];
%trimming left
idx_left = find(newStr == '1', 1, 'first');
if idx_left > 539
idx_left = 538;
end
idx_right = find(newStr == '1', 1, 'last');
if idx_right < 539
idx_right = 538;
end
newStr = newStr(idx_left:idx_right);
if sign == '1'
newStr = ['-' newStr];
end
end
end
For example:
your_string = my_fun(10^100);
  2 Kommentare
Mahdiyar
Mahdiyar am 22 Mai 2013
Thank you for your reply.
But it is not working for A=(2^64) - 1; in this case the output should be a vector with 64 elements whose values are equal to 1. For example, if you put 3 instead of 64 you have to get 1 1 1
José-Luis
José-Luis am 22 Mai 2013
Bearbeitet: José-Luis am 22 Mai 2013
I should have said that the function works for doubles. In that case the maximum precision is limited by the width of the fractional field, that is 52 bits, according to the IEEE convention. In the documentation for dec2bin, it says that the largest integer that can be exactly represented is 2^52. 2^64 is beyond that range.
If you do my_fun(7), you indeed get 111. If you want an exact representation of very large numbers, or with a large number of significant numbers (>15) then the limits imposed by the double precision become an hindrance, as you cannot store them exactly.
In that case you should use variable precision arithmetic. It is freely available in the file exchange:
Once it is installed and in the path:
>> d = vpi(2)^63-1
d =
9223372036854775807
>> vpi2bin(d)
ans =
111111111111111111111111111111111111111111111111111111111111111

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by