Is there a way to convert numbers to 8-bit and 12-bit floating point representation?

14 views (last 30 days)
By representation i mean, similar to the IEEE 754 single precision representation but instead of 32 bits we use only 8 or 12 bits to represent the number.

Accepted Answer

John D'Errico
John D'Errico on 20 Aug 2016
Simple answer in MATLAB - No. MATLAB does not provide floating point numbers with a generally user specified number of bits. Unless a single or double applies, then you are out of luck. So your choices are 52 bits or 23 bits.
Nothing stops you from creating a class of objects that contain numbers of your own creation, as long as you also provide tools to compute all operations on those numbers. That can take some serious work though.
  3 Comments
John D'Errico
John D'Errico on 21 Aug 2016
Short bit floating point numbers are a REALLY bad thing to work with. A REALLY bad idea.
If you are working in floating point, it is the least significant bits that lose a bit here, a bit there. On every computation, you have some significant chance of introducing an error in that LSB. These errors accumulate after many computations. But if you are only working in 8 bits of precision, can you really afford to lose anything at all? NOOOOO!!!!!!!!
Anyway, when you say 8 bit floats, how many bits will you put into the exponent? Shiver.
So what do you gain? Ok, a wee bit in terms of memory, that is, IF you can actually store those numbers efficiently. RAM IS CHEAP.
What else? speed? Again, you will see little in terms of a speed boost. Processors are designed to work with 32 bit buses and registers at least, usually now more than that. So you won't gain much by going to 8 or 12 bits in terms of speed.
Worse, if you think you can do linear algebra using 8 or 12 bit floats, you will need to write EVERYTHING yourself. Can you write faster code than the blas? Than tools like LAPACK?
So you gain little here, for a serious risk in terms of precision loss.
Do I know of anyone who has done this? No. OK, you could, in theory use my HPF toolbox to do short float computations. It will be slow of course, compared to compiled code. And HPF works in decimal. Finally, HPF will take more memory to store one number than you could achieve with a double. HPF is a fun tool to use, but far better to use it as it is targeted, to work in high precision - NOT low.
James Tursa
James Tursa on 22 Aug 2016
16-bit "Half Precision" floating point format is a relatively new but I think now common format used in graphics processors (e.g., the Nvidia Ge-Force processors). Since that is implemented in silicon, you could potentially do something with this format on the GPU that is fast and has h/w support. But once you got it on the MATLAB side of things, you couldn't really do much except to convert it to/from single or double for working with it. Some conversion code can be found here:
But 12-bits is really pushing it, and 8-bits sounds absurd. Other than writing converters to/from single and double to deal with h/w that works in these formats I see no compelling reason to try to work with these formats.
https://en.wikipedia.org/wiki/Half-precision_floating-point_format

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 21 Aug 2016
You might be able to do something useful with the Fixed Point Toolbox.
  2 Comments
aditya ramesh
aditya ramesh on 21 Aug 2016
Edited: aditya ramesh on 21 Aug 2016
Thank you Walter, but i already have the code for custom bit length fixed point conversion.
Walter Roberson
Walter Roberson on 21 Aug 2016
Which formats are you trying to support? See http://www.mrob.com/pub/math/floatformats.html#minifloat
Are you just looking for conversion routines, or are you also looking for operations (e.g., sum, product, log, gamma) ?

Sign in to comment.


Erendiz Tarakci
Erendiz Tarakci on 25 Nov 2017
Were you ever able to figure this out? And if so, do you mind sharing how you did it? I'm having a similar problem where I'm decreasing the bit depth of my matrix from 16 bits to 8 bits in 2 bit increments.

Community Treasure Hunt

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

Start Hunting!