Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help: Error when convert a very large integer(for example 2^256-1) to a binary due to the rounding error

Subject: Help: Error when convert a very large integer(for example 2^256-1) to a binary due to the rounding error

From: Shan Zhang

Date: 20 Apr, 2009 13:12:01

Message: 1 of 5

I want to use matlab to convert a very large integer (can be larger than 2^255) to a 256 bits long binary sequence, but since the integer is so large that, matlab will do some rounding operation, like: they will treat 2^256-2 the same as 2^256, if you input 2^256-(2^256-2) in matlab, the result will be 0. So when I convert the 2^256 and 2^256-2 to binary sequences, the results will be the same.

 I have tried with "de2bi", and I have also written a function by myself, but both do not help :

%calculate binary representation of integer, whose highest-order digit is the right-most one.

function out = dediconv(integer,n)
 
out = zeros(1,n);
    
if integer < 2^n
     
 for i=n:-1:1
     
     if integer>=2^(i-1)
     
         out(i) = 1;
         
         integer = integer - 2^(i-1);
         
     end
     
     if integer == 0
         
         break;
         
     end
     
 end

else
    error('debiconf_shan: the given length n is too small for representing the given integer.' );
    
end

Subject: Help: Error when convert a very large integer(for example 2^256-1) to a binary due to the rounding error

From: Roger Stafford

Date: 20 Apr, 2009 13:21:01

Message: 2 of 5

"Shan Zhang" <zscumt@hotmail.com> wrote in message <gshsb1$jpu$1@fred.mathworks.com>...
> I want to use matlab to convert a very large integer (can be larger than 2^255) to a 256 bits long binary sequence, but since the integer is so large that, matlab will do some rounding operation, like: they will treat 2^256-2 the same as 2^256, if you input 2^256-(2^256-2) in matlab, the result will be 0. So when I convert the 2^256 and 2^256-2 to binary sequences, the results will be the same.
>
> I have tried with "de2bi", and I have also written a function by myself, but both do not help :
>
> %calculate binary representation of integer, whose highest-order digit is the right-most one.
>
> function out = dediconv(integer,n)
>
> out = zeros(1,n);
>
> if integer < 2^n
>
> for i=n:-1:1
>
> if integer>=2^(i-1)
>
> out(i) = 1;
>
> integer = integer - 2^(i-1);
>
> end
>
> if integer == 0
>
> break;
>
> end
>
> end
>
> else
> error('debiconf_shan: the given length n is too small for representing the given integer.' );
>
> end

  You cannot express such large integers using matlab's 53-bit significands. You must use the symbolic toolbox for that.

Roger Stafford

Subject: Help: Error when convert a very large integer(for example 2^256-1) to a binary due to the rounding error

From: Shan Zhang

Date: 20 Apr, 2009 18:15:04

Message: 3 of 5

Thanks!

Subject: Help: Error when convert a very large integer(for example 2^256-1)

From: Christopher Creutzig

Date: 21 Apr, 2009 14:58:13

Message: 4 of 5

Shan Zhang wrote:
> I want to use matlab to convert a very large integer (can be larger than 2^255) to a 256 bits long binary sequence, but since the integer is so large that, matlab will do some rounding operation, like: they will treat 2^256-2 the same as 2^256, if you input 2^256-(2^256-2) in matlab, the result will be 0. So when I convert the 2^256 and 2^256-2 to binary sequences, the results will be the same.

Unless you have the symbolic toolbox already, you might find vpi from
FileExchange to be helpful.


Christopher

Subject: Help: Error when convert a very large integer(for example 2^256-1) to a binary due to the rounding error

From: John D'Errico

Date: 21 Apr, 2009 15:36:14

Message: 5 of 5

"Shan Zhang" <zscumt@hotmail.com> wrote in message <gshsb1$jpu$1@fred.mathworks.com>...
> I want to use matlab to convert a very large integer (can be larger than 2^255) to a 256 bits long binary sequence, but since the integer is so large that, matlab will do some rounding operation, like: they will treat 2^256-2 the same as 2^256, if you input 2^256-(2^256-2) in matlab, the result will be 0. So when I convert the 2^256 and 2^256-2 to binary sequences, the results will be the same.
>

From my vpi toolbox...

http://www.mathworks.com/matlabcentral/fileexchange/22725

vpi2bin(vpi(2)^256 -2)
ans =
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110

Consistent with dec2bin, it returns a character result. If you wish
a numeric result, then use this:

vpi2bin(vpi(2)^256 -2) - '0'

or this, for a boolean:

vpi2bin(vpi(2)^256 -2) == '1'

John

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us