Decimal to Floating Point Representation

36 views (last 30 days)
WhatIsMatlab-
WhatIsMatlab- on 14 Feb 2016
Edited: WhatIsMatlab- on 16 Feb 2016
I want to use a normalized mantissa. Since I do not want to use any of the built in converters how would I do this. Where is a good starting point.
Thanks

Answers (2)

Walter Roberson
Walter Roberson on 14 Feb 2016
I set out a specific method that does not use dec2bin() or any of the built-in converters in response to your previous question http://uk.mathworks.com/matlabcentral/answers/267926-decimal-to-binary-ieee-754#answer_209735
Whether you are using a normalized mantissa or not does not make a difference as to whether you could use dec2bin() on an integer data class such as I discussed in the above link.
  2 Comments
WhatIsMatlab-
WhatIsMatlab- on 14 Feb 2016
Edited: WhatIsMatlab- on 14 Feb 2016
Ok thank you but I was trying not to use any of the number conversions that matlab had.
Walter Roberson
Walter Roberson on 14 Feb 2016
uint64() is not a number conversion, it is a type conversion.
The number of exponent bits does not tell you what the exponent bias is. It is completely valid in floating point representation to favour large numbers or favour small numbers by adjusting the exponent value that is to be used for 2^0. For example if you were designing for astronomy, you might not have any use for values smaller than the Planck distance (about 1e-43) so you might design for the range 2^(-143) to 2^880 (10 bits of exponent.)
And because any particular number of bits can represent an even number of values, you need to define whether it is the negative exponents or the positive exponents that get short-changed to make room for the value corresponding to 0. If you were using, for example, 8 bits, would your representation range be 2^(-127) to 2^(+128), which together with 2^0 gives you 127+0+128 = 256 exponent values, or would your representation range be 2^(-128) to 2^(+127), which together with 2^0 gives you 128+0+127 = 256 exponent values? Or are you going to reserve one of the exponent values and use an equal number on each side, 2^(-127) to 2^(+127) with the 256'th possible exponent value reserved for NaN and Inf and the like?

Sign in to comment.


John D'Errico
John D'Errico on 14 Feb 2016
Edited: John D'Errico on 14 Feb 2016
Easy enough to extract the information from a number. Just convert to hex. Then extract the corresponding bits.
N = 1.23;
hex = sprintf('%bx',N)
hex =
3ff3ae147ae147ae
However, it sounds like you wish to create a number q that has a different number of bits allocated to each of the exponent and mantissa. You CANNOT have a floating point number in MATLAB that uses a user designated number of bits for the mantissa versus exponent. Sorry, but this is simply not an ability that MATLAB offers you, unless you will convert to perhaps C, and then do ALL of your computations there.
As an alternative, you can use a tool like my HPF toolbox to create a number with any designated number of decimal digits.
  3 Comments
Walter Roberson
Walter Roberson on 15 Feb 2016
I am not sure what 32 bits has to do with anything here?
You can create a number any binary number you want, and you can use MATLAB to do so, but to get MATLAB to display or operate on it in a manner compatible with your design, you would need to design a new object oriented class.
John D'Errico
John D'Errico on 15 Feb 2016
Even if you DO design some new class though, in order for it to work in MATLAB, you would need to write (from scratch) ALL operations on it. For example, want to multiply and add? Write the code for it. A matrix multiply? Write the code. FFT? The same. That is essentially what I had to do when I wrote HPF, and there are many tools that are not implemented for use with HPF, even after many hundreds of hours invested.
I'm not sure what you intend by sticking with 32 bits. MATLAB uses double precision by default, so 64 bits, unless you use single.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!