将9.2532十进制转换为二进制MATLAB [英] convert 9.2532 decimal into binary matlab

查看:84
本文介绍了将9.2532十进制转换为二进制MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用de2bi(x),但是它仅对整数有效,我想将点十进制的数字转换为二进制

I use de2bi(x) but it help only for integer i want to convert for digit with point decimal into binary

de2bi(x)

将此9.2553转换为带有小数部分的十进制,如果没有可能的话,也可以在Matlab中将其转换成二进制格式,而不是将要输出代码的函数文件

convert this 9.2553 into decimal with fraction part also convert into binary format in Matlab if possible than without function file with code want output

推荐答案

MATLAB当然已经使用

MATLAB, of course, already stores double values in binary using IEEE-754 binary64 format. All we have to do is somehow get MATLAB to show us the bits.

一种方法是使用 typecast ,这会使MATLAB解释一个一组不同类型的内存位置.在这种情况下,我们将使MATLAB认为doubleuint64,然后通过dec2bin发送整数".之后,我们必须对字符串进行一些分解才能获得实际值.

One way is to use typecast which makes MATLAB interpret a set of memory locations as a different type. In this case, we'll make MATLAB think a double is a uint64 and then send the "integer" through dec2bin. We'll have to do some decomposition on the string after that to get the actual value.

注意:当前仅适用于正值.如果您还需要负值,我将不得不进行一些调整.

Note: This currently only works with positive values. If you need negative values too, I'll have to make some adjustments.

function binstr = double2bin(d)
   d = double(d);   % make sure the input is a double-precision float
   ieee754_d = dec2bin(typecast(d, 'uint64'),64);   % read double as uint64
   % IEEE-754 64-bit double:
   %    bit 1 (msb) = sign bit (we'll ignore this for now)
   %    bits 2-12   = exponent with bias of 1023
   %    bits 13-64  = significand with leading 1 removed (implicit)
   exponent = bin2dec(ieee754_d(2:12))-1022;   % 2^n has n+1 bits
   significand = ['1' ieee754_d(13:64)];
   if (exponent < 1)   % d < 1, so we'll need to pad with zeros
      binstr = ['0.' repmat('0',1,-exponent) significand];
   else   % d >= 1; move exponent bits to the left of binary point
      binstr = [significand(1:exponent) '.' significand(exponent+1:end)];
   end
end

试运行:

>> double2bin(9.2532)
ans = 1001.0100000011010001101101110001011101011000111000100

这篇关于将9.2532十进制转换为二进制MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆