在Matlab若干二进制重新presentation [英] Binary representation of a number in Matlab
本文介绍了在Matlab若干二进制重新presentation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个返回浮点数的二进制重新presentation Matlab的功能?
Is there a Matlab function that returns the binary representation of a float number?
推荐答案
在MATLAB中,可以使用Java JDK功能。
In Matlab, one can use Java JDK functions.
有关在Matlab转换浮动(单precision 32位数字),以二进制字符串重新presentation简短的回答可能是:
The short answer for converting float (single precision 32-bit number) in Matlab to a binary string representation might be:
flt=3.14
import java.lang.Integer java.lang.Float;
Integer.toBinaryString(Float.floatToIntBits(flt))
长的答案:浮动(单precision 32位数字)在Matlab中转化为二进制字符串重新presentation
The long answer: conversion of float (single precision 32-bit number) in Matlab to a binary string representation
function out=float2binstring(flt)
% converts a float number to binary in matlab according to IEEE754
%
% Usage:
% float2binstring(-3.14)
%
% http://www.h-schmidt.net/FloatApplet/IEEE754.html
%
% Limitations:
% Rounding errors: Not every decimal number can be expressed exactly as a floating
% point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit.
%
% Andrej Mosat, nospam@mosi.sk
% 03/2012
% v0.0
% License: GNU GPL
%
% See also: BINSTRING2FLOAT
% this is a trick to use java JDK should be installed, tested on Matlab R2010
import java.lang.Integer java.lang.Float;
if ( ~isnumeric(flt) )
error('input must be a number');
end
out=Integer.toBinaryString(Float.floatToIntBits(flt));
end
和浮动一点点开销二进制字符串的转换:
And a conversion of binary string to float with a little overhead:
function out=binstring2float(binstr)
% converts a binary string to float number according to IEEE754
%
% Usage:
% binstring2float('11000000010010001111010111000011')
% -3.14
%
%
% http://www.h-schmidt.net/FloatApplet/IEEE754.html
%
% Limitations:
% Rounding errors: Not every decimal number can be expressed exactly as a floating
% point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit.
%
% Andrej Mosat, nospam@mosi.sk
% 03/2012
% v0.0
% License: GNU GPL
%
% See also: FLOAT2BINSTRING
import java.lang.Long java.lang.Float;
if isequal(class(binstr), 'java.lang.String')
binstr=char(binstr);
end
if ( ~isstr(binstr) )
error('input must be a binary string');
end
% Error handling for binary strings should be added here
% the sign is negative
if binstr(2)=='1'
binstr(2)='';
isnegative=1;
else
isnegative=0;
end
out=Float.intBitsToFloat( Long.parseLong( binstr , 2) );
if isnegative
out=-out;
end
end
这篇关于在Matlab若干二进制重新presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文