<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Gleitkommazahlen]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe eine Verständnisfrage zu Gleitkommazahlen in C++, im besonderen wie die binär kodiert sind. Mein Compiler ist der gcc-4.3 / gcc-4.4 und ich benutze folgendes Testprogramm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;sstream&gt;

using namespace std;

template&lt; class T &gt; string get_bin( T a )
{
    stringstream s;
    unsigned char *aa = (unsigned char*)(&amp;a);
    for( size_t i=0 ; i&lt;sizeof(T) ; ++i )
	for( int j=7 ; j&gt;=0 ; j--)
            s &lt;&lt; ((aa[i] &gt;&gt; j) &amp; 1);
    return s.str();
}

int main( int argc , char **argv )
{
    cout &lt;&lt; get_bin( 1.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( -1.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 2.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 3.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 4.0 ) &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Die Ausgabe davon ist:</p>
<pre><code>0000000000000000000000000000000000000000000000001111000000111111
0000000000000000000000000000000000000000000000001111000010111111
0000000000000000000000000000000000000000000000000000000001000000
0000000000000000000000000000000000000000000000000000100001000000
0000000000000000000000000000000000000000000000000001000001000000
</code></pre>
<p>Damit bekommt man schon ein bisschen was raus, z.b. dass das 8.bit von rechts das Vorzeichen markiert. Ich bin mir auch ziemlich sicher, dass die letzten 7 bits den exponenten darstellen. Allerdings habe ich keine Ahnung wie die Mantisse hier kodiert, bzw. dargestellt ist. Kann mir da jemand weiterhelfen oder kennt einige gute Links in denen das erklärt wird?</p>
<p>Vielen Dank und viele Grüße,</p>
<p>headmyshoulder</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/265716/gleitkommazahlen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 03 Sep 2026 19:27:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/265716.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 27 Apr 2010 20:30:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Gleitkommazahlen on Tue, 27 Apr 2010 20:30:55 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe eine Verständnisfrage zu Gleitkommazahlen in C++, im besonderen wie die binär kodiert sind. Mein Compiler ist der gcc-4.3 / gcc-4.4 und ich benutze folgendes Testprogramm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;sstream&gt;

using namespace std;

template&lt; class T &gt; string get_bin( T a )
{
    stringstream s;
    unsigned char *aa = (unsigned char*)(&amp;a);
    for( size_t i=0 ; i&lt;sizeof(T) ; ++i )
	for( int j=7 ; j&gt;=0 ; j--)
            s &lt;&lt; ((aa[i] &gt;&gt; j) &amp; 1);
    return s.str();
}

int main( int argc , char **argv )
{
    cout &lt;&lt; get_bin( 1.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( -1.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 2.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 3.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 4.0 ) &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Die Ausgabe davon ist:</p>
<pre><code>0000000000000000000000000000000000000000000000001111000000111111
0000000000000000000000000000000000000000000000001111000010111111
0000000000000000000000000000000000000000000000000000000001000000
0000000000000000000000000000000000000000000000000000100001000000
0000000000000000000000000000000000000000000000000001000001000000
</code></pre>
<p>Damit bekommt man schon ein bisschen was raus, z.b. dass das 8.bit von rechts das Vorzeichen markiert. Ich bin mir auch ziemlich sicher, dass die letzten 7 bits den exponenten darstellen. Allerdings habe ich keine Ahnung wie die Mantisse hier kodiert, bzw. dargestellt ist. Kann mir da jemand weiterhelfen oder kennt einige gute Links in denen das erklärt wird?</p>
<p>Vielen Dank und viele Grüße,</p>
<p>headmyshoulder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1889159</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1889159</guid><dc:creator><![CDATA[headmyshoulder]]></dc:creator><pubDate>Tue, 27 Apr 2010 20:30:55 GMT</pubDate></item><item><title><![CDATA[Reply to Gleitkommazahlen on Tue, 27 Apr 2010 20:37:41 GMT]]></title><description><![CDATA[<p>Wie das genau funktioniert ist zwar nicht durch den Sprachstandard vorgeschrieben, aber fast alle Maschinen benutzen dies hier:<br />
<a href="http://de.wikipedia.org/wiki/IEEE_754" rel="nofollow">http://de.wikipedia.org/wiki/IEEE_754</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1889160</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1889160</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 27 Apr 2010 20:37:41 GMT</pubDate></item><item><title><![CDATA[Reply to Gleitkommazahlen on Tue, 27 Apr 2010 20:45:41 GMT]]></title><description><![CDATA[<p>Hier ist auch noch etwas über die Darstellung von Fließkommazahlen:</p>
<p><a href="http://www.cirsovius.de/CPM/Projekte/Artikel/Mathe/IEEE_Reals/IEEE1.html" rel="nofollow">http://www.cirsovius.de/CPM/Projekte/Artikel/Mathe/IEEE_Reals/IEEE1.html</a></p>
<p>MfG, Jochen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1889162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1889162</guid><dc:creator><![CDATA[Jochen S.]]></dc:creator><pubDate>Tue, 27 Apr 2010 20:45:41 GMT</pubDate></item><item><title><![CDATA[Reply to Gleitkommazahlen on Tue, 27 Apr 2010 21:05:46 GMT]]></title><description><![CDATA[<p>Es würde die Anschaulichkeit erhöhen wenn du die Endianess beachtest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1889168</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1889168</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 27 Apr 2010 21:05:46 GMT</pubDate></item><item><title><![CDATA[Reply to Gleitkommazahlen on Wed, 28 Apr 2010 14:15:22 GMT]]></title><description><![CDATA[<p>Tim schrieb:</p>
<blockquote>
<p>Es würde die Anschaulichkeit erhöhen wenn du die Endianess beachtest.</p>
</blockquote>
<p>Jepp, das bringt mich auch schon sehr viel weiter, hier mal der neue Code</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;sstream&gt;

using namespace std;

template&lt; class T &gt;
inline string get_bin( T a )
{
    stringstream s;
    unsigned char *aa = reinterpret_cast&lt; unsigned char* &gt;(&amp;a);
    for( int i = sizeof(T)-1 ; i&gt;=0 ; --i )
    {
        for( int j=7 ; j&gt;=0 ; --j)
            s &lt;&lt; ((aa[i] &gt;&gt; j) &amp; 1);
        s &lt;&lt; &quot; &quot;;
    }
    return s.str();
}

int main( int argc , char **argv )
{
    cout &lt;&lt; get_bin( 1.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( -1.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 2.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 3.0 ) &lt;&lt; endl;
    cout &lt;&lt; get_bin( 4.0 ) &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>und die Ausgabe dazu:</p>
<pre><code>00111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
10111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
01000000 00001000 00000000 00000000 00000000 00000000 00000000 00000000
01000000 00010000 00000000 00000000 00000000 00000000 00000000 00000000
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1889392</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1889392</guid><dc:creator><![CDATA[headmyshoulder]]></dc:creator><pubDate>Wed, 28 Apr 2010 14:15:22 GMT</pubDate></item><item><title><![CDATA[Reply to Gleitkommazahlen on Wed, 28 Apr 2010 13:59:22 GMT]]></title><description><![CDATA[<p>Vielleicht noch ein paar allgemeine Bemerkungen zu deinem Code:</p>
<pre><code class="language-cpp">int n = sizeof(T);
    if( !n ) return &quot;&quot;;
</code></pre>
<p>1. Ich würde Vergleiche auf Null explizit hinschreiben ( <code>n == 0</code> ). Schliesslich ist die Bedeutung eine ziemlich andere als die Negation eines logischen Ausdrucks.<br />
2. Konstante Ausdrücke (zur Compilezeit bekannt) würde ich mit <code>const</code> deklarieren, das erlaubt dem Compiler Optimierungen und schränkt die Variable nicht unnötig ein.<br />
3. Der <code>sizeof</code> -Operator gibt niemals 0 zurück, da solch ein Objekt keinen Speicherplatz benötigen würde. Die Abfrage ist also überflüssig.</p>
<pre><code class="language-cpp">unsigned char *aa = (unsigned char*)(&amp;a);
</code></pre>
<p><em>Ich persönlich</em> würde so einen Cast mit <code>reinterpret_cast</code> kenntlich machen.</p>
<pre><code class="language-cpp">for( int i = n-1 ; i&gt;=0 ; --i )
    {
        for( int j=7 ; j&gt;=0 ; j--)
</code></pre>
<p>Warum <code>--i</code> , aber <code>j--</code> ? Hier sind beide Ausdrücke äquivalent, also würde ich mich für einen entscheiden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1889482</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1889482</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 28 Apr 2010 13:59:22 GMT</pubDate></item><item><title><![CDATA[Reply to Gleitkommazahlen on Wed, 28 Apr 2010 14:13:18 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Vielleicht noch ein paar allgemeine Bemerkungen zu deinem Code:</p>
</blockquote>
<p>Danke für die Hinweise, ich geb Dir vollkommen recht. Der Code wurde nur zur Demonstration geschrieben, deswegen vielleicht die eine oder andere Nachlässigkeit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1889495</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1889495</guid><dc:creator><![CDATA[headmyshoulder]]></dc:creator><pubDate>Wed, 28 Apr 2010 14:13:18 GMT</pubDate></item></channel></rss>