Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a half precision floating point number in C++
    primarykey
    data
    text
    <p>I am trying to implement a simple half precision floating point type, entirely for storage purposes (no arithmetic, converts to double implicitly), but I get weird behavior. I get completely wrong values for <code>Half</code> between -0.5 and 0.5. Also I get a nasty "offset" for values, for example 0.8 is decoded as 0.7998.</p> <p>I am very new to C++, so I would be great if you can point out my mistake and help me with improving the accuracy a bit. I am also curious how portable is this solution. Thanks!</p> <p>Here is the output - double value and actual decoded value from the half:</p> <pre><code>-1 -1 -0.9 -0.899902 -0.8 -0.799805 -0.7 -0.699951 -0.6 -0.599854 -0.5 -0.5 -0.4 -26208 -0.3 -19656 -0.2 -13104 -0.1 -6552 -1.38778e-16 -2560 0.1 6552 0.2 13104 0.3 19656 0.4 26208 0.5 32760 0.6 0.599854 0.7 0.699951 0.8 0.799805 0.9 0.899902 </code></pre> <p>Here is the code so far:</p> <pre><code>#include &lt;stdint.h&gt; #include &lt;cmath&gt; #include &lt;iostream&gt; using namespace std; #define EXP 4 #define SIG 11 double normalizeS(uint v) { return (0.5f * v / 2048 + 0.5f); } uint normalizeP(double v) { return (uint)(2048 * (v - 0.5f) / 0.5f); } class Half { struct Data { unsigned short sign : 1; unsigned short exponent : EXP; unsigned short significant : SIG; }; public: Half() {} Half(double d) { loadFromFloat(d); } Half &amp; operator = (long double d) { loadFromFloat(d); return *this; } operator double() { long double sig = normalizeS(_d.significant); if (_d.sign) sig = -sig; return ldexp(sig, _d.exponent /*+ 1*/); } private: void loadFromFloat(long double f) { long double v; int exp; v = frexp(f, &amp;exp); v &lt; 0 ? _d.sign = 1 : _d.sign = 0; _d.exponent = exp/* - 1*/; _d.significant = normalizeP(fabs(v)); } Data _d; }; int main() { Half a[255]; double d = -1; for (int i = 0; i &lt; 20; ++i) { a[i] = d; cout &lt;&lt; d &lt;&lt; " " &lt;&lt; a[i] &lt;&lt; endl; d += 0.1; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload