<?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[mathematisches (?) Problem]]></title><description><![CDATA[<p>Hallo, ich hab' ein merkwürdiges Problem mit dem BCB 5, bin nicht sicher ob es etwas mit dem Cast zu tun hat.</p>
<pre><code>double zahl = log(64)/log(2)+1; // zahl = 7
double zahl2 = (zahl-4)*2; // zahl2 = 6
int zahl3 = zahl2; // zahl3 = 5
ShowMessage (zahl3);
</code></pre>
<p>warum ist zahl3 = 5? in zahl2 steht doch eigentlich eine Ganzzahl, die man einem Integer zuweisen können müsste?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/120394/mathematisches-problem</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Jul 2026 22:46:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/120394.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 Sep 2005 18:21:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to mathematisches (?) Problem on Mon, 12 Sep 2005 18:21:39 GMT]]></title><description><![CDATA[<p>Hallo, ich hab' ein merkwürdiges Problem mit dem BCB 5, bin nicht sicher ob es etwas mit dem Cast zu tun hat.</p>
<pre><code>double zahl = log(64)/log(2)+1; // zahl = 7
double zahl2 = (zahl-4)*2; // zahl2 = 6
int zahl3 = zahl2; // zahl3 = 5
ShowMessage (zahl3);
</code></pre>
<p>warum ist zahl3 = 5? in zahl2 steht doch eigentlich eine Ganzzahl, die man einem Integer zuweisen können müsste?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870511</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870511</guid><dc:creator><![CDATA[Dopefish]]></dc:creator><pubDate>Mon, 12 Sep 2005 18:21:39 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Mon, 12 Sep 2005 19:16:52 GMT]]></title><description><![CDATA[<p>zahl2 ist wahrscheinlich ein, zwei Mü kleiner!<br />
5.999999999999...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870545</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870545</guid><dc:creator><![CDATA[DerAltenburger]]></dc:creator><pubDate>Mon, 12 Sep 2005 19:16:52 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Mon, 12 Sep 2005 19:38:22 GMT]]></title><description><![CDATA[<p>dieses bietet:</p>
<pre><code>int main(){
	unsigned double zahl = log(64)/log(2)+1; // zahl = 6
	printf(&quot;%d\n&quot;, zahl);
	unsigned double zahl2 = (zahl-4)*2; // zahl2 = 4
	printf(&quot;%d\n&quot;, zahl2);
	int zahl3 = zahl2; // zahl3 = 4
	printf(&quot;%d\n&quot;, zahl3);
}
</code></pre>
<p>aber nur in der unsigned variante, ansonsten landest du im minus bereich</p>
<p>ps: ich hab extra mal geschaut, was %d, also dezimal da bringt.. log ist zu ungenau.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870558</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870558</guid><dc:creator><![CDATA[elise]]></dc:creator><pubDate>Mon, 12 Sep 2005 19:38:22 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Mon, 12 Sep 2005 21:06:40 GMT]]></title><description><![CDATA[<p>Also grundsätzlich erstmal:<br />
Das Ergebnis müsste natürlich 6 sein, man kann das ja mal Schritt für Schritt ausrechnen (es kommen nur ganzzahlige Ergebnisse dabei raus). Es darf mathematisch nicht sein, dass das Ergebnis um ein Mü kleiner ist. Es ist ja auch bis zu der Stelle richtig, an der nach int gecastet wird (den int brauche ich für einen Array-Zugriff).</p>
<p>Der Taschenrechner und Dev-Cpp liefern auch das richtige Ergebnis, nur der BCB verrechnet sich (übrigens auch nur beim log-Wert von 64, bei allen anderen funktioniert es).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870605</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870605</guid><dc:creator><![CDATA[Dopefish]]></dc:creator><pubDate>Mon, 12 Sep 2005 21:06:40 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Tue, 13 Sep 2005 07:19:22 GMT]]></title><description><![CDATA[<p>Dieses Problem ist C / C++ spezifisch es gibt einfach bestimmte Nachkommawerte, die sich in Fließkommadatentypen nicht abbilden lassen. Welche Werte das sind, ist Datentypabhängig.<br />
Das war schon immer so und wird immer so sein. Deswegen: Zum Schätzen nimmt man Fließkommadatentypen, zum Rechnen Intergerdatentypen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870770</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870770</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 13 Sep 2005 07:19:22 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Tue, 13 Sep 2005 08:25:55 GMT]]></title><description><![CDATA[<p>Joe_M. schrieb:</p>
<blockquote>
<p>Dieses Problem ist C / C++ spezifisch es gibt einfach bestimmte Nachkommawerte, die sich in Fließkommadatentypen nicht abbilden lassen. Welche Werte das sind, ist Datentypabhängig.<br />
Das war schon immer so und wird immer so sein. Deswegen: Zum Schätzen nimmt man Fließkommadatentypen, zum Rechnen Intergerdatentypen.</p>
</blockquote>
<p>Es ist natürlich klar, dass beim Umwandeln von Typen Datenverlust entsteht, in diesem Fall ist es aber unlogisch, weil an keiner Stelle im Code (als Ergebnis) eine wirkliche Fließkommazahl entsteht (log(64)/log(2) = 6, +1 = 7, -4 = 3, *2 = 6). Wo soll da die 5 herkommen?</p>
<p>Naja, ist ja auch egal, kann mir vielleicht jemand einen Tipp geben, wie bei der Rechnung auch wirklich 6 rauskommt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870837</guid><dc:creator><![CDATA[Dopefish]]></dc:creator><pubDate>Tue, 13 Sep 2005 08:25:55 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Tue, 13 Sep 2005 08:56:09 GMT]]></title><description><![CDATA[<p>wenn du einen integralen datentyp erwartest, solltest du das ergebnis runden und dann zuweisen.</p>
<pre><code class="language-cpp">#define MF_LN_2  0.69314718055994530941723212145818 // log(2) (nat. Log.)

//---------------------------------------------------------------------------
// Funktion liefert dualen Logarithmus von 'x' (Basis 2)
//---------------------------------------------------------------------------
inline double Log2(const double &amp;x)
{
  return (log(x) / MF_LN_2);
}

//---------------------------------------------------------------------------
// Funktion rundet die angegebene Gleitkommazahl auf die nächste
// Ganzzahl (integer) auf oder ab.
//---------------------------------------------------------------------------
inline int Round(const double &amp;x)
{
  return ((int) (x &lt; 0 ? (x - 0.5) : (x + 0.5)));
}
</code></pre>
<pre><code class="language-cpp">double v1 = Log2(64);         // v = 6
int    v2 = Round(Log2(64));  // v = 6
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/870851</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870851</guid><dc:creator><![CDATA[Sunday]]></dc:creator><pubDate>Tue, 13 Sep 2005 08:56:09 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Tue, 13 Sep 2005 09:52:05 GMT]]></title><description><![CDATA[<p>Joe_M. schrieb:</p>
<blockquote>
<p>Dieses Problem ist C / C++ spezifisch</p>
</blockquote>
<p>Nein, das Problem ist nicht C/C++ spezifisch, dasselbe Problem hat man auch bei Delphi, VB o.ä.<br />
Das liegt an der internen Darstellung von Fließkommazahlen und so ziemlich jeder Programmierer<br />
(mich eingeschlossen) ist damit wohl schon mal auf die Schnauze gefallen.</p>
<p>Man muss halt bei der Verwendung von Fließkommazahlen immer daran denken, dass die Genauigkeit<br />
beschränkt ist. Bitweises Vergleichen ist kritisch und die Umwandlung in Ganzzahlen offensichtlich<br />
auch.</p>
<p>Wie Altenburger schon angemerkt hat ist der Ganzzahlwert der berechneten Fließkommazahl wohl 5 und<br />
der Rest wird beim Cast einfach abgeschnitten.</p>
<p>Gruß,</p>
<p>Alexander</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870918</guid><dc:creator><![CDATA[Alexander Kempf]]></dc:creator><pubDate>Tue, 13 Sep 2005 09:52:05 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Tue, 13 Sep 2005 10:34:42 GMT]]></title><description><![CDATA[<p>elise schrieb:</p>
<blockquote>
<pre><code>unsigned double zahl = log(64)/log(2)+1; // zahl = 6
</code></pre>
</blockquote>
<p>seit wann gibts <strong>unsigned</strong> auch fuer gleitkommatypen? meines wissens gilt das nur bei ganzzahltypen (char, int, long, short, __int64).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870957</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870957</guid><dc:creator><![CDATA[Sunday]]></dc:creator><pubDate>Tue, 13 Sep 2005 10:34:42 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Tue, 13 Sep 2005 17:26:11 GMT]]></title><description><![CDATA[<p>Ok, ich hab' es inzwischen hinbekommen, danke. <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>Eines würde mich aber doch interessieren:</p>
<blockquote>
<p>Wie Altenburger schon angemerkt hat ist der Ganzzahlwert der berechneten Fließkommazahl wohl 5 und<br />
der Rest wird beim Cast einfach abgeschnitten.</p>
</blockquote>
<p>Wie kommt man da auf 5? Das Ergebnis ist genau 6. <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 />
log(64)/log(2) ist genau 6 und der Rest ist ja wohl auch klar! Ich verstehe nicht, wie da was verloren gehen kann, sogar mein Taschenrechner kommt auf das richtige Ergebnis.... ich hab' auch mal nachgerechnet (einzelne Werte von den Logarithmen). wenn man Nachkommastellen abschneidet müsste eigentlich ein Wert über 6 rauskommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/871372</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/871372</guid><dc:creator><![CDATA[Dopefish]]></dc:creator><pubDate>Tue, 13 Sep 2005 17:26:11 GMT</pubDate></item><item><title><![CDATA[Reply to mathematisches (?) Problem on Tue, 13 Sep 2005 18:32:29 GMT]]></title><description><![CDATA[<p>Da hilft wohl nur, dass Du mal in die Welt der Fließkommazahlen (oder anscheinend besser Gleitkomma-<br />
zahlen) einsteigst. Das lernt man beispielsweise in den Grundlagenvorlesungen im Informatikstudium.<br />
Falls Du keine Lust hast Informatik zu studieren, kannst Du auch mal <a href="http://www.computerbase.de/lexikon/Flie%C3%9Fkomma-Zahlen" rel="nofollow">hier</a> schauen.<br />
Wenn Du dann immer noch nicht weißt, warum das Ergebnis nicht genau 6 ist, musst Du halt doch<br />
Informatik studieren <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>Gruß,</p>
<p>Alexander</p>
]]></description><link>https://www.c-plusplus.net/forum/post/871429</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/871429</guid><dc:creator><![CDATA[Alexander Kempf]]></dc:creator><pubDate>Tue, 13 Sep 2005 18:32:29 GMT</pubDate></item></channel></rss>