<?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[Frage zu &amp;quot;return&amp;quot;]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich versuche mich gerade daran, ein paar numerische Algorithmen zu implementieren, um ein wenig Übung zu bekommen. Nun habe ich den folgenden Quellcode:</p>
<pre><code class="language-cpp">void newt(double x_0)
{
    double x_n = 0, y_test = fct(x_0);

    if(y_test &lt; 1.e-30)
    {
        if(x_0 &lt; 1.e30 &amp;&amp; x_0 &gt; -1.e30)
        {
                x_0 = 0;
                cout &lt;&lt; &quot;Nullstelle bei x = &quot; &lt;&lt; x_0 &lt;&lt; endl;
        }
        else
                cout &lt;&lt; &quot;Nullstelle bei x = &quot; &lt;&lt; x_0 &lt;&lt; endl;
    }
    else
    {
        x_n = x_0 - fct(x_0)/der(x_0);
        newt(x_n);
    }
}
</code></pre>
<p>Wie man unschwer erkennen kann, handelt es sich hierbei um das Newton-Verfahren, mit dem man Nullstellen approximieren kann. Die Funktion an sich funktioniert in der Form schon sehr gut, jedoch dachte ich mir, dass ich flexibler arbeiten kann, wenn ich die Ausgaben etc. auf die main-Funktion auslagere. So habe ich die folgenden Veränderungen vorgenommen:</p>
<pre><code class="language-cpp">double newt(double x_0) // Ausführen des Newtonverfahrens
{
    double x_n = 0, y_test = fct(x_0);

    if(y_test &lt; 1.e-30)
    {
        if(x_0 &lt; 1.e30 &amp;&amp; x_0 &gt; -1.e30)
        {
                x_0 = 0;
                return 0;
        }
        else
        {
                return x_0;
        }
    }
    else
    {
        x_n = x_0 - fct(x_0)/der(x_0);
        newt(x_n);
    }
}
</code></pre>
<p>und die main() entsprechend:</p>
<pre><code class="language-cpp">int main(int argc, char** argv) {

    double erg = 0, x_0 = 4;

    erg = newt(x_0);
    cout &lt;&lt; erg &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Leider übergibt die Funktion einen falschen Wert. So habe ich, insofern ich mir den letzten Wert, der von der Funktion newt() berechnet wird, direkt von dieser Funktion aufs Terminal ausgeben lasse, die richtige Nullstelle als Ergebnis. Gebe ich mir die Variable &quot;erg&quot; in der main()-Funktion aus, erscheint nur &quot;nan&quot; in der Konsole, offenbar geht also etwas bei der Übergabe schief.</p>
<p>Kann mir wer erklären, was da falsch ist?</p>
<p>Viele Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/304409/frage-zu-quot-return-quot</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 18:45:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/304409.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 05 Jun 2012 14:59:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 14:59:41 GMT]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich versuche mich gerade daran, ein paar numerische Algorithmen zu implementieren, um ein wenig Übung zu bekommen. Nun habe ich den folgenden Quellcode:</p>
<pre><code class="language-cpp">void newt(double x_0)
{
    double x_n = 0, y_test = fct(x_0);

    if(y_test &lt; 1.e-30)
    {
        if(x_0 &lt; 1.e30 &amp;&amp; x_0 &gt; -1.e30)
        {
                x_0 = 0;
                cout &lt;&lt; &quot;Nullstelle bei x = &quot; &lt;&lt; x_0 &lt;&lt; endl;
        }
        else
                cout &lt;&lt; &quot;Nullstelle bei x = &quot; &lt;&lt; x_0 &lt;&lt; endl;
    }
    else
    {
        x_n = x_0 - fct(x_0)/der(x_0);
        newt(x_n);
    }
}
</code></pre>
<p>Wie man unschwer erkennen kann, handelt es sich hierbei um das Newton-Verfahren, mit dem man Nullstellen approximieren kann. Die Funktion an sich funktioniert in der Form schon sehr gut, jedoch dachte ich mir, dass ich flexibler arbeiten kann, wenn ich die Ausgaben etc. auf die main-Funktion auslagere. So habe ich die folgenden Veränderungen vorgenommen:</p>
<pre><code class="language-cpp">double newt(double x_0) // Ausführen des Newtonverfahrens
{
    double x_n = 0, y_test = fct(x_0);

    if(y_test &lt; 1.e-30)
    {
        if(x_0 &lt; 1.e30 &amp;&amp; x_0 &gt; -1.e30)
        {
                x_0 = 0;
                return 0;
        }
        else
        {
                return x_0;
        }
    }
    else
    {
        x_n = x_0 - fct(x_0)/der(x_0);
        newt(x_n);
    }
}
</code></pre>
<p>und die main() entsprechend:</p>
<pre><code class="language-cpp">int main(int argc, char** argv) {

    double erg = 0, x_0 = 4;

    erg = newt(x_0);
    cout &lt;&lt; erg &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Leider übergibt die Funktion einen falschen Wert. So habe ich, insofern ich mir den letzten Wert, der von der Funktion newt() berechnet wird, direkt von dieser Funktion aufs Terminal ausgeben lasse, die richtige Nullstelle als Ergebnis. Gebe ich mir die Variable &quot;erg&quot; in der main()-Funktion aus, erscheint nur &quot;nan&quot; in der Konsole, offenbar geht also etwas bei der Übergabe schief.</p>
<p>Kann mir wer erklären, was da falsch ist?</p>
<p>Viele Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219849</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219849</guid><dc:creator><![CDATA[Zateha]]></dc:creator><pubDate>Tue, 05 Jun 2012 14:59:41 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 15:02:36 GMT]]></title><description><![CDATA[<p>Was gibt die Funktion denn zurück, wenn der Test in Zeile 5 false ist? Suche mal nach rekursion. <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/2219851</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219851</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 05 Jun 2012 15:02:36 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 15:08:27 GMT]]></title><description><![CDATA[<p>Hi!</p>
<p>Whups, das habe ich glatt übersehen. Habe das jetzt weiter eingegrenzt, sodass nicht alles unter der x-Achse auch als Nullstelle gewertet wird. *g*</p>
<p>Wie dem auch sei, leider gibt er mir bei der Übergabe an die main() weiterhin &quot;nan&quot; aus, während die direkte Ausgabe in newt() weiterhin das richtige Ergebnis liefert. :S</p>
<p>Viele Grüße und danke für die Antwort!</p>
<p>Edit: hab dein Edit nicht rechtzeitig gesehen, daher schiebe ich hier mein Edit nach: Gute Frage, wahrscheinlich &quot;nichts&quot;, im wahrsten Sinne, denn für den Fall ist ja kein return eingetragen. Guter Punkt eigentlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219853</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219853</guid><dc:creator><![CDATA[Zateha]]></dc:creator><pubDate>Tue, 05 Jun 2012 15:08:27 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 15:09:26 GMT]]></title><description><![CDATA[<p>Zeig doch noch mal deinen Code.<br />
Edit: Jetzt habe ich dein Edit nicht gesehen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219857</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219857</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 05 Jun 2012 15:09:26 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 15:14:22 GMT]]></title><description><![CDATA[<p>Hehe.</p>
<p>Ich frage mich gerade, was ich da als return eintragen kann, denn der Algorithmus durchläuft das Ding ja rekursiv und soll ja nicht nach jedem Rekusrionsschritt einen Wert zurückgeben, sondern erst dann, wenn die Nullstelle tatsächlich gefunden ist. Kann man dem irgendwie sagen &quot;gib erstmal nichts zurück und warte, bis die NST gefunden ist&quot;?</p>
<p>Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219861</guid><dc:creator><![CDATA[Zateha]]></dc:creator><pubDate>Tue, 05 Jun 2012 15:14:22 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 15:20:12 GMT]]></title><description><![CDATA[<p>Zurückgegeben werden nicht Variablen, sondern Werte.. und zurückgegeben wird immer an den Aufrufer, das muss nicht zwangsläufig die main Funktion sein.. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f4a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--light_bulb"
      title=":bulb:"
      alt="💡"
    /><br />
(Einfach die Lösung sagen wäre ja langweilig. :p)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219865</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219865</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 05 Jun 2012 15:20:12 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 15:44:12 GMT]]></title><description><![CDATA[<p>Heureka!</p>
<pre><code class="language-cpp">double newt(double x_0) // Ausführen des Newtonverfahrens
{
    double x_n = 0, y_test = fct(x_0), erg = 0;

    if(y_test &lt; 1.e-30 &amp;&amp; y_test &gt; -1.e-30)
    {
        return x_0;
    }
    x_n = x_0 - fct(x_0)/der(x_0);

    return newt(x_n);
}
</code></pre>
<p>Scheint zu funktionieren!</p>
<p>Vielen Dank für die Hinweise!</p>
<p>Tante Edit: Grade noch nen Absolutbetrag in der Bedingung spendiert, das sieht hübscher aus. <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2219877</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219877</guid><dc:creator><![CDATA[Zateha]]></dc:creator><pubDate>Tue, 05 Jun 2012 15:44:12 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 17:05:59 GMT]]></title><description><![CDATA[<p>Und da bin ich wieder. Diesmal mit einem, wahrscheinlich, noch trivialerem Problem. Ich scheine den Wald vor lauter Bäumen nicht zu sehen.</p>
<p>Folgendes Problem: Um von Funktionen mehrere Nullstellen bestimmen zu können, ohne das Programm mehrfach ausführen zu müssen, habe ich eine while()-Schleife in die main()-Funktion eingebaut, die quasi kleine Teilintervalle auf Nullstellen überprüft. Dazu der folgende Quellcode:</p>
<pre><code class="language-cpp">int main(int argc, char** argv)
{
    double from = 0, to = 0, erg = 0, erg_1 = 1.e200, step = 0;
    ofstream ausgabe;

    /* Benutzereingaben */
    cout &lt;&lt; &quot;Parametereingabe:&quot; &lt;&lt; endl;

    cout &lt;&lt; &quot;Startwert: &quot;;
    cin &gt;&gt; from;
    cout &lt;&lt; &quot;Endwert: &quot;;
    cin &gt;&gt; to;
    cout &lt;&lt; &quot;Schrittweite: &quot;;
    cin &gt;&gt; step;

    /* Ausführen des Algorithmus in kleinen Teilintervallen */
    while(from &lt;= to)
    {     
        erg = newt(from); // Finden der Nullstellen einer Funktion

        if(erg != erg_1) // Nur neue Ergebnisse ausgeben
        {
                cout &lt;&lt; &quot;Nullstelle bei x = &quot; &lt;&lt; erg &lt;&lt; endl; // Ausgabe der Nullstelle auf das Terminal

                /* Dateioperationen */
                ausgabe.open(&quot;nullstellen.dat&quot;,ios::app); // Ausgabedatei öffnen
                if(ausgabe.is_open()) // Nur schreiben, wenn die Datei geöffnet ist
                        ausgabe &lt;&lt; erg &lt;&lt; endl; // Nullstelle in Datei schreiben
                else
                    cout &lt;&lt; &quot;Datei kann nicht aufgerufen werden!&quot; &lt;&lt; endl;
                ausgabe.close(); // Ausgabedatei schließen
        }
        erg_1 = erg;
        from += step;
    }

    return 0;
}
</code></pre>
<p>Dummerweise scheint die Schleife abzubrechen, sobald eine Nullstelle gefunden wurde, woran kann das liegen? Meiner Ansicht nach ist die Exit-Bedingung doch eigentlich eindeutig.</p>
<p>Viele Grüße!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219910</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219910</guid><dc:creator><![CDATA[Zateha]]></dc:creator><pubDate>Tue, 05 Jun 2012 17:05:59 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 17:21:29 GMT]]></title><description><![CDATA[<p>Fehler liegt woanders, es wird nur eine Nullstelle gefunden. Der Code ist so korrekt. (Nicht unbedingt schön. :p)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219917</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219917</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 05 Jun 2012 17:21:29 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Tue, 05 Jun 2012 17:25:19 GMT]]></title><description><![CDATA[<p>Hmm, dann suche ich mal die restlichen Funktionen ab. Wie kann man denn, nur aus Interesse, schöner coden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2219919</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2219919</guid><dc:creator><![CDATA[Zateha]]></dc:creator><pubDate>Tue, 05 Jun 2012 17:25:19 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 12:20:11 GMT]]></title><description><![CDATA[<p>Kleiner Tipp:</p>
<p>ich würde deine Funktion so umschreiben:</p>
<pre><code class="language-cpp">double newt(double x_0, double(*fct)(double)) // Ausführen des Newtonverfahrens
{
    double x_n = 0, y_test = fct(x_0), erg = 0;

    if(y_test &lt; 1.e-30 &amp;&amp; y_test &gt; -1.e-30)
    {
        return x_0;
    }
    x_n = x_0 - fct(x_0)/der(x_0);

    return newt(x_n);
}
</code></pre>
<p>Der zweite Parameter ist, die Funktion, die untersucht werden soll.</p>
<p>(Kleine Frage: was macht die Funktion <code>der()</code> ? Ist das die Ableitung? falls ja müsste die Funktion so aussehen:</p>
<pre><code class="language-cpp">double newt(double x_0, double(*fct)(double), double(*der)(double))
//...
</code></pre>
<p>)</p>
<p>Noch Fragen? Und glaub mir, so ist besser.</p>
<p>Und aufruf sieht so aus:</p>
<pre><code class="language-cpp">newt(x, &amp;funktion);     // Mit zwei Parametern und &quot;der()&quot; ist eine feste Funktion
newt(x, &amp;funktion, &amp;der);    //Mit drei Parametern und &quot;der()&quot; ist die Ableitung.
</code></pre>
<p>Viel Spaß damit!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220217</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 12:20:11 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 12:26:34 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Noch Fragen? Und glaub mir, so ist besser.</p>
</blockquote>
<p>Ja: Warum kein Template?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220220</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220220</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Wed, 06 Jun 2012 12:26:34 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 12:32:24 GMT]]></title><description><![CDATA[<p>Es geht hier um Funktionen.</p>
<p>Natürlich könnte man noch ein Template reinhauen, aber das wäre, glaube ich, zu viel.</p>
<p>Nagut, hier mit Template:</p>
<pre><code class="language-cpp">//Eine Funktion
template &lt;class T&gt; T newt(T x_0, T(*fct)(T))
{
    //...
}

//Zwei Funktionen:
template &lt;class T&gt; T newt(T x_0, T(*fct)(T), T(*der)(T))
{
    //...
}
</code></pre>
<p>Würde auch gehen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220226</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220226</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 12:32:24 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:19:14 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Es geht hier um Funktionen.</p>
<p>Natürlich könnte man noch ein Template reinhauen, aber das wäre, glaube ich, zu viel.</p>
<p>Nagut, hier mit Template:</p>
<p>Würde auch gehen...</p>
</blockquote>
<p>rofl, nein.</p>
<pre><code class="language-cpp">template &lt;typename Functor&gt;
double newt(double x_0, Functor fct)
{
    double x_n = 0, y_test = fct(x_0), erg = 0;

    if(y_test &lt; 1.e-30 &amp;&amp; y_test &gt; -1.e-30)
    {
        return x_0;
    }
    x_n = x_0 - fct(x_0)/der(x_0);

    return newt(x_n, fct);
}
</code></pre>
<p>Nicht nur übersichtlicher, sondern erzeugt auch noch sehr wahrscheinlich deutlich schnelleren Code. <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/2220233</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220233</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:19:14 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:01:26 GMT]]></title><description><![CDATA[<p>Das habe ich noch nicht gelernt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220244</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220244</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:01:26 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:03:06 GMT]]></title><description><![CDATA[<p>Danke für die Antworten, ich schau mir das mal in Ruhe an. <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="🙂"
    /><br />
Habe btw. herausgefunden, weshalb die Funktion nicht alle Nullstellen findet: Die numerische Berechnung der Ableitung zickt bei bestimmten Werten für &quot;dx&quot; ein wenig rum. Wenn man das variiert, funktioniert es.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220245</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220245</guid><dc:creator><![CDATA[Zateha]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:03:06 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:17:34 GMT]]></title><description><![CDATA[<p>Nur, um Cooky zu ärgern kommt jetzt die ultimative Variante der Funktion:</p>
<pre><code class="language-cpp">template &lt;typename T, typename Functor&gt;
T newt(T x_0, Functor fct, Functor der)
{
    double x_n = 0, y_test = fct(x_0), erg = 0;

    if(y_test &lt; 1.e-30 &amp;&amp; y_test &gt; -1.e-30)
    {
        return x_0;
    }
    x_n = x_0 - fct(x_0)/der(x_0);

    return newt(x_n);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2220253</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220253</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:17:34 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:18:46 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Nur, um Cooky zu ärgern kommt jetzt die ultimative Variante der Funktion:</p>
</blockquote>
<p>Die ist aber falsch. fct und der müssen ja nicht vom gleichen Typ sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220254</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220254</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:18:46 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:39:10 GMT]]></title><description><![CDATA[<p>sollten aber, weil der die Ableitung von fct ist...</p>
<p>Also doch richtig!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220259</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220259</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:39:10 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:45:25 GMT]]></title><description><![CDATA[<p>Hat das auch einen Sinn oder ist das nur geistige Onanie? Ändere lieber</p>
<pre><code class="language-cpp">y_test &lt; 1.e-30 &amp;&amp; y_test &gt; -1.e-30
</code></pre>
<p>in</p>
<pre><code class="language-cpp">abs(y_test) &lt; 1e-30
</code></pre>
<p>und frage dich dann, ob diese Schranke wirklich sinnvoll ist bzw. ob man sie nicht in Abhängigkeit von T jeweils anders wählen sollte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220261</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:45:25 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 13:46:22 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Also doch richtig!</p>
</blockquote>
<p>Nene, das hat erstmal nichts mit dem Typ zu tun. Funktortypen setzen sich u.U. aus mehr zusammen als nur aus einer Funktionssignatur, darauf kannste dich also nicht verlassen. Zudem hast du in Zeile 12 noch was vergessen. <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/2220262</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220262</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Wed, 06 Jun 2012 13:46:22 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 16:17:54 GMT]]></title><description><![CDATA[<p>Dann halt so :p</p>
<pre><code class="language-cpp">template &lt;typename T, typename Functor, typename Functor2&gt;
T newt(T x_0, Functor fct, Functor2 der)
{
    T x_n = 0, y_test = fct(x_0);

    if(abs(y_test) &lt; 1.e-30)
    {
        return x_0;
    }
    x_n = x_0 - fct(x_0)/der(x_0);

    return newt(x_n);
}
</code></pre>
<p>Es war nicht Zeile 12, sondern Zeile 4. Also ganz blöd bin ich auch nicht!</p>
<p>Wobei ich immernoch die Version nehmen würde. Da ist nämlich sichergestellt, dass der Datentyp stimmt!</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
T newt(T x_0, T (*fct)(T), T (*der)(T))
{
    T x_n = 0, y_test = fct(x_0);

    if(abs(y_test) &lt; 1.e-30)
    {
        return x_0;
    }
    x_n = x_0 - fct(x_0)/der(x_0);

    return newt(x_n);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2220315</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220315</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 16:17:54 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 18:09:17 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Es war nicht Zeile 12, sondern Zeile 4. Also ganz blöd bin ich auch nicht!</p>
</blockquote>
<p>Guck dir Zeile 12 noch mal genau an.</p>
<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Wobei ich immernoch die Version nehmen würde. Da ist nämlich sichergestellt, dass der Datentyp stimmt!</p>
</blockquote>
<p>Ja? Mit T = std::string? Nene, solange C++ keine Concepts anbietet kannste das vergessen. (Manchmal kann man vielleicht schönere Fehlermeldungen mit static_assert bekommen, aber das ist auch mühselig.) Nimm Funktoren. <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/2220344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220344</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Wed, 06 Jun 2012 18:09:17 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 18:19:53 GMT]]></title><description><![CDATA[<p>Ich meinte, dass so sichergestellt ist, dass der Datentyp der Funktionen in der Funktion stimmt. Bei deiner Variant ist es das nämlich nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220349</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220349</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 18:19:53 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 18:30:45 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Ich meinte, dass so sichergestellt ist, dass der Datentyp der Funktionen in der Funktion stimmt. Bei deiner Variant ist es das nämlich nicht.</p>
</blockquote>
<p><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="😕"
    /> Lesen -&gt; Denken -&gt; Verstehen. Natürlich meintest du das. Ändert aber nichts daran, dass du ohne SFINAE Tricks oder static_assert bei einem Template nie sicher sein kannst, dass man einen ordentlichen Typ vorgesetzt bekommt. Egal wo. Das ist ziemlich blöd, aber ohne Concepts nicht vermeidbar. Und ich halte es für ziemlich großen Quatsch, die <strong>wesentlich</strong> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/26a0.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--warning"
      title=":warning:"
      alt="⚠"
    /> schlechtere Lösung zu bevorzugen, nur um sich selbst mit einem falschen Eindruck von Typsicherheit zu beglücken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220352</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220352</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Wed, 06 Jun 2012 18:30:45 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;return&amp;quot; on Wed, 06 Jun 2012 18:37:47 GMT]]></title><description><![CDATA[<p>Ok. Soweit klar, aber std::string wird der Compiler nicht akzeptieren, weil der Operator - vorkommt und vorallem die Funktion abs(). Die lässt nur numerische Typen zu und das ist doch genau das, was er braucht?</p>
<p>Außerdem haben wir noch die Operatoren / und &lt;. &lt; unterstützen zwar viele Typen, aber / so gut wie ausschließlich numerische.</p>
<p>Wobei ich sagen würde, in beide Fällen mekert der Compiler, wenn man Mist eingibt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220355</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220355</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Wed, 06 Jun 2012 18:37:47 GMT</pubDate></item></channel></rss>