<?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[Problem beim Runden von 1.025 auf 2 Nachkommastellen]]></title><description><![CDATA[<p>hi,</p>
<p>folgende funktion rundet mir eine zahl (double) auf eine anzahl von nachkommastellen.</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------
// LUT für 10^x (max. x = 9)
//---------------------------------------------------------------------------
static const int Power10_LUT[] = {        1,         10,
                                        100,       1000,
                                      10000,     100000,
                                    1000000,   10000000,
                                  100000000, 1000000000 };
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Funktion rundet die angegebene Gleitkommazahl auf die nächste
// Ganzzahl (integer) auf oder ab.
//---------------------------------------------------------------------------
template &lt;&gt; 
inline int Round(const double Value)
{
  return ((int) (Value &lt; 0 ? (Value - 0.5) : (Value + 0.5)));
}

//---------------------------------------------------------------------------
// Funktion rundet die angegebene Gleitkommazahl auf die gewünschte
// Anzahl an Nachkommastellen.
//---------------------------------------------------------------------------
template &lt;&gt; 
inline double Round(const double Value, const u_int Precision)
{
  int Power10;

  if (Precision &gt; 9) Power10 = pow(10.0, (double) Precision);
  else               Power10 = *(Power10_LUT + Precision);

  return ((double) Round(Value * Power10)) / Power10;
}
</code></pre>
<p>wenn ich nun folgendes aufrufe</p>
<pre><code class="language-cpp">double zahl = 1.025
double rnd  = Round(zahl, 2);
</code></pre>
<p>erhalte ich <strong>1.02</strong> als ergebnis obwohl dieses ja <strong>1.03</strong> lauten müsste. der rückgabewert von <strong>Round(Value * Power10)</strong> beträgt auch korrekterweise <strong>103</strong>. nach der division durch 100 (für 2 nachkommastellen) kommt dann aber 1.02 raus.</p>
<p>wenn man statt 1.025 z.b. <strong>1.0250000001</strong> nimmt, kommt auch 1.03 raus.</p>
<p>woran kanns liegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/93758/problem-beim-runden-von-1-025-auf-2-nachkommastellen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Jul 2026 13:32:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/93758.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 02 Dec 2004 10:46:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem beim Runden von 1.025 auf 2 Nachkommastellen on Thu, 02 Dec 2004 10:46:16 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>folgende funktion rundet mir eine zahl (double) auf eine anzahl von nachkommastellen.</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------
// LUT für 10^x (max. x = 9)
//---------------------------------------------------------------------------
static const int Power10_LUT[] = {        1,         10,
                                        100,       1000,
                                      10000,     100000,
                                    1000000,   10000000,
                                  100000000, 1000000000 };
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Funktion rundet die angegebene Gleitkommazahl auf die nächste
// Ganzzahl (integer) auf oder ab.
//---------------------------------------------------------------------------
template &lt;&gt; 
inline int Round(const double Value)
{
  return ((int) (Value &lt; 0 ? (Value - 0.5) : (Value + 0.5)));
}

//---------------------------------------------------------------------------
// Funktion rundet die angegebene Gleitkommazahl auf die gewünschte
// Anzahl an Nachkommastellen.
//---------------------------------------------------------------------------
template &lt;&gt; 
inline double Round(const double Value, const u_int Precision)
{
  int Power10;

  if (Precision &gt; 9) Power10 = pow(10.0, (double) Precision);
  else               Power10 = *(Power10_LUT + Precision);

  return ((double) Round(Value * Power10)) / Power10;
}
</code></pre>
<p>wenn ich nun folgendes aufrufe</p>
<pre><code class="language-cpp">double zahl = 1.025
double rnd  = Round(zahl, 2);
</code></pre>
<p>erhalte ich <strong>1.02</strong> als ergebnis obwohl dieses ja <strong>1.03</strong> lauten müsste. der rückgabewert von <strong>Round(Value * Power10)</strong> beträgt auch korrekterweise <strong>103</strong>. nach der division durch 100 (für 2 nachkommastellen) kommt dann aber 1.02 raus.</p>
<p>wenn man statt 1.025 z.b. <strong>1.0250000001</strong> nimmt, kommt auch 1.03 raus.</p>
<p>woran kanns liegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/664302</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/664302</guid><dc:creator><![CDATA[Sunday]]></dc:creator><pubDate>Thu, 02 Dec 2004 10:46:16 GMT</pubDate></item><item><title><![CDATA[Reply to Problem beim Runden von 1.025 auf 2 Nachkommastellen on Thu, 02 Dec 2004 12:21:40 GMT]]></title><description><![CDATA[<p>wenn ich das ganze mit float oder long double mache klappt es.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/664404</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/664404</guid><dc:creator><![CDATA[Sunday]]></dc:creator><pubDate>Thu, 02 Dec 2004 12:21:40 GMT</pubDate></item><item><title><![CDATA[Reply to Problem beim Runden von 1.025 auf 2 Nachkommastellen on Thu, 02 Dec 2004 12:25:52 GMT]]></title><description><![CDATA[<p>eventuell bekommst du da rundungsfehler durch pow...</p>
<p>wiso machst du die sache eigentlich so kompliziert.. multipliziere doch einfach für die anzahl an stellen die du haben willst jeweils mit 10... dann addierst du 0.5 und castest nacht int ... und danach wieder anzahl der stellen mal durch 10 teilen und fertig ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/664408</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/664408</guid><dc:creator><![CDATA[Windalf]]></dc:creator><pubDate>Thu, 02 Dec 2004 12:25:52 GMT</pubDate></item><item><title><![CDATA[Reply to Problem beim Runden von 1.025 auf 2 Nachkommastellen on Thu, 02 Dec 2004 12:27:55 GMT]]></title><description><![CDATA[<p>1.025 kann ja nicht genau gespeichert werden, vermutlich ist für double sowas wie<br />
1.0249999999....<br />
am nähesten dran, damit zu 1.02 gerundet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/664411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/664411</guid><dc:creator><![CDATA[Geo]]></dc:creator><pubDate>Thu, 02 Dec 2004 12:27:55 GMT</pubDate></item><item><title><![CDATA[Reply to Problem beim Runden von 1.025 auf 2 Nachkommastellen on Thu, 02 Dec 2004 13:42:24 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/5110">@windalf</a></p>
<p>das mache ich doch auch so!</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/1152">@geo</a></p>
<p>ja in der tat, wenn ich long double nehme, dann ist 1.025 -&gt; 1.024999999... was dann natürlich 1.02 ergibt. wenn ich noch den suffix l anhänge (1.025l), dann funktioniert das ganze für long double.</p>
<p>krieg noch mal die krise mit den gleitkomma-berechnungen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/664462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/664462</guid><dc:creator><![CDATA[Sunday]]></dc:creator><pubDate>Thu, 02 Dec 2004 13:42:24 GMT</pubDate></item><item><title><![CDATA[Reply to Problem beim Runden von 1.025 auf 2 Nachkommastellen on Fri, 03 Dec 2004 06:22:23 GMT]]></title><description><![CDATA[<p>Wie hat ein Buchautor aus dem Delphi-Bereich mal geschrieben: Fließkommazahlen nimmt man zum Schätzen, Ganzzahlen zum Messen...</p>
<p>Indem Du den Datentyp änderst, velagerst Du das Problem nur auf andere Werte, die dann nicht korrekt abgebildet werden können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/664889</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/664889</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 03 Dec 2004 06:22:23 GMT</pubDate></item><item><title><![CDATA[Reply to Problem beim Runden von 1.025 auf 2 Nachkommastellen on Fri, 03 Dec 2004 10:01:01 GMT]]></title><description><![CDATA[<blockquote>
<p>das mache ich doch auch so!</p>
</blockquote>
<p>Ich meinte damit eher verwende kein pow... ist unnötig...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/665002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/665002</guid><dc:creator><![CDATA[Windalf]]></dc:creator><pubDate>Fri, 03 Dec 2004 10:01:01 GMT</pubDate></item></channel></rss>