<?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[Runden]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Funktion geschrieben, die eine Gleitkommazahl rundet. Hier ist der Code:;</p>
<pre><code class="language-cpp">int round (float Zahl)                                                           // Bsp. mit Pi (ca. 3.1415) //7.6
{
  vector &lt;int&gt;Test;
  Test.push_back (static_cast &lt;int&gt;(Zahl));                         //Test[0] ist 3 und Test[1] ist 4 //[0] = 7, [1] = 8
  Test.push_back (Test[0] + 1);

  if (Zahl - Test[0] &lt; Test[1] - Zahl)                                       //--&gt; 3.1415 - 3 = 0.1415; 4 - 3.1415 = 0.9695
  {  
    return Test[0];
  }
  else //if (Zahl - Test[0] &gt;= Test[1] - Zahl)
  {
    return Test[1];
  }
}
</code></pre>
<p>Am Beispiel von Pi funktioniert das alles ja noch, aber wenn ich dann beispielsweise<br />
das Ganze mit 7.6 mache liefert die Funktion mir 7 zurück, obwohl 8 richtig wäre.<br />
Hat irgendjemand eine Idee, was falsch ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/261091/runden</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 18:15:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/261091.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 13 Feb 2010 16:17:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:17:11 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Funktion geschrieben, die eine Gleitkommazahl rundet. Hier ist der Code:;</p>
<pre><code class="language-cpp">int round (float Zahl)                                                           // Bsp. mit Pi (ca. 3.1415) //7.6
{
  vector &lt;int&gt;Test;
  Test.push_back (static_cast &lt;int&gt;(Zahl));                         //Test[0] ist 3 und Test[1] ist 4 //[0] = 7, [1] = 8
  Test.push_back (Test[0] + 1);

  if (Zahl - Test[0] &lt; Test[1] - Zahl)                                       //--&gt; 3.1415 - 3 = 0.1415; 4 - 3.1415 = 0.9695
  {  
    return Test[0];
  }
  else //if (Zahl - Test[0] &gt;= Test[1] - Zahl)
  {
    return Test[1];
  }
}
</code></pre>
<p>Am Beispiel von Pi funktioniert das alles ja noch, aber wenn ich dann beispielsweise<br />
das Ganze mit 7.6 mache liefert die Funktion mir 7 zurück, obwohl 8 richtig wäre.<br />
Hat irgendjemand eine Idee, was falsch ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855087</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855087</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:17:11 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:24:21 GMT]]></title><description><![CDATA[<p>Die Implementierung ist ziemlich over-engineered. <code>std::vector</code> ist definitiv nicht angebracht hier.</p>
<p>Was spricht gegen</p>
<pre><code class="language-cpp">int Round(float decimal)
{
    return static_cast&lt;int&gt;(decimal + 0.5f);
}
</code></pre>
<p>Kommt halt drauf an, wie du negative Zahlen behandeln willst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855090</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855090</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:24:21 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:26:06 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int round (float Zahl)                                                           // Bsp. mit Pi (ca. 3.1415) //7.6
{
  assert(Zahl&gt;=0);
  return static_cast&lt;int&gt;(floor(zahl+0.5));
}
</code></pre>
<p>Da fühle ich mich sehr unwohl, wenn ein vector mitspielt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855091</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855091</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:26:06 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:27:09 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<pre><code class="language-cpp">return static_cast&lt;int&gt;(floor(zahl+0.5));
</code></pre>
</blockquote>
<p>Warum <code>floor()</code> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855092</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:27:09 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:43:19 GMT]]></title><description><![CDATA[<p>Hey Danke!!!</p>
<p>Auf die Idee bin ich garnicht gekommen. Warum ist Vector hier eigentlich nicht angebracht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855095</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855095</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:43:19 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:36:19 GMT]]></title><description><![CDATA[<p>C++H schrieb:</p>
<blockquote>
<p>Auf die Idee bin ich garnicht gekommen. Warum ist Vector hier eigentlich nicht angebracht?</p>
</blockquote>
<p>Weil <code>std::vector</code> schon nur einige Byte Speicheroverhead hat, ohne die eigentlichen Elemente. Dazu kommt die dynamische Speicheranforderung und Vergrösserung mit entsprechendem Umkopieren. Da der Standard-Heap-Allokator meistens auch übermässig viel Overhead hat und nicht für kleine Allokationen optimiert ist, hat man dort wieder Speicher verschwendet. Von der nötigen Zeit eines <code>new</code> und <code>delete</code> ganz zu schweigen. Alles total unnötig.</p>
<p>Merke dir einfach, dass Containerklassen nicht ganz leichtgewichtig sind und dazu eingesetzt werden sollten, um mehrere Elemente zu speichern. Ihre Stärken zahlen sich besonders bei vielen Elementen aus. Für so kleine Algorithmen nimmst du lieber einzelne automatische Variablen, oder wenns geht (und hier geht es) ein Einzeiler mit temporären Ausdrücken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855098</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855098</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:36:19 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:36:05 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<pre><code class="language-cpp">return static_cast&lt;int&gt;(floor(zahl+0.5));
</code></pre>
</blockquote>
<p>Warum <code>floor()</code> ?</p>
</blockquote>
<p>War zu faul zu überlegen, wohin der static_cast rundet.<br />
Wohin rundet er? Und was passiert bei Minuszahlen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855101</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:36:05 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:40:42 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Wohin rundet er?</p>
</blockquote>
<p>Bei positiven Zahlen werden doch einfach die Nachkommastellen abgeschnitten, oder?</p>
<p>volkard schrieb:</p>
<blockquote>
<p>Und was passiert bei Minuszahlen?</p>
</blockquote>
<p>Die hast du ja mit <code>assert()</code> ausgeschlossen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Naja, je nachdem will man Minuszahlen vielleicht anders behandeln. Von daher auch &quot;Kommt halt drauf an, wie du negative Zahlen behandeln willst&quot;. Ich bin mir aber nicht sicher, ob der Standard bei <code>static_cast</code> garantiert, dass negative Zahlen in eine bestimmte Richtung gerundet werden, vielleicht ist dazu wirklich <code>floor()</code> nötig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855103</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855103</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:40:42 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:44:49 GMT]]></title><description><![CDATA[<p>Trotzdem funktioniert es nicht, ich habe es auf Nexus Art gemacht, trotzdem bekomme ich 3 zurück.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

int round (float Zahl)
{
  return static_cast &lt;int&gt;(Zahl + 0.5);
}

int main ()
{
  int pi=3.9;
  cout &lt;&lt;round (pi);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855107</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:44:49 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 16:47:48 GMT]]></title><description><![CDATA[<p>C++H schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int pi=3.9;//löl
</code></pre>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1855109</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855109</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 13 Feb 2010 16:47:48 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 17:00:40 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>C++H schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int pi=3.9;//löl
</code></pre>
</blockquote>
</blockquote>
<p>Ja, ich weiß, dass Pi nicht 3.9 ist, aber ansonsten verstehe ich dein löl nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855113</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855113</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Sat, 13 Feb 2010 17:00:40 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 17:04:42 GMT]]></title><description><![CDATA[<p>C++H schrieb:</p>
<blockquote>
<p>Ja, ich weiß, dass Pi nicht 3.9 ist, aber ansonsten verstehe ich dein löl nicht.</p>
</blockquote>
<p>Schau doch mal den Datentypen an.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855119</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 13 Feb 2010 17:04:42 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 17:06:06 GMT]]></title><description><![CDATA[<p>Ich Idiot!!!!!!!!!!!!<br />
Warum merke Dinge nicht beim ersten mal?!<br />
Danke für den wichtigen Hinweis!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855121</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855121</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Sat, 13 Feb 2010 17:06:06 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 19:18:52 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int pi=3.9;
</code></pre>
<p>Ein guter Compiler hätte dir eine Warnung dafür ausgespuckt, schadet im allgemeinen nicht auch alle Warnungen aus deinem Projekt zu beheben.</p>
<p>warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855123</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855123</guid><dc:creator><![CDATA[JaykopX]]></dc:creator><pubDate>Sat, 13 Feb 2010 19:18:52 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 18:24:00 GMT]]></title><description><![CDATA[<p>JaykopX schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int pi=3.9;
</code></pre>
<p>Ein guter Compiler hätte dir eine Warnung dafür ausgespuckt, schadet im allgemeinen nicht auch alle Warnungen aus deinem Projekt zu entfernen.</p>
</blockquote>
<p>&quot;Error: Wrong value for 'pi'&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855151</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855151</guid><dc:creator><![CDATA[asdasd]]></dc:creator><pubDate>Sat, 13 Feb 2010 18:24:00 GMT</pubDate></item><item><title><![CDATA[Reply to Runden on Sat, 13 Feb 2010 18:28:27 GMT]]></title><description><![CDATA[<p>Wegen der Minus Angelegenheit, könnte man doch einfach folgendes machen:</p>
<pre><code class="language-cpp">int round(float Zahl)
{
  return static_cast&lt;int&gt;((Zahl &lt; 0) ? Zahl - 0.5 : Zahl + 0.5);
}
</code></pre>
<p>Oder liege ich da falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855153</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855153</guid><dc:creator><![CDATA[freaky]]></dc:creator><pubDate>Sat, 13 Feb 2010 18:28:27 GMT</pubDate></item></channel></rss>