user2045143
user2045143

Reputation: 249

How to convert decimal to IEEE double precision floating point binary in Matlab?

I am trying to generate an MIF file for FPGA synthesis and I am programming in Matlab to do it.

I want numbers ranging from -1 to 1 (-.9,-.8,-.7...1) in IEEE standard double precision floating point so that I can write it onto an MIF file an use in Verilog. Are there any Matlab functions to do this in an easy way? Thanks for any suggestion in advance.

Upvotes: 0

Views: 1363

Answers (1)

nispio
nispio

Reputation: 1744

Try this:

oldFormat = get(0,'Format');
format('hex'); display( -1:0.1:1 );
format(oldFormat);

Only the middle line is important. The other two just put your format back to what it was before ('short' by default). I don't know for certain that the output conforms to IEEE, but I suspect it will. According to the documentation this will display a "Hexadecimal representation of a binary double-precision number."

If you want to save them as strings you could print later use sprintf with format %bx:

x = -1:0.1:1;
for i = 1:length(x)
    vals{i,1} = x(i);
    vals{i,2} = sprintf('%bx',x(i));
    vals{i,3} = dec2bin(hex2dec(sprintf('%bx',x(i))),64);
end
display(vals(:,[1 2]));
display(vals(:,3));

Upvotes: 3

Related Questions