是否有一個Matlab函數返回浮點數的二進制表示形式?在Matlab中的數字的二進制表示法
4
A
回答
4
在Matlab中,可以使用Java JDK函數。
用於在Matlab轉換浮子(單精度32位數字)爲二進制字符串表示可能是簡單的回答:
flt=3.14
import java.lang.Integer java.lang.Float;
Integer.toBinaryString(Float.floatToIntBits(flt))
長的答案:浮子的轉換(單精度32位數字)在Matlab中的二進制字符串表示
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, [email protected]
% 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
和二進制字符串轉換爲浮點用一個小的開銷:
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, [email protected]
% 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
1
入住此FileExchange提交:
2
相關問題
- 1. 數字的二進制表示
- 2. 二進制數的IEEE表示法
- 3. 非整數數字二進制表示
- 4. 在二進制符號數字(冗餘二進制表示)中除以2
- 5. Matlab二維圖 - 二進制顯示軸
- 6. pgsql的數字/十進制二進制表示,可可
- 7. 在Matlab中處理二進制數據
- 8. 有符號二進制數字char * of 2s恭維二進制表示的char * *?
- 9. 雙數的二進制表示
- 10. c中的二進制數和字符串表示形式
- 11. Matlab:如何用二進制表示實數
- 12. 用十進制數表示的二進制浮點數
- 13. 十六進制爲二進制和十進制在matlab中
- 14. 在文件中表示二進制樹
- 15. 在Perl中浮點到十進制轉換的二進制表示法
- 16. 表示八進制中的數字
- 17. 在MatLab中讀取二進制文件
- 18. bigint轉換爲二進制(8)列的二進制表示
- 19. 將表示二進制的字符串轉換爲二進制python
- 20. 爲什麼十進制數16 == 5的二進制表示中的位數?
- 21. 二進制和十六進制中由7位數表示的值的數量
- 22. 如何在PHP中獲取浮點數的二進制表示?
- 23. 這是什麼樣的二進制數字表示形式?
- 24. 二進制數據的Python字符串表示
- 25. Go - 將表示二進制數的字符串轉換爲int
- 26. 32位整數的二進制表示法
- 27. 替換算法C++無效操作數的二進制表示
- 28. 寫的字符串二進制數據的二進制文件
- 29. MIPS懂二進制表示
- 30. 二進制表示樹
原生MATLAB代碼怎麼樣? – Royi 2014-05-28 11:24:31