<?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[Frage zu fmod unter Codeblocks]]></title><description><![CDATA[<p>Hey Leute, ich benutze den GCC, der mit Codeblocks mitgeliefert ist. Dabei ist mir gerade Folgendes aufgefallen</p>
<pre><code>#include &lt;cmath&gt;
using namespace std;
int main()
{
    cout&lt;&lt;fmod(18.L,0.04L) &lt;&lt; endl;     //Ausgabe: 3.659e-19
    cout&lt;&lt;fmod(18.L,(long double)0.04); //Ausgabe: 0.04
}
</code></pre>
<p>Die erste Ausgabe ist klar. Aber warum gibt er mir beim 2. etwas anderes aus?<br />
Codepad sagt das gleiche: <a href="http://codepad.org/1GE94oYr" rel="nofollow">http://codepad.org/1GE94oYr</a><br />
Kommt das durch Rundungsfehler beim Umwandeln von &quot;double&quot; in &quot;long double&quot; zustande? Und sollte der Compiler das &quot;double&quot; literal durch den cast nicht zur Compilezeit als &quot;long double&quot; literal auffassen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/310509/frage-zu-fmod-unter-codeblocks</link><generator>RSS for Node</generator><lastBuildDate>Tue, 04 Aug 2026 13:18:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/310509.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 13 Nov 2012 13:16:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 13:16:51 GMT]]></title><description><![CDATA[<p>Hey Leute, ich benutze den GCC, der mit Codeblocks mitgeliefert ist. Dabei ist mir gerade Folgendes aufgefallen</p>
<pre><code>#include &lt;cmath&gt;
using namespace std;
int main()
{
    cout&lt;&lt;fmod(18.L,0.04L) &lt;&lt; endl;     //Ausgabe: 3.659e-19
    cout&lt;&lt;fmod(18.L,(long double)0.04); //Ausgabe: 0.04
}
</code></pre>
<p>Die erste Ausgabe ist klar. Aber warum gibt er mir beim 2. etwas anderes aus?<br />
Codepad sagt das gleiche: <a href="http://codepad.org/1GE94oYr" rel="nofollow">http://codepad.org/1GE94oYr</a><br />
Kommt das durch Rundungsfehler beim Umwandeln von &quot;double&quot; in &quot;long double&quot; zustande? Und sollte der Compiler das &quot;double&quot; literal durch den cast nicht zur Compilezeit als &quot;long double&quot; literal auffassen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270466</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270466</guid><dc:creator><![CDATA[fmod]]></dc:creator><pubDate>Tue, 13 Nov 2012 13:16:51 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 13:37:15 GMT]]></title><description><![CDATA[<p>0.04 ist als binäre Fließkommazahl stumpf nicht genau darstellbar. Abhängig vom Datentypen wird also der genaue Wert des zweiten Parameters, den du an fmod gibst, im einen Fall etwas über und im anderen Fall etwas unter 0.04 liegen. Dementsprechend ist das Ergebnis einmal knapp über Null und einmal knapp unter diesem zweiten Parameter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270469</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270469</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Tue, 13 Nov 2012 13:37:15 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 13:37:39 GMT]]></title><description><![CDATA[<p>Doch, es wird schon die long double Variante von fmod genommen. Jedoch ist der Wert ein anderer:<br />
0.04 ist im Binärsystem ein nicht abbrechender Bruch (so wie 1/3 im Dezimalsystem 0.33333… ist). Daher wird, wenn du 0.04 einer Fließkommazahl zuweist (zumindest bei den üblichen Implementierungen von Fließkommazahlen) der nächst da dran gelegene Wert genommen. Also um beim Beispiel 1/3 zu bleiben hätte ein float z.B. den Wert 0.333333 und ein double den Wert 0.33333333333333 (ja, 0.333333 ist auch wieder nicht glatt im Binärsystem. Ist bloß ein Beispiel).</p>
<p>Jetzt sollte klar sein, was passiert: 0.04L ist ein long double, der so nah an 0.04 ist wie möglich. static_cast&lt;long double&gt;(0.04) ist ein long double, aber der Wert ist der von dem double-Literal 0.04, welches so nahe wie möglich an 0.04 ist, wie ein double dies erlaubt. Der long double ist zwar potentiell präziser, aber der Cast kann nicht wissen, was hier eigentlich gewollt ist und sieht nur den gerundeten double-Wert. Und dieser wird dann dem long double zugewiesen.</p>
<p>Irgendwie ein bisschen wirr. Ist schwer zu beschreiben. Nochmal mit 1/3:</p>
<pre><code>float foo = 1./3.;  // 0.333333
double bar = 1./3.; // 0.3333333333333
bar = foo;          // 0.333333
</code></pre>
<p>Klar geworden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270470</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270470</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 13 Nov 2012 13:37:39 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 13:44:16 GMT]]></title><description><![CDATA[<p>Ja ok. Ich habe gedacht, der Compiler würde den cast mit einem präzisieren wert durchführen, wenn das ganze zur Compilezeit geschieht (so wie er auch Konstanten auswertet). Danke für die schnellen Antworten!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270475</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270475</guid><dc:creator><![CDATA[fmod]]></dc:creator><pubDate>Tue, 13 Nov 2012 13:44:16 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 15:19:01 GMT]]></title><description><![CDATA[<p>wie kann denn dabei als ergebnis 0.04 rauskommen?<br />
bei</p>
<pre><code>x % m = y; // wobei 0 &lt;= y &lt; m
</code></pre>
<p>und nicht y &lt;= m, zumal bei der ersten rechnung (approximativ) 0 rauskommt (klar weil 0.8*450 = 18, da ist kein rest drinne)</p>
<p>hier wundern mich mehrere dinge....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270496</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Tue, 13 Nov 2012 15:19:01 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 15:30:09 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>wie kann denn dabei als ergebnis 0.04 rauskommen?</p>
</blockquote>
<p>Die double-Repräsentation von 0.04 in IEEE754 ist 4.00000000000000008326672684689E-2, also ein kleines bisschen größer als 0.04. Das Ergebnis passt schon.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270499</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270499</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 13 Nov 2012 15:30:09 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 21:37:34 GMT]]></title><description><![CDATA[<p>nein, wieso denn?<br />
das modulo hält den werte bereich doch zwischen 0 und eben hier 0.04. da ist es physikalisch doch gar nicht möglich, dass die 0.04 überschritten werden</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270600</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Tue, 13 Nov 2012 21:37:34 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 21:50:07 GMT]]></title><description><![CDATA[<p>Das Ergebnis ist möglicherweise sowas wie 0.03999999.... In der Ausgabe wird das dann auf 0.04 gerundet, da nicht alle Nachkommastellen angezeigt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270602</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270602</guid><dc:creator><![CDATA[Ramanujan]]></dc:creator><pubDate>Tue, 13 Nov 2012 21:50:07 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 22:22:20 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>nein, wieso denn?<br />
das modulo hält den werte bereich doch zwischen 0 und eben hier 0.04. da ist es physikalisch doch gar nicht möglich, dass die 0.04 überschritten werden</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
4.00000000000000008326672684689E-2 &gt; 0.04</p>
<p>Und wo die Physik hier her kommt, versteh ich auch nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270608</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270608</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 13 Nov 2012 22:22:20 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu fmod unter Codeblocks on Tue, 13 Nov 2012 22:38:12 GMT]]></title><description><![CDATA[<p>Ach, du meintest den Divisor und nicht das Ergebnis. Da haben skym0sh0 und ich dich falsch verstanden. Ja dann macht alles auch wieder Sinn^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270609</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270609</guid><dc:creator><![CDATA[Ramanujan]]></dc:creator><pubDate>Tue, 13 Nov 2012 22:38:12 GMT</pubDate></item></channel></rss>