MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / doublePacked2Float

Function doublePacked2Float

libraries/IEEE754tools/IEEE754tools.h:148–183  ·  view source on GitHub ↗

converts a packed array of bytes into a 32bit float. there can be an exponent overflow the mantissa is truncated to 23 bits.

Source from the content-addressed store, hash-verified

146// there can be an exponent overflow
147// the mantissa is truncated to 23 bits.
148float doublePacked2Float(byte* bar, int byteOrder = LSBFIRST)
149{
150 _FLOATCONV fl;
151 _DBLCONV dbl;
152
153#ifdef IEEE754_ENABLE_MSB
154 if (byteOrder == LSBFIRST)
155 {
156#endif
157 for (int i = 0; i < 8; i++)
158 {
159 dbl.b[i] = bar[i];
160 }
161#ifdef IEEE754_ENABLE_MSB
162 }
163 else
164 {
165 for (int i = 0; i < 8; i++)
166 {
167 dbl.b[i] = bar[7-i];
168 }
169 }
170#endif
171
172 int e = dbl.p.e - 1023 + 127; // exponent adjust
173 // TODO check exponent overflow.
174 if (e >=0 || e <= 255)
175 {
176 fl.p.s = dbl.p.s;
177 fl.p.e = e;
178 fl.p.m = dbl.p.m; // note this one clips the mantisse
179 return fl.f;
180 }
181 return NAN; // OR +-INF?
182 // return (fl.p.s) ? -INFINITY : INFINITY;
183}
184
185
186//

Callers 1

unittestFunction · 0.85

Calls

no outgoing calls

Tested by 1

unittestFunction · 0.68