Single precision floating point conversion converter decoder makes it readable
Revision 0:f99d1aa55201, committed 2015-01-20
- Comitter:
- LORDTEK
- Date:
- Tue Jan 20 14:45:08 2015 +0000
- Commit message:
- Single Precision Floating Point converter makes it readable
Changed in this revision
SP_FP.cpp | Show annotated file Show diff for this revision Revisions of this file |
SP_FP.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SP_FP.cpp Tue Jan 20 14:45:08 2015 +0000 @@ -0,0 +1,18 @@ +#include "SP_FP.h" +#include "mbed.h" + +SP_FP::SP_FP() { + result = 0.0f; + value_Temp = 0; +} + +float SP_FP::decoder(uint32_t VALUE) { + (VALUE >> 31) ? result = -1.0f : result = 1.0f; // Determine sign + value_Temp = VALUE << 1; // Delete sign bit + value_Temp = value_Temp >> 24; // Make it alone only exponent + result *= pow(2.0f, ((float) ((int) value_Temp - 127))); // E (will be pow of 2) = X - 127 + value_Temp = VALUE << 9; // Delete after 23rd bit + value_Temp = value_Temp >> 9; // Make it alone only mantissa (fraction, significand) + result *= 1.0f + (float) value_Temp / 8388608.0f; // pow(2.0, 23.0) thanks to http://softelectro.ru + return result; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SP_FP.h Tue Jan 20 14:45:08 2015 +0000 @@ -0,0 +1,15 @@ +#ifndef SP_FP_H +#define SP_FP_H +#include "mbed.h" + +class SP_FP { +public: + SP_FP(); + float decoder(uint32_t VALUE); + +private: + float result; + uint32_t value_Temp; // Needed because of bitwise operations +}; + +#endif \ No newline at end of file