<?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 bei der Berechnung von Pi]]></title><description><![CDATA[<p>Wie muss ich diese Schleife ändern, damit sie mir die Nachkommastellen mit ausgibt ? Hab es mit Double probiert aber ich bin nen bissl hilflos.. ( cpp noob )</p>
<pre><code>#include &lt;iostream&gt;

using namespace std;

int main()
{

    int z; // vllt double ? aber funzt au net
    int b;

    for (int i=3; i&lt;100; i++)
    {

        b=2;

        if(i==3)
        {
            z = 4-(4/3);
        }
        else
        {
            if((i % 2) == 0)
            {
            }
            else
            {
                b = b+1;

                if((b % 2) == 0)
                {
                    z = z - (4/i);
                }
                else
                {
                    z = z + (4/i);
                }
            }
        }

    }

    cout &lt;&lt; &quot;Pie ist: &quot; &lt;&lt; z &lt;&lt; endl;

    return 0;
    }
</code></pre>
<p>Liebe Grüße PapaSchlumpf</p>
<p>PS: Ich weiß es ist etwas umständlich gelöst ^^ aber von Visual Basic auf C++ ist syntaxmäßig doch eine heftige umstellung in vb wär das keine sache ^^ aber ich will mich mal an eine hochsprache tun <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_tongue"
      title=":P"
      alt="😛"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/220090/problem-bei-der-berechnung-von-pi</link><generator>RSS for Node</generator><lastBuildDate>Wed, 07 Oct 2026 11:00:44 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/220090.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 Aug 2008 19:55:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Mon, 11 Aug 2008 19:58:49 GMT]]></title><description><![CDATA[<p>Wie muss ich diese Schleife ändern, damit sie mir die Nachkommastellen mit ausgibt ? Hab es mit Double probiert aber ich bin nen bissl hilflos.. ( cpp noob )</p>
<pre><code>#include &lt;iostream&gt;

using namespace std;

int main()
{

    int z; // vllt double ? aber funzt au net
    int b;

    for (int i=3; i&lt;100; i++)
    {

        b=2;

        if(i==3)
        {
            z = 4-(4/3);
        }
        else
        {
            if((i % 2) == 0)
            {
            }
            else
            {
                b = b+1;

                if((b % 2) == 0)
                {
                    z = z - (4/i);
                }
                else
                {
                    z = z + (4/i);
                }
            }
        }

    }

    cout &lt;&lt; &quot;Pie ist: &quot; &lt;&lt; z &lt;&lt; endl;

    return 0;
    }
</code></pre>
<p>Liebe Grüße PapaSchlumpf</p>
<p>PS: Ich weiß es ist etwas umständlich gelöst ^^ aber von Visual Basic auf C++ ist syntaxmäßig doch eine heftige umstellung in vb wär das keine sache ^^ aber ich will mich mal an eine hochsprache tun <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_tongue"
      title=":P"
      alt="😛"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562907</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562907</guid><dc:creator><![CDATA[PapaSchlumpf]]></dc:creator><pubDate>Mon, 11 Aug 2008 19:58:49 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Mon, 11 Aug 2008 20:00:32 GMT]]></title><description><![CDATA[<p>Deine Überlegung, dass du vielleicht auf double umsteigen solltest, ist ja schon ganz richtig. Variablen vom Typ Integer speichern dir nur ganze Zahlen. Willst du reelle Zahlen verarbeiten, dann musst du double wählen oder halt float. Aber im Sinne der Genauigkeit würde ich eher immer double wählen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562911</guid><dc:creator><![CDATA[Berufspenner]]></dc:creator><pubDate>Mon, 11 Aug 2008 20:00:32 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Mon, 11 Aug 2008 20:04:12 GMT]]></title><description><![CDATA[<p>Wenn ich double nehme kommt aber trotzdem 3 raus.... und ich will sie ja eig auf 50 nachkommastellen... also ist mit der formel pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 blabla reicht es wenn ich einfach anstatt</p>
<pre><code>int z;
</code></pre>
<pre><code>double z;
</code></pre>
<p>nehme ? bei mir funzt das nicht</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562914</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562914</guid><dc:creator><![CDATA[PapaSchlumpf]]></dc:creator><pubDate>Mon, 11 Aug 2008 20:04:12 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Mon, 11 Aug 2008 20:10:30 GMT]]></title><description><![CDATA[<p>Naja, 50 Stellen wirst du in double eh nicht Speichern können. Dafür musst du anders an die Speicherung deiner Werte heran gehen oder dir vorgefertigten Bibliotheken für Rechnungen mit großen Zahlen wie GMP angucken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562920</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562920</guid><dc:creator><![CDATA[Berufspenner]]></dc:creator><pubDate>Mon, 11 Aug 2008 20:10:30 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Mon, 11 Aug 2008 20:13:56 GMT]]></title><description><![CDATA[<p>Es funktioniert ja nichtmal mit 2 Nachkommastellen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562927</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562927</guid><dc:creator><![CDATA[PapaSchlumpf]]></dc:creator><pubDate>Mon, 11 Aug 2008 20:13:56 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Mon, 11 Aug 2008 20:21:25 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

int main()
{

    double z(0.0); // vllt double ? aber funzt au net
    int b(0);

    for (int i=3; i&lt;100; i++)
    {

        b=2;

        if(i==3)
        {
            z = 4.0-(4.0/3.0);
        }
        else
        {
            if((i % 2) == 0)
            {
            }
            else
            {
                b = b+1;

                if((b % 2) == 0)
                {
                    z = z - (4.0/i);
                }
                else
                {
                    z = z + (4.0/i);
                }
            }
        }

    }

    cout &lt;&lt; &quot;Pie ist: &quot; &lt;&lt; z &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Damit klappt zumindest schonmal die formale Ausgabe auf dem Bildschirm. Aber irgendwie ergibt es trotzdem keine sinnvollen Ergebnisse <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1562929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562929</guid><dc:creator><![CDATA[Berufspenner]]></dc:creator><pubDate>Mon, 11 Aug 2008 20:21:25 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Tue, 12 Aug 2008 15:24:02 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">double pi(0.0);
for (int i=1;i&lt;=100;i+=2)pi+=(i&amp;2 ? -1 : 1)*4.0/i;
std::cout &lt;&lt; &quot;Pi ist: &quot; &lt;&lt; pi;
</code></pre>
<p><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>Ich würde dir empfehlen, deine Version mal mit dem Debugger durchzugehen, dann solltest du schnell merken, was da schief läuft.</p>
<p>Edit:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

int main()
{

    double z(0.0); // vllt double ? aber funzt au net
    int b(0);

    for (int i=3; i&lt;100; i++)
    {

        b=2; //Der Fehler liegt hier, b sollte nur einmal vor der Schleife initialisiert werden

        if(i==3)
        {
            z = 4.0-(4.0/3.0); //da das nur einmalig gemacht wird, kann (und sollte) man das ausserhalb der Schleife machen
        }
        else
        {
            if((i % 2) == 0) //dann doch lieber gleich (i % 2) != 0... ausserdem kannst du die Schleife ohnehin gleich in
            {                //Zweierschritten durchgehen, dann sparst du dir das alles
            }
            else
            {
                b = b+1;

                if((b % 2) == 0)
                {
                    z = z - (4.0/i);
                }
                else
                {
                    z = z + (4.0/i);
                }
            }
        }

    }

    cout &lt;&lt; &quot;Pie ist: &quot; &lt;&lt; z &lt;&lt; endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1563446</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1563446</guid><dc:creator><![CDATA[Nanyuki]]></dc:creator><pubDate>Tue, 12 Aug 2008 15:24:02 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei der Berechnung von Pi on Tue, 12 Aug 2008 16:46:33 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt; 
using namespace std; 

int main() 
  { 
    double pi (4-4.0/3.0);
    bool is_even (false);

    for (int i (5); i &lt; 100; i += 2, is_even ^= is_even)
      {
        const double temp (4.0 / static_cast &lt;double&gt; (i));
        if (is_even)
          {
            z -= temp;
          }
          else
            {
              z += temp;
            }
      }
    cout &lt;&lt; &quot;Pi ist: &quot; &lt;&lt; pi &lt;&lt; endl;
  }
</code></pre>
<p>Ich hoffe, ich hab den Algorithmus an sich jetzt nicht kaputt gemacht ^^<br />
Aber mir gefällts so ein wenig mehr ^^</p>
<p>der Vollständigkeit halber noch mal dein Fehler:</p>
<blockquote>
<p>z = z - (4.0**/i**);</p>
</blockquote>
<p>bb</p>
<p>edit: hab ma auch double genommen - hatte aus gewohnheit float ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1563526</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1563526</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Tue, 12 Aug 2008 16:46:33 GMT</pubDate></item></channel></rss>