-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfp2bin.m
More file actions
92 lines (82 loc) · 3.49 KB
/
fp2bin.m
File metadata and controls
92 lines (82 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function binary = fp2bin(flpoint, format)
%FP2BIN Convert a floating point number into it's binary representation
%
% Usage:
% x = 3.25;
% s = fp2bin(x, 'single');
%
% -------------------------------------------------------------------------
% Author: Michael Wulf
% Washington University in St. Louis
% Kepecs Lab
%
% Date: 04/15/2022
% Version: 1.0.2
% Github: https://github.com/Michael-Wulf/OSCMessage
%
% Copyright (C) 2022 Michael Wulf
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
% MA 02110-1301, USA.
% -------------------------------------------------------------------------
if ( nargin < 1)
error('No floating point number give!');
end
% Validate the flpoint argument
validateattributes(flpoint, {'single', 'double'}, {'nonempty', 'scalar'}, 'fp2bin', 'flpoint');
if ( nargin == 1 )
format = 'double';
end
% Validate the format argument
validateattributes(format, {'char'}, {'nonempty'}, 'fp2bin', 'format');
% Remove leading and trailing whitespaces...
format = strtrim(format);
if ( (strcmpi(format, 'single')) || (strcmpi(format, 'float')) )
% Take the internal function num2hex to convert a single/float value
% into the corresponding hex notation
hexValues = num2hex(single(flpoint));
% Now take the hex representation and interprete it as a decimal value
% and convert this to a binary representation
binary = dec2bin(hex2dec(hexValues),32);
elseif ( strcmpi(format, 'double') )
% Take the internal function num2hex to convert a double value
% into the corresponding hex notation
hexValues = num2hex(double(flpoint));
% ATTENTION:
% It is not possible to just do it the same way as for the single/float
% format! The problem is, that especcially larger values can't be
% stored accurate enough! hex2dec tries to convert a hex representation
% (as an integer value!!!) into the double format. Up until 2^53, integer
% values can be stored precisely, but from 2^53 the double format will even
% lose precisions for integers! From 2^53 to 2^54 only the even numbers
% can be stored. From 2^42 to 2^55 only multiples of 4 can be stored.
% Than multiples of 8 and so on. Distance between two adjacent numbers
% in the range between 2^n and 2^(n+1) can be calculated by 2^(n-52)!
%
% Solution:
% To avoid the previously described circumstance, we can split it into
% 2x32-bit values!
% Take the internal function num2hex to convert the first 8 hex values
% to a binary representation y first converting it to an integer and
% than take it's binary representation
binary1 = dec2bin(hex2dec(hexValues(1:8)),32);
% Now do the same thing with the second 32-bit part
binary2 = dec2bin(hex2dec(hexValues(9:16)),32);
% Finally, combine both binary representations
binary = [binary1 binary2];
else
error('Unknown format: %s', format);
end
end