Decimal to Binary (ieee 754)

7 ビュー (過去 30 日間)
WhatIsMatlab-
WhatIsMatlab- 2016 年 2 月 12 日
回答済み: Walter Roberson 2016 年 2 月 14 日
I want to create a code that converts decimal to ieee754. I do not want to use dec2bin so I do not know where to start. I want x to be my decimal number, eb to number of exponent bits and mb for my mantissa. The outputs are the convert value 'q' followed by s, e, m for sign, exponent, mantissa.
function [q, s, e, m,] = GetRepresentationIEEE754(x, eb, mb)
  5 件のコメント
Guillaume
Guillaume 2016 年 2 月 13 日
Note that I wouldn't call my function conv as that name is already used by matlab for 1d convolution.
A better name would be GetRepresentationIEEE754.
WhatIsMatlab-
WhatIsMatlab- 2016 年 2 月 13 日
編集済み: WhatIsMatlab- 2016 年 2 月 13 日
I will note that. I did not know that thank you. I mainly just want to use the ieee 754 format of converting decimal numbers. It does not always have to be 32 bit.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 14 日
First you should convert the values to binary. You can do that with num2hex() to get hex that can be converted. Or you can use typecast() to reform the number into one of the unsigned integer data types.
Once you have the number in an unsigned integer data type, you can use bitget() or dec2bin() or de2bi() to convert the number into a binary form, from which you can extract individual groups of bits.
As a practical matter, I would point out that if you use dec2bin() on a uint64() then the number will be converted to double precision before it is converted to binary, and that is going to lose about 13 bits of value in the process (because uint64 are 64 bits and double precision can represent 53 bits.) I would therefore suggest that if you are planning to use dec2bin() that you do not typecast() to uint64 -- typecast to either uint8() or uint32() instead.
But personally I would probably typecast() to uint64, mod() to get the mantissa, bitshift() to remove the mantissa, mod() to get the exponent, bitshift() to get the sign...

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by