<?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[Suche gute fuzzyCompare funktion]]></title><description><![CDATA[<p>Hallo liebe Forengemeinde,</p>
<p>ich suche eine Funktion, die zwei floats miteinander vergleicht. Dabei gelten zwei floats als gleich, wenn sie in den ersten Nachkommastellen übereinstimmen. Die genaue Anzahl an Nachkommastellen, die übereinstimmen sollen, kenn ich nicht. Ich vertraue da euren Empfehlungen.<br />
Eine solche Funktion gibt es zB in der Qt Bibliothek als qFuzzyCompare. Leider haben sie den Quelltext rausgenommen.<br />
Wäre echt nett, wenn jmd. kurz eine solche Funktion hier posten könnte, falls einer das schon iwo zu liegen hat.</p>
<p>Danke im voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/293913/suche-gute-fuzzycompare-funktion</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 04:04:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/293913.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 13 Oct 2011 10:54:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Suche gute fuzzyCompare funktion on Thu, 13 Oct 2011 10:54:29 GMT]]></title><description><![CDATA[<p>Hallo liebe Forengemeinde,</p>
<p>ich suche eine Funktion, die zwei floats miteinander vergleicht. Dabei gelten zwei floats als gleich, wenn sie in den ersten Nachkommastellen übereinstimmen. Die genaue Anzahl an Nachkommastellen, die übereinstimmen sollen, kenn ich nicht. Ich vertraue da euren Empfehlungen.<br />
Eine solche Funktion gibt es zB in der Qt Bibliothek als qFuzzyCompare. Leider haben sie den Quelltext rausgenommen.<br />
Wäre echt nett, wenn jmd. kurz eine solche Funktion hier posten könnte, falls einer das schon iwo zu liegen hat.</p>
<p>Danke im voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130913</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130913</guid><dc:creator><![CDATA[*-BootLag-*]]></dc:creator><pubDate>Thu, 13 Oct 2011 10:54:29 GMT</pubDate></item><item><title><![CDATA[Reply to Suche gute fuzzyCompare funktion on Thu, 13 Oct 2011 11:04:58 GMT]]></title><description><![CDATA[<p>Gut sei mal dahin gestellt, ist das erste was mir dazu eingefallen ist und erfüllt seinen Zweck ungefähr:</p>
<pre><code class="language-cpp">#include &lt;cmath&gt;

bool fuzzy_compare(double lhs, double rhs, unsigned N=8)
{
  if (std::fabs(rhs - lhs) &lt; lhs/std::pow(10,N))
    return true;
  else 
    return false;
}

#include &lt;iostream&gt;

using namespace std;

int main()
{
  cout &lt;&lt; fuzzy_compare(1.23456789, 1.23456788) &lt;&lt; '\n'
       &lt;&lt; fuzzy_compare(1.23456789, 1.23457788) &lt;&lt; '\n'
       &lt;&lt; fuzzy_compare(123456789, 123456788) &lt;&lt; '\n'
       &lt;&lt; fuzzy_compare(-123456789, -123457788) &lt;&lt; '\n'
       &lt;&lt; fuzzy_compare(123456789, 123456788, 10) &lt;&lt; '\n'
       &lt;&lt; fuzzy_compare(123456789, 123457788, 4) &lt;&lt; '\n'
       &lt;&lt; fuzzy_compare(-0.000001234, -0.000001233) &lt;&lt; '\n'
       &lt;&lt; fuzzy_compare(0.000012345, 0.000012344,3) &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2130916</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130916</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 13 Oct 2011 11:04:58 GMT</pubDate></item><item><title><![CDATA[Reply to Suche gute fuzzyCompare funktion on Thu, 13 Oct 2011 11:15:08 GMT]]></title><description><![CDATA[<p>Was heißt denn Qt hat den Quelltext rausgenommen? Auf deren Seite find ich immer noch nen source download.</p>
<p>Edit: Schau mal was ich gefunden hab:</p>
<pre><code class="language-cpp">static inline bool qFuzzyCompare(double p1, double p2)
{
    return (qAbs(p1 - p2) &lt;= 0.000000000001 * qMin(qAbs(p1), qAbs(p2)));
}

static inline bool qFuzzyCompare(float p1, float p2)
{
    return (qAbs(p1 - p2) &lt;= 0.00001f * qMin(qAbs(p1), qAbs(p2)));
}
</code></pre>
<p>qAbs/qMin dann einfach durch std:: sachen ersetzen, oder wenn du willst such ich dir den code dazu auch raus.</p>
<p>Edit2:</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
inline const T &amp;qMin(const T &amp;a, const T &amp;b) { if (a &lt; b) return a; return b; }

template &lt;typename T&gt;
inline T qAbs(const T &amp;t) { return t &gt;= 0 ? t : -t; }
</code></pre>
<p>Stand: 4.7.3 (4.7.4 gibts glaub ich schon, glaube aber nicht, dass es da nennenswerte änderungen gibt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130917</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130917</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Thu, 13 Oct 2011 11:15:08 GMT</pubDate></item><item><title><![CDATA[Reply to Suche gute fuzzyCompare funktion on Thu, 13 Oct 2011 11:28:55 GMT]]></title><description><![CDATA[<p>Ok vielen Dank für die schnelle und großartige Hilfe!<br />
Ich muss zu meiner Schande gestehen, dass ich wohl in den falschen Dateien gesucht habe. Habe nämlich den SDK Ordner auf meinem PC durchsucht und nur eine gekürzte Fassung oder derartiges gefunden. Aber nun habe ich ja eine gute Lösung von euch!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130922</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130922</guid><dc:creator><![CDATA[*-BootLag-*]]></dc:creator><pubDate>Thu, 13 Oct 2011 11:28:55 GMT</pubDate></item><item><title><![CDATA[Reply to Suche gute fuzzyCompare funktion on Thu, 13 Oct 2011 12:17:05 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#include &lt;cmath&gt;

bool fuzzy_compare(double lhs, double rhs, unsigned N=8)
{
  if (std::fabs(rhs - lhs) &lt; lhs/std::pow(10,N))
    return true;
  else 
    return false;
}
</code></pre>
</blockquote>
<p>Warum überprüfst du einen bool um ihm dann das Ergebnis des if zuzuweisen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<pre><code class="language-cpp">bool fuzzy_compare(double lhs, double rhs, unsigned N=8)
{
  return (std::fabs(rhs - lhs) &lt; lhs/std::pow(10,N));
}
</code></pre>
<p><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/2130943</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130943</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Thu, 13 Oct 2011 12:17:05 GMT</pubDate></item><item><title><![CDATA[Reply to Suche gute fuzzyCompare funktion on Thu, 13 Oct 2011 12:43:15 GMT]]></title><description><![CDATA[<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>−</mo><mi>B</mi><mi>o</mi><mi>o</mi><mi>t</mi><mi>L</mi><mi>a</mi><mi>g</mi><mo>−</mo></mrow><annotation encoding="application/x-tex">-BootLag-</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.8777699999999999em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord">−</span><span class="mord mathit" style="margin-right:0.05017em;">B</span><span class="mord mathit">o</span><span class="mord mathit">o</span><span class="mord mathit">t</span><span class="mord mathit">L</span><span class="mord mathit">a</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mbin">−</span></span></span></span> schrieb:</p>
<blockquote>
<p>Dabei gelten zwei floats als gleich, wenn sie in den ersten Nachkommastellen übereinstimmen.</p>
</blockquote>
<p>Also, 99.999 und 100.000 sind total verschieden, ja? :-p</p>
<p>Vielleicht suchst Du eher so etwas</p>
<pre><code>abs(x-y) &lt;= 0.01 * max(abs(x),abs(y))
</code></pre>
<p>wobei die 0.01 hier so eine Art &quot;relative Unschärfe&quot; wäre.</p>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>−</mo><mi>B</mi><mi>o</mi><mi>o</mi><mi>t</mi><mi>L</mi><mi>a</mi><mi>g</mi><mo>−</mo></mrow><annotation encoding="application/x-tex">-BootLag-</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.8777699999999999em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord">−</span><span class="mord mathit" style="margin-right:0.05017em;">B</span><span class="mord mathit">o</span><span class="mord mathit">o</span><span class="mord mathit">t</span><span class="mord mathit">L</span><span class="mord mathit">a</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mbin">−</span></span></span></span> schrieb:</p>
<blockquote>
<p>Die genaue Anzahl an Nachkommastellen, die übereinstimmen sollen, kenn ich nicht. Ich vertraue da euren Empfehlungen.</p>
</blockquote>
<p>Janz falscher Ansatz; denn das kommt sehr stark drauf an, was Du da eigentlich berechnest. Siehe Artikel <a href="http://de.wikipedia.org/wiki/Ausl%C3%B6schung_%28numerische_Mathematik%29" rel="nofollow">Numerische Auslöschung</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130952</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 13 Oct 2011 12:43:15 GMT</pubDate></item><item><title><![CDATA[Reply to Suche gute fuzzyCompare funktion on Thu, 13 Oct 2011 12:46:01 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>−</mo><mi>B</mi><mi>o</mi><mi>o</mi><mi>t</mi><mi>L</mi><mi>a</mi><mi>g</mi><mo>−</mo></mrow><annotation encoding="application/x-tex">-BootLag-</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.8777699999999999em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord">−</span><span class="mord mathit" style="margin-right:0.05017em;">B</span><span class="mord mathit">o</span><span class="mord mathit">o</span><span class="mord mathit">t</span><span class="mord mathit">L</span><span class="mord mathit">a</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mbin">−</span></span></span></span> schrieb:</p>
<blockquote>
<p>Dabei gelten zwei floats als gleich, wenn sie in den ersten Nachkommastellen übereinstimmen.</p>
</blockquote>
<p>Also, 99.999 und 100.000 sind total verschieden, ja? :-p</p>
</blockquote>
<p>Heh, meine Lösung macht das bewusst richtig und die ziemlich identische Qt-Lösung ebenfalls.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130953</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130953</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 13 Oct 2011 12:46:01 GMT</pubDate></item></channel></rss>