How to convert half-precision number into hexadecimal representation?
Show older comments
Hi,
I am using matlab 2019B with support for complex half-precision operations. I want to store my results in a file but fwrite doesn't support class half. Some workaround is to convert to hex and then store as uint16. The problem is that num2hex also doesn't support half class. One way to obtain hexadecimal representation I have found is to use format hex, but parsing output string is not a solution for me.
My question is - Is there any native posibility to store half-precission numbers if file, or convert to hexadecimal representation for further processing?
Best Regards
Adam
Accepted Answer
More Answers (1)
Krishna Bindumadhavan
on 6 May 2020
2 votes
You can use half.typecast() and storedInteger functions as a workaround to convert to and from half precision and the underlying uint16 storage.
ex.
>>storedInteger(half(100))
ans =
uint16
22080
>>half.typecast(ans)
ans =
half
100
Appreciate your feedback regarding typecast,fwrite,num2hex, and mxArray limitations. We will consider better support for these in a future release.
Best Regards,
Krishna.
1 Comment
James Tursa
on 6 May 2020
Edited: James Tursa
on 6 May 2020
Thanks!
I would note that unlike the typecast( ) function for doubles and singles which produces shared data copies, the storedInteger( ) and half.typecast( ) functions listed above produce deep data copies. E.g.,
>> h = half(rand(1,100000000/2,'single'));
>> whos
Name Size Bytes Class Attributes
h 1x50000000 100000000 half
>> memory
Maximum possible array: 18314 MB (1.920e+10 bytes) *
Memory available for all arrays: 18314 MB (1.920e+10 bytes) *
Memory used by MATLAB: 1992 MB (2.088e+09 bytes)
Physical Memory (RAM): 8088 MB (8.481e+09 bytes)
* Limited by System Memory (physical + swap file) available.
>> s = storedInteger(h);
>> memory
Maximum possible array: 18170 MB (1.905e+10 bytes) *
Memory available for all arrays: 18170 MB (1.905e+10 bytes) *
Memory used by MATLAB: 2097 MB (2.199e+09 bytes)
Physical Memory (RAM): 8088 MB (8.481e+09 bytes)
* Limited by System Memory (physical + swap file) available.
>> H = half.typecast(s);
>> memory
Maximum possible array: 18128 MB (1.901e+10 bytes) *
Memory available for all arrays: 18128 MB (1.901e+10 bytes) *
Memory used by MATLAB: 2204 MB (2.311e+09 bytes)
Physical Memory (RAM): 8088 MB (8.481e+09 bytes)
* Limited by System Memory (physical + swap file) available.
You can see in the above that the 100MB half variable h gets deep data copied for these steps (i.e., the Memory used by MATLAB increases by 100MB). This is an example of what I hope can be avoided in future versions of MATLAB. Shared data copy methods to get at the underlying data, and pointer access to the data inside mex routines. Making half a simple numeric class instead of an opaque class is one way to accomplish this.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!