<?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::fixed komische Werte?]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hab ein Problem mit folgendem Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;limits&gt;

int main()
{
	float f = 1.57772181e-30f;

	std::cout.precision(std::numeric_limits&lt;float&gt;::max_digits10);
	std::cout &lt;&lt; f &lt;&lt; '\n';

	std::cout.precision(50);
	std::cout &lt;&lt; std::fixed &lt;&lt; f &lt;&lt; '\n';
}
</code></pre>
<p>Output ist:</p>
<pre><code>1.57772181e-30
0.00000000000000000000000000000157772181044202361082
</code></pre>
<p>Ich hätte aber</p>
<pre><code>1.57772181e-30
0.00000000000000000000000000000157772181000000000000
</code></pre>
<p>erwartet. Was sollen diese Ziffern nach der maximalen Genauigkeit? Und wie kann man die wegbekommen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/334556/std-fixed-komische-werte</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 09:21:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/334556.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 22 Sep 2015 11:22:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::fixed komische Werte? on Tue, 22 Sep 2015 11:22:43 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hab ein Problem mit folgendem Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;limits&gt;

int main()
{
	float f = 1.57772181e-30f;

	std::cout.precision(std::numeric_limits&lt;float&gt;::max_digits10);
	std::cout &lt;&lt; f &lt;&lt; '\n';

	std::cout.precision(50);
	std::cout &lt;&lt; std::fixed &lt;&lt; f &lt;&lt; '\n';
}
</code></pre>
<p>Output ist:</p>
<pre><code>1.57772181e-30
0.00000000000000000000000000000157772181044202361082
</code></pre>
<p>Ich hätte aber</p>
<pre><code>1.57772181e-30
0.00000000000000000000000000000157772181000000000000
</code></pre>
<p>erwartet. Was sollen diese Ziffern nach der maximalen Genauigkeit? Und wie kann man die wegbekommen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2468745</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2468745</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Tue, 22 Sep 2015 11:22:43 GMT</pubDate></item><item><title><![CDATA[Reply to std::fixed komische Werte? on Tue, 22 Sep 2015 11:39:43 GMT]]></title><description><![CDATA[<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>0</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>1</mn><mn>5</mn><mn>7</mn><mn>7</mn><mn>7</mn><mn>2</mn><mn>1</mn><mn>8</mn><mn>1</mn></mrow><annotation encoding="application/x-tex">0.00000000000000000000000000000157772181</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">1</span><span class="mord mathrm">5</span><span class="mord mathrm">7</span><span class="mord mathrm">7</span><span class="mord mathrm">7</span><span class="mord mathrm">2</span><span class="mord mathrm">1</span><span class="mord mathrm">8</span><span class="mord mathrm">1</span></span></span></span> lässt sich nicht in einem <code>float</code> darstellen. Als normalisierte, binäre Fließkommazahl dargestellt sieht die Zahl so aus:1.1111111111111111111111111111110110010111111010001011010010010000101011100111110011100110011110001111111000101101110111101011111100010011101110001010101000010101100010111111101110011101011110001001100100\dots_{2} \cdot 2^{-100}<br />
Unsere Mantisse hat aber leider nur 24 Stellen. Wir runden und approximieren also<br />
1.111,111,111,111,111,111,111,11\dots_{2} \cdot 2^{-100} \approx 1_{2} \cdot 2^{-99} = 1.5777218104420236\dots \cdot 10^{-30}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2468752</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2468752</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 22 Sep 2015 11:39:43 GMT</pubDate></item><item><title><![CDATA[Reply to std::fixed komische Werte? on Tue, 22 Sep 2015 11:55:20 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>0</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>1</mn><mn>5</mn><mn>7</mn><mn>7</mn><mn>7</mn><mn>2</mn><mn>1</mn><mn>8</mn><mn>1</mn></mrow><annotation encoding="application/x-tex">0.00000000000000000000000000000157772181</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">1</span><span class="mord mathrm">5</span><span class="mord mathrm">7</span><span class="mord mathrm">7</span><span class="mord mathrm">7</span><span class="mord mathrm">2</span><span class="mord mathrm">1</span><span class="mord mathrm">8</span><span class="mord mathrm">1</span></span></span></span> lässt sich nicht in einem <code>float</code> darstellen.</p>
</blockquote>
<p>Aber ich dachte mit <code>max_digits10</code> wird mir immer genau so viel angezeigt wie benötigt um die Zahl exakt zu serialisieren??</p>
<p>Warum zeigt mir dann <code>cout 1.57772181e-30</code> an, wenn das gar nicht als <code>float</code> darstellbar ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2468754</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2468754</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Tue, 22 Sep 2015 11:55:20 GMT</pubDate></item><item><title><![CDATA[Reply to std::fixed komische Werte? on Tue, 22 Sep 2015 14:17:35 GMT]]></title><description><![CDATA[<p>happystudent schrieb:</p>
<blockquote>
<p>Warum zeigt mir dann <code>cout 1.57772181e-30</code> an, wenn das gar nicht als <code>float</code> darstellbar ist?</p>
</blockquote>
<p>Weil es reicht damit der Round-Trip funktioniert.<br />
Also damit bei <code>float-&gt;text-&gt;float</code> wieder am Ende der selbe <code>float</code> rauskommt.</p>
<p>Angenommen ich hab nur 2 Bit Mantisse. Damit kann ich dann z.B. 1, 1.25, 1.5 und 1.75 darstellen.<br />
Dezimal brauch ich aber bloss eine Kommastelle um einen verlustfreien Round-Trip zu machen. 1 bleibt 1, 1.25 wird 1.2 (oder 1.3 - je nach Rundungsverfahren), 1.5 bleibt 1.5 und 1.75 wird 1.8. Der Round-Trip ist verlustfrei da sowohl 1.2 als auch 1.3 näher zu 1.25 als zu 1 bzw. 1.5 liegen =&gt; sie werden also beide auf 1.25 abgebildet. Und 1.8 halt auf 1.75.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2468776</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2468776</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 22 Sep 2015 14:17:35 GMT</pubDate></item><item><title><![CDATA[Reply to std::fixed komische Werte? on Tue, 22 Sep 2015 18:05:32 GMT]]></title><description><![CDATA[<p>Irgendwie versteh ichs immer noch nicht, sry...</p>
<p>Ein float hat doch 7-8 signifikante Stellen. Wieso brauch ich dann überhaupt noch eine neunte Stelle für den Round-Trip? Liest der Rechner die Zahl in einer höheren Genauigkeit als float ein um sie korrekt runden zu können und wandelt sie dann in float zurück?</p>
<p>Besonders wenn ich mir das Ergebnis</p>
<pre><code>0.00000000000000000000000000000157772181044202361082
</code></pre>
<p>angucke, sind das ja viel mehr signifikante Stellen, aber diese sind ja korrekt. Wie kann das sein, dass beim serialisieren hier eine viel größere Genauigkeit zustande kommt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2468795</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2468795</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Tue, 22 Sep 2015 18:05:32 GMT</pubDate></item><item><title><![CDATA[Reply to std::fixed komische Werte? on Tue, 22 Sep 2015 18:45:09 GMT]]></title><description><![CDATA[<p>Wo siehst du eine 9. Stelle?<br />
Bzw. falls dich das verwirrt: Der 1er am Anfang zählt nicht. Weil da für &quot;normale&quot; float Zahlen immer ein 1er ist. Ausnahme sind bloss die beiden Nullen, die beiden INFs, NaN und Denormals.</p>
<p>happystudent schrieb:</p>
<blockquote>
<p>Besonders wenn ich mir das Ergebnis</p>
<pre><code>0.00000000000000000000000000000157772181044202361082
</code></pre>
<p>angucke, sind das ja viel mehr signifikante Stellen, aber diese sind ja korrekt. Wie kann das sein, dass beim serialisieren hier eine viel größere Genauigkeit zustande kommt?</p>
</blockquote>
<p>Nein, die sind eben nicht <em>signifikant</em>. <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/2468796</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2468796</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 22 Sep 2015 18:45:09 GMT</pubDate></item><item><title><![CDATA[Reply to std::fixed komische Werte? on Wed, 23 Sep 2015 13:30:45 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Wo siehst du eine 9. Stelle?</p>
</blockquote>
<p>Naja, <code>std::numeric_limits&lt;float&gt;::max_digits10</code> ist ja 9, deswegen (mein Beispiel war schlecht gewählt merke ich). Also ich verstehe, dass wenn man 8 bit Genauigkeit mit richtig gerundeter letzter Stelle 9 Stellen braucht. Was ich nicht verstehe ist, wie der Compiler/PC das macht wenn er doch nur 8 Stellen hat.</p>
<p>Angenommen man hat einen string mit einem float drinnen und will den korrekt runden:</p>
<p>&quot;1.12345678999999999&quot;</p>
<p>Um das überhaupt zu können müsste der Rechner das doch erstmal in einen Datentyp wandeln der mindestens 9 Stellen Genauigkeit hat, dann runden, dann zurück zu float? Also:</p>
<p>&quot;1.12345678999999999&quot; -&gt; 1.123456789 -&gt; 1.12345679</p>
<p>Wo ist mein Denkfehler?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2468876</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2468876</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Wed, 23 Sep 2015 13:30:45 GMT</pubDate></item><item><title><![CDATA[Reply to std::fixed komische Werte? on Mon, 28 Sep 2015 10:30:46 GMT]]></title><description><![CDATA[<p>Wenn ich 100001/300001 auf 1/3 runde habe ich in der dezimalen Darstellung trotzdem nicht weniger Nachkommastellen als vorher. Es macht daher gar keinen Sinn, die Nachkommastellen in der dezimalen Darstellung zu zaehlen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2469239</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2469239</guid><dc:creator><![CDATA[TGGC]]></dc:creator><pubDate>Mon, 28 Sep 2015 10:30:46 GMT</pubDate></item></channel></rss>