<?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[C++ Rundet die Werte?]]></title><description><![CDATA[<p>Hallo Community.</p>
<p>Ich habe da einen ganz schön komischen Bug. C++ ( gcc Version 4.3.3) rundet die Werte, wenn ich einen Double in einen Integer umwandle.</p>
<p>Hier der Code von meiner Beispielklasse (Bruchrechnen Simulieren)</p>
<pre><code class="language-cpp">Bruch::Bruch(const double val)
{
	int ganzZahl = val;   // BEISPIEL 2.8  ==&gt; ganzZahl = 2
	int nachkomma = (val - ganzZahl) * 10;  // Problem: nachKomma = 7?? nicht 8 ???

	std::cout &lt;&lt; &quot; Ganzzahl: &quot; &lt;&lt; ganzZahl &lt;&lt; &quot;\t nachkomma: &quot; &lt;&lt; nachkomma &lt;&lt; std::endl;

	if(nachkomma == 0)
	{
		this-&gt;nenner = 1;
		this-&gt;zaehler = ganzZahl;
	}
	else
	{
		this-&gt;nenner = nachkomma;
		this-&gt;zaehler = (ganzZahl *  nachkomma)+1;
	}
}
</code></pre>
<p>Woran könnte das liegen?</p>
<p>Vielen Dank schonmal für die Antworten im Voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Drake</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/252417/c-rundet-die-werte</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 12:33:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/252417.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Oct 2009 15:12:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Rundet die Werte? on Mon, 19 Oct 2009 15:12:02 GMT]]></title><description><![CDATA[<p>Hallo Community.</p>
<p>Ich habe da einen ganz schön komischen Bug. C++ ( gcc Version 4.3.3) rundet die Werte, wenn ich einen Double in einen Integer umwandle.</p>
<p>Hier der Code von meiner Beispielklasse (Bruchrechnen Simulieren)</p>
<pre><code class="language-cpp">Bruch::Bruch(const double val)
{
	int ganzZahl = val;   // BEISPIEL 2.8  ==&gt; ganzZahl = 2
	int nachkomma = (val - ganzZahl) * 10;  // Problem: nachKomma = 7?? nicht 8 ???

	std::cout &lt;&lt; &quot; Ganzzahl: &quot; &lt;&lt; ganzZahl &lt;&lt; &quot;\t nachkomma: &quot; &lt;&lt; nachkomma &lt;&lt; std::endl;

	if(nachkomma == 0)
	{
		this-&gt;nenner = 1;
		this-&gt;zaehler = ganzZahl;
	}
	else
	{
		this-&gt;nenner = nachkomma;
		this-&gt;zaehler = (ganzZahl *  nachkomma)+1;
	}
}
</code></pre>
<p>Woran könnte das liegen?</p>
<p>Vielen Dank schonmal für die Antworten im Voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Drake</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794893</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794893</guid><dc:creator><![CDATA[Drake0815]]></dc:creator><pubDate>Mon, 19 Oct 2009 15:12:02 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Rundet die Werte? on Mon, 19 Oct 2009 15:15:23 GMT]]></title><description><![CDATA[<p>Hab gerade noch etwas ausprobiert.</p>
<p>Wenn ich 2 Nachkommastellen angebe (2.34) dann kommt das gewünschte Ergebnis heraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794895</guid><dc:creator><![CDATA[Drake0815]]></dc:creator><pubDate>Mon, 19 Oct 2009 15:15:23 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Rundet die Werte? on Mon, 19 Oct 2009 15:20:07 GMT]]></title><description><![CDATA[<p>Wie sieht 0.8 in Binaerdarstellung aus? Wie sieht 10 in Fliesskommadarstellung aus?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794900</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 19 Oct 2009 15:20:07 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Rundet die Werte? on Mon, 19 Oct 2009 15:35:43 GMT]]></title><description><![CDATA[<p>Die Zahl 2.8 ist üblicherweise nicht exact durch ein double darstellbar.</p>
<pre><code>2,8 (dezimal) = 10,1100110011... (binär)
</code></pre>
<p>Es <em>kann</em> also wegen der endlichen Genauigkeit sein, dass die am dichtesten an 2.8 darstellbare Zahl etwas kleiner ist und val diesen Wert repräsentiert: val &lt; 2.8. Dann wäre (val-ganzZahl)*10 &lt; 8, was die 7 erklärt, denn bei der Konvertierung von double nach int werden die Nachkommastellen einfach abgeschnitten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794905</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794905</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Mon, 19 Oct 2009 15:35:43 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Rundet die Werte? on Mon, 19 Oct 2009 15:28:14 GMT]]></title><description><![CDATA[<p>Danke für die Antworten, hab ganz vergessen, dass der PC auch seine Grenzen hat ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794908</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794908</guid><dc:creator><![CDATA[Drake 0815]]></dc:creator><pubDate>Mon, 19 Oct 2009 15:28:14 GMT</pubDate></item></channel></rss>