<?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[Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs]]></title><description><![CDATA[<p>Ich bin auf der Suche nach einer &quot;sicheren&quot; (=erkennt Over/Underflows, Subnormale, NaNs,etc.) und performanten C/C++ Library für einfache Operation wie + - * /...</p>
<p>Fuer Integer Under/Overflow-Detection nutzen ich die SafeInt Library (<a href="https://safeint.codeplex.com/" rel="nofollow">https://safeint.codeplex.com/</a>) von Microsoft - nur zu Floats habe ich leider bisher nichts gefunden</p>
<p>Ich habe mir mal ein Testprogramm geschrieben um die verschiedenen Problemfälle aufzuzeigen - bisher erkenne ich einen Over/Underflow und den Rest nach der Operation - bin mir aber nicht sicher ob das so richtig ist (meine Platformen sind alle IEE754 Konform)</p>
<p>Hat jemand Erfahrungen mit solchen Tests?</p>
<pre><code>#include &lt;limits&gt;
#include &lt;cassert&gt;

static bool is_subnormal_bits(const double&amp; p_value)
{
  const unsigned int* const p = reinterpret_cast&lt;const unsigned int*&gt;(&amp;p_value);
  return (p[1] &amp; 0x7FF00000) == 0  &amp;&amp; (p[0] || p[1] &amp; 0x000FFFFF);
}

static bool is_pinf(const double&amp; p_value)
{
  return p_value &gt; 0.0 &amp;&amp; p_value/p_value != p_value/p_value;
}

static bool is_ninf(const double&amp; p_value)
{
  return p_value &lt; 0.0 &amp;&amp; p_value/p_value != p_value/p_value;
}

static bool is_nan(const double&amp; p_value)
{
  return p_value != p_value;
}

int main(int argc, char** argv)
{
  double max = std::numeric_limits&lt;double&gt;::max(); // groesster double Wert
  double min_before_0 = std::numeric_limits&lt;double&gt;::min(); // kleinster Wert vor 0.0
  double lowest = -max; // kleinster double Wert
  double null = argc - argc; // sonst kommt der Kompiler und mault wegen divison durch 0.0
  assert(null == 0.0);

  //NaN
  double nan = sqrt(-1.0);
  assert(is_nan(nan));

  //+/- Infinite
  double pinf = 1.0/null;
  double ninf = log(null);
  assert(is_pinf(pinf));
  assert(is_ninf(ninf));

  //Subnormal
  double subnormal = min_before_0 / 2.0;
  assert(is_subnormal_bits(subnormal));

  //Overflows

  //TEST 1
  {
    double a = max;
    double b = 100.0; // ein bisschen ueber max
    double r = a + b;
    assert(r == a); // Ergebnis entspricht immer noch a und b &gt; 0.0
  }

  //TEST 2
  {
    double a = max;
    double b = max; // max ueber max
    double r = a + b;
    assert(is_pinf(r)); // +INFINITY
  }

  //FRAGE: Ab welchem Wert von b geht das Ergebnis auf +INIFINTY?

  //TEST 3
  {
    double a = max;
    double b = 2.0; // max ueber max
    double r = a * b;
    assert(is_pinf(r)); // +INFINITY
  }

  // Underflows

  //TEST 4
  {
    double a = lowest;
    double b = 100.0;
    double r = a - b;
    assert(r == a); // Ergebnis entspricht immer noch a und b &gt; 0.0
  }

  //TEST 5
  {
    double a = lowest;
    double b = max;
    double r = a - b;
    assert(is_ninf(r)); // -INFINITY
  }

  //TEST 6
  {
    double a = (lowest + 1.0);
    double b = 2.0;
    double r = a - b;
    assert(r == a); // Ergebnis entspricht immer noch a
  }

  //FRAGE: Ab welchem Wert von b geht das Ergebnis auf -INIFINTY?

  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/331675/fragen-zu-floating-pointer-over-underflow-infinity-subnormale-und-nans</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 03:30:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/331675.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 13 Mar 2015 14:09:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Fri, 13 Mar 2015 14:09:17 GMT]]></title><description><![CDATA[<p>Ich bin auf der Suche nach einer &quot;sicheren&quot; (=erkennt Over/Underflows, Subnormale, NaNs,etc.) und performanten C/C++ Library für einfache Operation wie + - * /...</p>
<p>Fuer Integer Under/Overflow-Detection nutzen ich die SafeInt Library (<a href="https://safeint.codeplex.com/" rel="nofollow">https://safeint.codeplex.com/</a>) von Microsoft - nur zu Floats habe ich leider bisher nichts gefunden</p>
<p>Ich habe mir mal ein Testprogramm geschrieben um die verschiedenen Problemfälle aufzuzeigen - bisher erkenne ich einen Over/Underflow und den Rest nach der Operation - bin mir aber nicht sicher ob das so richtig ist (meine Platformen sind alle IEE754 Konform)</p>
<p>Hat jemand Erfahrungen mit solchen Tests?</p>
<pre><code>#include &lt;limits&gt;
#include &lt;cassert&gt;

static bool is_subnormal_bits(const double&amp; p_value)
{
  const unsigned int* const p = reinterpret_cast&lt;const unsigned int*&gt;(&amp;p_value);
  return (p[1] &amp; 0x7FF00000) == 0  &amp;&amp; (p[0] || p[1] &amp; 0x000FFFFF);
}

static bool is_pinf(const double&amp; p_value)
{
  return p_value &gt; 0.0 &amp;&amp; p_value/p_value != p_value/p_value;
}

static bool is_ninf(const double&amp; p_value)
{
  return p_value &lt; 0.0 &amp;&amp; p_value/p_value != p_value/p_value;
}

static bool is_nan(const double&amp; p_value)
{
  return p_value != p_value;
}

int main(int argc, char** argv)
{
  double max = std::numeric_limits&lt;double&gt;::max(); // groesster double Wert
  double min_before_0 = std::numeric_limits&lt;double&gt;::min(); // kleinster Wert vor 0.0
  double lowest = -max; // kleinster double Wert
  double null = argc - argc; // sonst kommt der Kompiler und mault wegen divison durch 0.0
  assert(null == 0.0);

  //NaN
  double nan = sqrt(-1.0);
  assert(is_nan(nan));

  //+/- Infinite
  double pinf = 1.0/null;
  double ninf = log(null);
  assert(is_pinf(pinf));
  assert(is_ninf(ninf));

  //Subnormal
  double subnormal = min_before_0 / 2.0;
  assert(is_subnormal_bits(subnormal));

  //Overflows

  //TEST 1
  {
    double a = max;
    double b = 100.0; // ein bisschen ueber max
    double r = a + b;
    assert(r == a); // Ergebnis entspricht immer noch a und b &gt; 0.0
  }

  //TEST 2
  {
    double a = max;
    double b = max; // max ueber max
    double r = a + b;
    assert(is_pinf(r)); // +INFINITY
  }

  //FRAGE: Ab welchem Wert von b geht das Ergebnis auf +INIFINTY?

  //TEST 3
  {
    double a = max;
    double b = 2.0; // max ueber max
    double r = a * b;
    assert(is_pinf(r)); // +INFINITY
  }

  // Underflows

  //TEST 4
  {
    double a = lowest;
    double b = 100.0;
    double r = a - b;
    assert(r == a); // Ergebnis entspricht immer noch a und b &gt; 0.0
  }

  //TEST 5
  {
    double a = lowest;
    double b = max;
    double r = a - b;
    assert(is_ninf(r)); // -INFINITY
  }

  //TEST 6
  {
    double a = (lowest + 1.0);
    double b = 2.0;
    double r = a - b;
    assert(r == a); // Ergebnis entspricht immer noch a
  }

  //FRAGE: Ab welchem Wert von b geht das Ergebnis auf -INIFINTY?

  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2446606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2446606</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Fri, 13 Mar 2015 14:09:17 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Fri, 13 Mar 2015 19:23:46 GMT]]></title><description><![CDATA[<p>ich habe jetzt eine checked add und mul routinen gebaut</p>
<p>bin mir aber einfach nicht sicher ob das alles so richtig ist - oder ob es Lücken gibt die ich nicht sehe, es viel performanter geht, man vor der Operation prüfen sollte/muss/kann</p>
<pre><code>#include &lt;limits&gt; 
#include &lt;cassert&gt; 

static bool is_subnormal_bits(const double&amp; p_value) 
{ 
  const unsigned int* const p = reinterpret_cast&lt;const unsigned int*&gt;(&amp;p_value); 
  return (p[1] &amp; 0x7FF00000) == 0  &amp;&amp; (p[0] || p[1] &amp; 0x000FFFFF); 
} 

static bool is_inf(const double&amp; p_value) 
{ 
  return p_value/p_value != p_value/p_value; 
} 

static bool is_pinf(const double&amp; p_value) 
{ 
  return p_value &gt; 0.0 &amp;&amp; is_inf(p_value); 
} 

static bool is_ninf(const double&amp; p_value) 
{ 
  return p_value &lt; 0.0 &amp;&amp; is_inf(p_value); 
} 

static bool is_nan(const double&amp; p_value) 
{ 
  return p_value != p_value; 
} 

struct result_t
{
  enum enum_t
  {
    FP_OK,
    FP_UNDERFLOW,
    FP_OVERFLOW,
    FP_NAN,
    FP_SUBNORMAL
  };
};

static result_t::enum_t check_result(const double&amp; p_a, const double&amp; p_b, const double&amp; p_result, const double&amp; p_no_change_value)
{
  if(is_nan(p_result))
  {
    return result_t::FP_NAN;
  }

  if(is_subnormal_bits(p_result))
  {
    return result_t::FP_SUBNORMAL;
  }

  if(p_b == p_no_change_value) return result_t::FP_OK;

  const bool equal = p_a == p_result;
  const bool inf = is_inf(p_result);
  if(equal || inf)
  {
    return (p_b &lt; 0.0) ? result_t::FP_UNDERFLOW : result_t::FP_OVERFLOW;
  }

  return result_t::FP_OK;
}

static double checked_fp_add(const double&amp; p_a, const double&amp; p_b, result_t::enum_t&amp; p_status)
{
  const double result = p_a + p_b;
  p_status = check_result(p_a, p_b, result, 0.0);
  return result;
}

static double checked_fp_mul(const double&amp; p_a, const double&amp; p_b, result_t::enum_t&amp; p_status)
{
  const double result = p_a * p_b;
  p_status = check_result(p_a, p_b, result, 1.0);
  return result;
}

int main(int argc, char** argv) 
{ 
  double max = std::numeric_limits&lt;double&gt;::max(); // groesster double Wert 
  double min_before_0 = std::numeric_limits&lt;double&gt;::min(); // kleinster Wert vor 0.0 
  double lowest = -max; // kleinster double Wert 
  double null = argc - argc; // sonst kommt der Kompiler und mault wegen divison durch 0.0 
  assert(null == 0.0); 

  //NaN 
  double nan = sqrt(-1.0); 
  assert(is_nan(nan)); 

  //+/- Infinite 
  double pinf = 1.0/null; 
  double ninf = log(null); 
  assert(is_pinf(pinf)); 
  assert(is_ninf(ninf)); 

  //Subnormal 
  double subnormal = min_before_0 / 2.0; 
  assert(is_subnormal_bits(subnormal)); 

  //Overflows 
  result_t::enum_t result = result_t::FP_OK;

  //TEST 1 
  double r = checked_fp_add(max, 100.0, result);
  assert(result == result_t::FP_OVERFLOW);

  //TEST 2 
  r = checked_fp_add(max, max, result);
  assert(result == result_t::FP_OVERFLOW);

  //FRAGE: Ab welchem Wert von b geht das Ergebnis auf +INIFINTY? 

  //TEST 3 
  r = checked_fp_add(max, 2.0, result);
  assert(result == result_t::FP_OVERFLOW);

  //TEST 4 
  r = checked_fp_add(lowest, -100.0, result);
  assert(result == result_t::FP_UNDERFLOW);

  //TEST 5 
  r = checked_fp_add(lowest, -max, result);
  assert(result == result_t::FP_UNDERFLOW);

  //TEST 6 
  r = checked_fp_add(lowest + 1.0, -2.0, result);
  assert(result == result_t::FP_UNDERFLOW);

  //FRAGE: Ab welchem Wert von b geht das Ergebnis auf -INIFINTY? 

  //TEST 7
  r = checked_fp_mul(min_before_0, 1.0 / 2.0, result);
  assert(result == result_t::FP_SUBNORMAL);

  //TEST 8
  r = checked_fp_mul(max, -2.0, result);
  assert(result == result_t::FP_UNDERFLOW);

  //TEST 9
  r = checked_fp_mul(nan, 1.0, result);
  assert(result == result_t::FP_NAN);

  return 0; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2446656</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2446656</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Fri, 13 Mar 2015 19:23:46 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Mon, 16 Mar 2015 06:28:19 GMT]]></title><description><![CDATA[<p>Es geht mir nicht unbedingt darum ob man beim Rechnen sowas beachten muss sondern eher um Abbildung solcher Werte/Bereiche in anderen Software-Tools</p>
<p>z.B. hatte ich schon mit dem SQL-Server Problem mit Subnormalen und NaNs bei INSERTs - wie es mit INFNITY aussieht habe ich noch nicht getestet</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2446880</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2446880</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Mon, 16 Mar 2015 06:28:19 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Mon, 16 Mar 2015 07:55:15 GMT]]></title><description><![CDATA[<p>warum nutzt Du nicht die Floating Point Exceptions?<br />
<a href="https://msdn.microsoft.com/en-us/library/aa289157%28v=vs.71%29.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/aa289157(v=vs.71).aspx</a></p>
<p>(Abschnitt: Floating-Point Exceptions as C++ Exceptions)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2446884</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2446884</guid><dc:creator><![CDATA[except]]></dc:creator><pubDate>Mon, 16 Mar 2015 07:55:15 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Mon, 16 Mar 2015 08:27:01 GMT]]></title><description><![CDATA[<blockquote>
<p>warum nutzt Du nicht die Floating Point Exceptions?</p>
</blockquote>
<p>Würde ich (möglicherweise) noch als Erweiterung in meine Library reinbauen - falls schon aktiv gesetzt</p>
<p>Probleme:</p>
<p>Ist das Platform-Unabhängig? (IEE754 ist aber schon meine mindest Anforderung)<br />
hat ARM und andere Platformen was vergleichbares?</p>
<p>Weil ich damit als Library/Dll Fremd-Applikationen in ihrem Floating-Point Verhalten beeinflusse - falls ich die Exceptions scharf schalten würde<br />
und es in der Applikation Code gibt der z.B. FP-Exceptions auf C++ Exceptions mappt - aber bisher immer inaktiv war usw. habe ich dann plötzlich mit meiner Lib das Programmverhalten geändert - böse böse</p>
<p>Oder kann man diese (möglicherweise) applikationsweiten Auswirkungen von FP-Exceptions - heute/anders besser kontrollieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2446889</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2446889</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Mon, 16 Mar 2015 08:27:01 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Fri, 20 Mar 2015 14:09:00 GMT]]></title><description><![CDATA[<p>Abschnitt 3.10 Absatz 10 von <a href="http://open-std.org/JTC1/SC22/WG21/docs/papers/2014/n4296.pdf" rel="nofollow">einem der letzten C++ Standard Entwürfe</a> sagt</p>
<p>n4296.pdf schrieb:</p>
<blockquote>
<p>If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:<br />
[…]</p>
</blockquote>
<p>und listet nur Dinge auf, die nichts mit deinen <code>reinterpret_cast</code> s zu tun haben. Das heißt: Dein Code ruft undefiniertes Verhalten hervor. Das würde ich vermeiden.</p>
<p>Dazu verlässt Du Dich darauf, dass</p>
<ul>
<li><code>double</code> ein 64bit IEEE754 float ist</li>
<li>der Code auf einer little-endian Maschine läuft</li>
<li><code>int</code> 32bit breit ist</li>
</ul>
<p>Ich würde stattdessen mit <code>static_assert</code> sicherstellen, dass<a href="http://en.cppreference.com/w/cpp/types/numeric_limits/is_iec559" rel="nofollow"> <code>std::numeric_limits&lt;double&gt;::is_iec559</code> </a> <code>true</code> ist und statt zwei <code>unsigned int</code> s einfach einen <code>std::uint64_t</code> benutzen — zumindest dann, wenn Du Bitfrickelei bei <code>double</code> s machen willst.</p>
<p>Und</p>
<pre><code class="language-cpp">p_value/p_value != p_value/p_value
</code></pre>
<p>kannst Du natürlich mit</p>
<pre><code class="language-cpp">is_nan(p_value/p_value)
</code></pre>
<p>abkürzen.</p>
<p>Ich frage mich auch, welche Motivation Du hattest, <code>static</code> zu verwenden. Wenn ich nicht möchte, dass etwas außerhalb der Übersetzungseinheit nicht über seinen Namen erreichbar ist, verwende ich dazu in C++ anonyme Namensräume statt <code>static</code> , weil ich es IMHO die Motivation besser ausdrückt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2447428</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2447428</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 20 Mar 2015 14:09:00 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Fri, 20 Mar 2015 15:02:19 GMT]]></title><description><![CDATA[<blockquote>
<p>double ein 64bit IEEE754 float ist<br />
der Code auf einer little-endian Maschine läuft<br />
int 32bit breit ist</p>
</blockquote>
<p>Das ist mir klar, meine Vorgabe und auch so geprüft - nur eben nicht in diesem Beispiel</p>
<blockquote>
<p>und listet nur Dinge auf, die nichts mit deinen reinterpret_casts zu tun haben.</p>
</blockquote>
<p>ist das ein ganzer Satz - ich habe keine Ahnung was du mir sagen möchtest - was meinst du, float&lt;-&gt;int cast aliasing Probleme? das macht der std::uint64_t aber auch nicht besser - und mit 2x uint32 bleibt der Code gleich - ist aber geschmackssache</p>
<blockquote>
<p>Ich frage mich auch, welche Motivation Du hattest, static zu verwenden.</p>
</blockquote>
<p>nur aus Gewohnheit - der VS2010 Kompiler generiert trotz anonymen Namensraum die Funktionen als externals - stört mich irgendwie</p>
<p>Die Kernfrage ist aber nicht ob mein Code schön ist oder besser aufgebaut werden kann sondern nur ob ich so meine OVERFLOW/UNDERFLOW Tests machen kann</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2447441</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2447441</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Fri, 20 Mar 2015 15:02:19 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Fri, 20 Mar 2015 16:07:06 GMT]]></title><description><![CDATA[<p>Gast3 schrieb:</p>
<blockquote>
<blockquote>
<p>und listet nur Dinge auf, die nichts mit deinen reinterpret_casts zu tun haben.</p>
</blockquote>
<p>ist das ein ganzer Satz - ich habe keine Ahnung was du mir sagen möchtest - was meinst du, float&lt;-&gt;int cast aliasing Probleme? das macht der std::uint64_t aber auch nicht besser - und mit 2x uint32 bleibt der Code gleich - ist aber geschmackssache</p>
</blockquote>
<p>Du hast nur die zweite Hälfte des Satzes zitiert. Ja. Ich rede vom Aliasing. Ich habe auch nicht behauptet, dass <code>uint64_t</code> das Aliasing-Problem löst. Das war eher ein Tipp bzgl Endianness, weil Du dann die Endian-spezifischen Indizes [0] und [1] nicht mehr brauchst.</p>
<p>Gast3 schrieb:</p>
<blockquote>
<p>Die Kernfrage ist aber nicht ob mein Code schön ist oder besser aufgebaut werden kann sondern nur ob ich so meine OVERFLOW/UNDERFLOW Tests machen kann.</p>
</blockquote>
<p>Du <em>kannst</em> von mir aus natürlich auch sonst was machen, wie z.B. undefiniertes Verhalten hervorrufen. Aber tatsächlich tun würde ich das an Deiner Stelle nicht.</p>
<p>Bzgl Underflow: 0.0 oder -0.0 kann auch das Ergebnis eines Underflows sein. Das fängst Du so aber nicht ab.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2447454</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2447454</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 20 Mar 2015 16:07:06 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Fri, 20 Mar 2015 18:09:57 GMT]]></title><description><![CDATA[<blockquote>
<p>Das war eher ein Tipp bzgl Endianness, weil Du dann die Endian-spezifischen Indizes [0] und [1] nicht mehr brauchst.</p>
<p>Du kannst von mir aus natürlich auch sonst was machen, wie z.B. undefiniertes Verhalten hervorrufen.</p>
</blockquote>
<p>ok dann nutze ich ein uint64 für die Endianess-Vereinfachung und mach ein memcpy gegen das Aliasing Problem</p>
<p>die meisten (alle?) Kompiler optimieren das memcpy wenn sie kein Aliasing-Problem sehen auf einen einfachen Cast</p>
<p>oder hast du einen bessern Tip (Unions sind da ja auch nichts wert)</p>
<blockquote>
<p>Bzgl Underflow: 0.0 oder -0.0 kann auch das Ergebnis eines Underflows sein. Das fängst Du so aber nicht ab.</p>
</blockquote>
<p>kannst du mal ein Beispiel zeigen wo genau die beiden Werte ein Underflow sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2447471</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2447471</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Fri, 20 Mar 2015 18:09:57 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Fri, 20 Mar 2015 22:05:53 GMT]]></title><description><![CDATA[<p>Gast3 schrieb:</p>
<blockquote>
<p>die meisten (alle?) Kompiler optimieren das memcpy wenn sie kein Aliasing-Problem sehen auf einen einfachen Cast</p>
</blockquote>
<p>Hmm, dieser Satz irritiert mich gerade etwas. Verwechselst Du Aliasing vielleicht mit Alignment? Die Fehlerquelle bei der Verletzung der Aliasing-Regel ist doch, dass der Compiler Code nach der as-if-Regel umsortieren und dabei annehmen darf, dass Du die Aliasing-Regeln <em>nicht</em> verletzt. Tust Du es doch, macht das Programm nicht unbedingt mehr das, was es soll.</p>
<p>Ich hoffe doch mal, dass memcpy von 4 oder 8 Bytes (wobei die Größe zur Übersetzungszeit bekannt ist) irgendwie effizient übersetzt wird. Geprüft habe ich das aber nicht.</p>
<p>Gast3 schrieb:</p>
<blockquote>
<p>oder hast du einen bessern Tip (Unions sind da ja auch nichts wert)</p>
</blockquote>
<p>Der GCC garantiert übrigens explizit, dass &quot;type punning&quot; per <code>union</code> s OK ist. Es ist quasi ein zusätzliches GCC Feature. Aber aus Gründen der Portabilität würde ich darauf verzichten. Nee, ich habe da jetzt auch keinen besseren Tipp als memcpy parat.</p>
<p>Gast3 schrieb:</p>
<blockquote>
<blockquote>
<p>Bzgl Underflow: 0.0 oder -0.0 kann auch das Ergebnis eines Underflows sein. Das fängst Du so aber nicht ab.</p>
</blockquote>
<p>kannst du mal ein Beispiel zeigen wo genau die beiden Werte ein Underflow sind?</p>
</blockquote>
<p>Klar. Das geht ganz einfach:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cmath&gt;

double naive_hypot(double x, double y) {
	return std::sqrt(x*x + y*y);
}

int main() {
	double x = 3e-200;
	double y = 4e-200;
	double z = naive_hypot(x,y);
	std::cout &lt;&lt; z &lt;&lt; std::endl;
}
</code></pre>
<p>In naive_hypot wird x*x und y*y in diesem Beispiel gleich 0.0 wegen Underflow sein. Die Ausgabe des Programms ist also 0 statt 5e-200. Ein Underflow kann Dir also bei zwei Zahlen s und t mit s!=0 und t!=0 für s*t eine 0 geben. Dem Ergebnis allein siehst Du das jetzt nicht an. Eine Multiplikation von zwei Zahlen kann ja wirklich 0 sein, sollte es nur nicht, falls beide Argumente ungleich 0 sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2447506</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2447506</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 20 Mar 2015 22:05:53 GMT</pubDate></item><item><title><![CDATA[Reply to Fragen zu Floating Pointer Over&#x2F;Underflow, +&#x2F;-Infinity, Subnormale und NaNs on Sat, 21 Mar 2015 05:07:32 GMT]]></title><description><![CDATA[<blockquote>
<p>Hmm, dieser Satz irritiert mich gerade etwas. Verwechselst Du Aliasing vielleicht mit Alignment?</p>
<p>Ich hoffe doch mal, dass memcpy von 4 oder 8 Bytes (wobei die Größe zur Übersetzungszeit bekannt ist) irgendwie effizient übersetzt wird</p>
</blockquote>
<p>ja verwechselt und ja memcpy wird optimiert (von VStudio, GCC, Clang, ARM, weitere?)</p>
<blockquote>
<p>Klar. Das geht ganz einfach</p>
</blockquote>
<p>wenn man so wie ich auf Einzeloperationen testet muss man den Algorithmus eben auch so schreiben - oder gibt es eine andere Möglichkeit</p>
<pre><code>double checked_fp_naive_hypot(double x, double y) {
  result_t::enum_t result = result_t::FP_OK;
  double x2 = checked_fp_mul(x,x, result); //==&gt; FP_OVERFLOW
  double y2 = checked_fp_mul(y,y, result); //==&gt; FP_OVERFLOW
  double x2_add_y2 = checked_fp_add(x,y, result);
  double sqrt_result = std::sqrt(x2_add_y2);
  return sqrt_result;
}
</code></pre>
<p>wenn ich einen checked_double-Typ mit Operatoren fuer + - * / oder sowas haette<br />
dann wuerde dein x*x oder y*y schon eine Exception/oder anderes werfen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2447519</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2447519</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Sat, 21 Mar 2015 05:07:32 GMT</pubDate></item></channel></rss>