<?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[std::norm von std::complex benutzt std::sqrt????]]></title><description><![CDATA[<p>Ich habe Performance-Messungen angestellt und herausgefunden, warum <code>std::complex&lt;double&gt;</code> meinen Algorithmus langsam macht.</p>
<p>Es geht um die Funktion <code>std::norm(c)</code> , die eigentlich nichts anderes als <code>c.real()*c.real() + c.imag()*c.imag()</code> berechnet (des Quadrat der euklidischen Norm halt). Aus der Doku:</p>
<p>&lt;a href= schrieb:</p>
<blockquote>
<p><a href="http://en.cppreference.com/w/cpp/numeric/complex/norm" rel="nofollow">http://en.cppreference.com/w/cpp/numeric/complex/norm</a>&quot;&gt;The norm calculated by this function is also known as <a href="http://en.wikipedia.com/wiki/Field_norm" rel="nofollow">field norm</a> or <a href="http://mathworld.wolfram.com/AbsoluteSquare.html" rel="nofollow">absolute square</a>.</p>
<p>The <em>Euclidean norm</em> of a complex number is provided by<a href="http://en.cppreference.com/w/cpp/numeric/complex/abs" rel="nofollow"> <code>std::abs</code> </a>, which is more costly to compute. In some situations, it may be replaced by <code>std::norm</code> , for example, if <code>abs(z1) &gt; abs(z2)</code> then <code>norm(z1) &gt; norm(z2)</code> .</p>
</blockquote>
<p>Aus der Standardbibliothek vom GCC:</p>
<pre><code class="language-cpp">// 26.2.7/5: norm(__z) returns the squared magnitude of __z.
  //     As defined, norm() is -not- a norm is the common mathematical
  //     sense used in numerics.  The helper class _Norm_helper&lt;&gt; tries to
  //     distinguish between builtin floating point and the rest, so as
  //     to deliver an answer as close as possible to the real value.
  template&lt;bool&gt;
    struct _Norm_helper
    {
      template&lt;typename _Tp&gt;
        static inline _Tp _S_do_it(const complex&lt;_Tp&gt;&amp; __z)
        {
          const _Tp __x = __z.real();
          const _Tp __y = __z.imag();
          return __x * __x + __y * __y;
        }
    };

  template&lt;&gt;
    struct _Norm_helper&lt;true&gt;
    {
      template&lt;typename _Tp&gt;
        static inline _Tp _S_do_it(const complex&lt;_Tp&gt;&amp; __z)
        {
          _Tp __res = std::abs(__z);
          return __res * __res;
        }
    };

  template&lt;typename _Tp&gt;
    inline _Tp
    norm(const complex&lt;_Tp&gt;&amp; __z)
    {
      return _Norm_helper&lt;__is_floating&lt;_Tp&gt;::__value 
        &amp;&amp; !_GLIBCXX_FAST_MATH&gt;::_S_do_it(__z);
    }

// ...

  // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
  template&lt;typename _Tp&gt;
    inline _Tp
    __complex_abs(const complex&lt;_Tp&gt;&amp; __z)
    {
      _Tp __x = __z.real();
      _Tp __y = __z.imag();
      const _Tp __s = std::max(abs(__x), abs(__y));
      if (__s == _Tp())  // well ...
        return __s;
      __x /= __s; 
      __y /= __s;
      return __s * sqrt(__x * __x + __y * __y);
    }

// ...

  template&lt;typename _Tp&gt;
    inline _Tp
    abs(const complex&lt;_Tp&gt;&amp; __z) { return __complex_abs(__z); }
</code></pre>
<p>Mit anderen Worten, es werden zahlreiche Checks durchgeführt, unter anderem die Wurzel gezogen und am Schluss wieder quadriert.</p>
<p>Was soll das? Wer würde das je wollen? Wie schalte ich das ab? IEEE-Kompatibilität ist mir wichtig, deshalb kommt es für mich nicht in Frage, mit <code>--ffast-math</code> zu kompilieren. Das Makro <code>_GLIBCXX_FAST_MATH</code> ist leider nicht dafür vorgesehen, vom Benutzer gesetzt zu werden, da es im config-Header gesetzt wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/335117/std-norm-von-std-complex-benutzt-std-sqrt</link><generator>RSS for Node</generator><lastBuildDate>Fri, 24 Apr 2026 21:01:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/335117.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 31 Oct 2015 00:52:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::norm von std::complex benutzt std::sqrt???? on Sat, 31 Oct 2015 00:52:23 GMT]]></title><description><![CDATA[<p>Ich habe Performance-Messungen angestellt und herausgefunden, warum <code>std::complex&lt;double&gt;</code> meinen Algorithmus langsam macht.</p>
<p>Es geht um die Funktion <code>std::norm(c)</code> , die eigentlich nichts anderes als <code>c.real()*c.real() + c.imag()*c.imag()</code> berechnet (des Quadrat der euklidischen Norm halt). Aus der Doku:</p>
<p>&lt;a href= schrieb:</p>
<blockquote>
<p><a href="http://en.cppreference.com/w/cpp/numeric/complex/norm" rel="nofollow">http://en.cppreference.com/w/cpp/numeric/complex/norm</a>&quot;&gt;The norm calculated by this function is also known as <a href="http://en.wikipedia.com/wiki/Field_norm" rel="nofollow">field norm</a> or <a href="http://mathworld.wolfram.com/AbsoluteSquare.html" rel="nofollow">absolute square</a>.</p>
<p>The <em>Euclidean norm</em> of a complex number is provided by<a href="http://en.cppreference.com/w/cpp/numeric/complex/abs" rel="nofollow"> <code>std::abs</code> </a>, which is more costly to compute. In some situations, it may be replaced by <code>std::norm</code> , for example, if <code>abs(z1) &gt; abs(z2)</code> then <code>norm(z1) &gt; norm(z2)</code> .</p>
</blockquote>
<p>Aus der Standardbibliothek vom GCC:</p>
<pre><code class="language-cpp">// 26.2.7/5: norm(__z) returns the squared magnitude of __z.
  //     As defined, norm() is -not- a norm is the common mathematical
  //     sense used in numerics.  The helper class _Norm_helper&lt;&gt; tries to
  //     distinguish between builtin floating point and the rest, so as
  //     to deliver an answer as close as possible to the real value.
  template&lt;bool&gt;
    struct _Norm_helper
    {
      template&lt;typename _Tp&gt;
        static inline _Tp _S_do_it(const complex&lt;_Tp&gt;&amp; __z)
        {
          const _Tp __x = __z.real();
          const _Tp __y = __z.imag();
          return __x * __x + __y * __y;
        }
    };

  template&lt;&gt;
    struct _Norm_helper&lt;true&gt;
    {
      template&lt;typename _Tp&gt;
        static inline _Tp _S_do_it(const complex&lt;_Tp&gt;&amp; __z)
        {
          _Tp __res = std::abs(__z);
          return __res * __res;
        }
    };

  template&lt;typename _Tp&gt;
    inline _Tp
    norm(const complex&lt;_Tp&gt;&amp; __z)
    {
      return _Norm_helper&lt;__is_floating&lt;_Tp&gt;::__value 
        &amp;&amp; !_GLIBCXX_FAST_MATH&gt;::_S_do_it(__z);
    }

// ...

  // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
  template&lt;typename _Tp&gt;
    inline _Tp
    __complex_abs(const complex&lt;_Tp&gt;&amp; __z)
    {
      _Tp __x = __z.real();
      _Tp __y = __z.imag();
      const _Tp __s = std::max(abs(__x), abs(__y));
      if (__s == _Tp())  // well ...
        return __s;
      __x /= __s; 
      __y /= __s;
      return __s * sqrt(__x * __x + __y * __y);
    }

// ...

  template&lt;typename _Tp&gt;
    inline _Tp
    abs(const complex&lt;_Tp&gt;&amp; __z) { return __complex_abs(__z); }
</code></pre>
<p>Mit anderen Worten, es werden zahlreiche Checks durchgeführt, unter anderem die Wurzel gezogen und am Schluss wieder quadriert.</p>
<p>Was soll das? Wer würde das je wollen? Wie schalte ich das ab? IEEE-Kompatibilität ist mir wichtig, deshalb kommt es für mich nicht in Frage, mit <code>--ffast-math</code> zu kompilieren. Das Makro <code>_GLIBCXX_FAST_MATH</code> ist leider nicht dafür vorgesehen, vom Benutzer gesetzt zu werden, da es im config-Header gesetzt wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473585</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473585</guid><dc:creator><![CDATA[baukomplex]]></dc:creator><pubDate>Sat, 31 Oct 2015 00:52:23 GMT</pubDate></item><item><title><![CDATA[Reply to std::norm von std::complex benutzt std::sqrt???? on Sat, 31 Oct 2015 01:09:44 GMT]]></title><description><![CDATA[<p>Eigentlich könnte mir das egal sein, da ich <code>norm</code> selten direkt verwende und mir eine eigene Funktion schreiben könnte.</p>
<p>Aber <code>operator/</code> benutzt <code>norm</code> . Und den brauche ich!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473586</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473586</guid><dc:creator><![CDATA[baukomplex]]></dc:creator><pubDate>Sat, 31 Oct 2015 01:09:44 GMT</pubDate></item><item><title><![CDATA[Reply to std::norm von std::complex benutzt std::sqrt???? on Sat, 31 Oct 2015 05:31:54 GMT]]></title><description><![CDATA[<p>Allein schon ein<br />
<a href="https://www.google.de/search?&amp;q=std%3A%3Anorm%20gcc" rel="nofollow">Google: std::norm gcc</a><br />
beantwortet die Frage. Die Begründungen sind mir zwar fadenscheinig, aber so ist nun einmal der Stand der Dinge. Gut zu wissen, dann werde ich die GCC-Implementierung in Zukunft selber auch vermeiden.</p>
<p>baukomplex schrieb:</p>
<blockquote>
<p>Eigentlich könnte mir das egal sein, da ich <code>norm</code> selten direkt verwende und mir eine eigene Funktion schreiben könnte.</p>
<p>Aber <code>operator/</code> benutzt <code>norm</code> . Und den brauche ich!</p>
</blockquote>
<p>Dann musst du eben auch eine eigene Funktion dafür schreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473588</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473588</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sat, 31 Oct 2015 05:31:54 GMT</pubDate></item><item><title><![CDATA[Reply to std::norm von std::complex benutzt std::sqrt???? on Sat, 31 Oct 2015 06:44:13 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Allein schon ein<br />
<a href="https://www.google.de/search?&amp;q=std%3A%3Anorm%20gcc" rel="nofollow">Google: std::norm gcc</a><br />
beantwortet die Frage. Die Begründungen sind mir zwar fadenscheinig, aber so ist nun einmal der Stand der Dinge.</p>
</blockquote>
<p>Sorry, Filterblase!<br />
Mir zeigt google ganz andere Sachen als Dir und auf der ersten Seite ist kein Link, der irgendwie zu Deinem Text passt. Möglicherweise wäre es eventuell unter Umständen manchmal keine völlig absurde Idee, in nichttrivialen Fällen das Linkziel statt der Suchanfrage zu posten.</p>
<p>Der GCC-Weg ist eigentlich, Oberkacke in die stdlib zu schreiben und dann den Compiler daraufhin anzupassen, daß er die Oberkacke wegoptimiert bzw nicht anwarnt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473591</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473591</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 31 Oct 2015 06:44:13 GMT</pubDate></item><item><title><![CDATA[Reply to std::norm von std::complex benutzt std::sqrt???? on Sat, 31 Oct 2015 06:57:33 GMT]]></title><description><![CDATA[<p>Nanu? Ich war mir eigentlich recht sicher, die Filterblase ausgeschaltet zu haben. Relevante Ergebnisse:<br />
<a href="http://www.programdoc.com/1094_43623_1.htm" rel="nofollow">http://www.programdoc.com/1094_43623_1.htm</a><br />
<a href="https://gcc.gnu.org/ml/gcc-patches/2009-03/msg00197.html" rel="nofollow">https://gcc.gnu.org/ml/gcc-patches/2009-03/msg00197.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473594</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473594</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sat, 31 Oct 2015 06:57:33 GMT</pubDate></item><item><title><![CDATA[Reply to std::norm von std::complex benutzt std::sqrt???? on Sat, 31 Oct 2015 09:38:47 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Nanu? Ich war mir eigentlich recht sicher, die Filterblase ausgeschaltet zu haben. Relevante Ergebnisse:<br />
<a href="http://www.programdoc.com/1094_43623_1.htm" rel="nofollow">http://www.programdoc.com/1094_43623_1.htm</a><br />
<a href="https://gcc.gnu.org/ml/gcc-patches/2009-03/msg00197.html" rel="nofollow">https://gcc.gnu.org/ml/gcc-patches/2009-03/msg00197.html</a></p>
</blockquote>
<p>Der programdoc-Link kommt bei mir erst ganz unten auf der zweiten Seite, so weit schaue ich nie.</p>
<p>Die paar Begründungen (die für mich eher nach wilden Vermutungen tönen) nützen mir ausserdem nichts, ich will Lösungen.</p>
<p>Ist einen eigenen complex schreiben das beste, was ich machen kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473601</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473601</guid><dc:creator><![CDATA[baukomplex]]></dc:creator><pubDate>Sat, 31 Oct 2015 09:38:47 GMT</pubDate></item></channel></rss>