Single precision floating point conversion converter decoder makes it readable
SP_FP.cpp@0:f99d1aa55201, 2015-01-20 (annotated)
- Committer:
- LORDTEK
- Date:
- Tue Jan 20 14:45:08 2015 +0000
- Revision:
- 0:f99d1aa55201
Single Precision Floating Point converter makes it readable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LORDTEK | 0:f99d1aa55201 | 1 | #include "SP_FP.h" |
LORDTEK | 0:f99d1aa55201 | 2 | #include "mbed.h" |
LORDTEK | 0:f99d1aa55201 | 3 | |
LORDTEK | 0:f99d1aa55201 | 4 | SP_FP::SP_FP() { |
LORDTEK | 0:f99d1aa55201 | 5 | result = 0.0f; |
LORDTEK | 0:f99d1aa55201 | 6 | value_Temp = 0; |
LORDTEK | 0:f99d1aa55201 | 7 | } |
LORDTEK | 0:f99d1aa55201 | 8 | |
LORDTEK | 0:f99d1aa55201 | 9 | float SP_FP::decoder(uint32_t VALUE) { |
LORDTEK | 0:f99d1aa55201 | 10 | (VALUE >> 31) ? result = -1.0f : result = 1.0f; // Determine sign |
LORDTEK | 0:f99d1aa55201 | 11 | value_Temp = VALUE << 1; // Delete sign bit |
LORDTEK | 0:f99d1aa55201 | 12 | value_Temp = value_Temp >> 24; // Make it alone only exponent |
LORDTEK | 0:f99d1aa55201 | 13 | result *= pow(2.0f, ((float) ((int) value_Temp - 127))); // E (will be pow of 2) = X - 127 |
LORDTEK | 0:f99d1aa55201 | 14 | value_Temp = VALUE << 9; // Delete after 23rd bit |
LORDTEK | 0:f99d1aa55201 | 15 | value_Temp = value_Temp >> 9; // Make it alone only mantissa (fraction, significand) |
LORDTEK | 0:f99d1aa55201 | 16 | result *= 1.0f + (float) value_Temp / 8388608.0f; // pow(2.0, 23.0) thanks to http://softelectro.ru |
LORDTEK | 0:f99d1aa55201 | 17 | return result; |
LORDTEK | 0:f99d1aa55201 | 18 | } |