<?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[feststellen ob ein double eine natürliche Zahl ist]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich versuche gerade rauszufinden ob es möglich ist ffestzustellen das</p>
<pre><code>double d = 1.0;
</code></pre>
<p>eigentlich das selbe wie ein int 1 ist,</p>
<p>gibt es dafür eine Standard Bibliothek Funktion, oder muss man da mit &lt;&gt; epsilon oder ähnlichen selbst etwas schreiben?</p>
<p>vielleicht noch als Zusatz Erklärung:</p>
<blockquote>
<p>sqlite&gt; create table t1 (a NUM);<br />
sqlite&gt; insert into t1 values (1.0);<br />
sqlite&gt; select * from t1;<br />
a<br />
1</p>
</blockquote>
<p>es scheint irgendwo, entweder schon beim einfügen, oder beim anzeigen der Query, scheint sqlite3 sich ziemlich sicher zu sein das 1.0 eigentlich eine 1 ist.</p>
<p>da sqlite3 ein C program ist, jedoch viel zu gross für mich um da irgendetwas zu finden, frage ich mich ob das eine Standard Funktion ist oder nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/339999/feststellen-ob-ein-double-eine-natürliche-zahl-ist</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 16:54:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/339999.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 13 Oct 2016 07:17:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to feststellen ob ein double eine natürliche Zahl ist on Thu, 13 Oct 2016 07:20:58 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich versuche gerade rauszufinden ob es möglich ist ffestzustellen das</p>
<pre><code>double d = 1.0;
</code></pre>
<p>eigentlich das selbe wie ein int 1 ist,</p>
<p>gibt es dafür eine Standard Bibliothek Funktion, oder muss man da mit &lt;&gt; epsilon oder ähnlichen selbst etwas schreiben?</p>
<p>vielleicht noch als Zusatz Erklärung:</p>
<blockquote>
<p>sqlite&gt; create table t1 (a NUM);<br />
sqlite&gt; insert into t1 values (1.0);<br />
sqlite&gt; select * from t1;<br />
a<br />
1</p>
</blockquote>
<p>es scheint irgendwo, entweder schon beim einfügen, oder beim anzeigen der Query, scheint sqlite3 sich ziemlich sicher zu sein das 1.0 eigentlich eine 1 ist.</p>
<p>da sqlite3 ein C program ist, jedoch viel zu gross für mich um da irgendetwas zu finden, frage ich mich ob das eine Standard Funktion ist oder nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2511412</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2511412</guid><dc:creator><![CDATA[kurze_frage]]></dc:creator><pubDate>Thu, 13 Oct 2016 07:20:58 GMT</pubDate></item><item><title><![CDATA[Reply to feststellen ob ein double eine natürliche Zahl ist on Thu, 13 Oct 2016 07:48:43 GMT]]></title><description><![CDATA[<p>Ganze Zahlen lassen sich auch in einem double (zumindest wenn es sich um ieee754 handelt) exakt und verlustfrei darstellen, du kannst einen double also mit == auf 1.0 vergleichen und brauchst kein epsilon.</p>
<p>Das geht natürlich nur bis zu einer bestimmten Grenze, ab der dann nicht mehr alle Zahlen darstellbar sind. Die Grenze liegt so bei 2^53.</p>
<p>9007199254740992 ist noch darstellbar, aber wenn man dann 1 addiert, bleibt die Zahl gleich, d.h. 9007199254740993 gibt es in double dann nicht mehr. Ab dann musst du dir Sorgen machen, wenn du auf Gleichheit mit ganzen Zahlen testen willst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2511417</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2511417</guid><dc:creator><![CDATA[wob]]></dc:creator><pubDate>Thu, 13 Oct 2016 07:48:43 GMT</pubDate></item><item><title><![CDATA[Reply to feststellen ob ein double eine natürliche Zahl ist on Thu, 13 Oct 2016 07:48:46 GMT]]></title><description><![CDATA[<p>SQL != C. SQLite wird diese Zahlen intern anders darstellen als als C float.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2511418</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2511418</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 13 Oct 2016 07:48:46 GMT</pubDate></item><item><title><![CDATA[Reply to feststellen ob ein double eine natürliche Zahl ist on Thu, 13 Oct 2016 08:03:09 GMT]]></title><description><![CDATA[<p>erst mal danke, aber ich denke ich muss meine Frage noch mal nach formulieren.</p>
<p>eventuell so:<br />
Ist feststellbar ob ein double verlustfrei in einen Integer (int64_t) konvertiert werden kann</p>
<p>ist dieser, noch pseudo- Code zu naiv?,</p>
<pre><code>int64_t convertOrThrow(double d)
{
 int64_t i = static_cast&lt;int64_t&gt;(d);

 if ( d - i != 0.0)
  throw &quot;verlust&quot;

 return i ;
}
</code></pre>
<p>geht das besser?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2511420</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2511420</guid><dc:creator><![CDATA[kurze_frage]]></dc:creator><pubDate>Thu, 13 Oct 2016 08:03:09 GMT</pubDate></item><item><title><![CDATA[Reply to feststellen ob ein double eine natürliche Zahl ist on Thu, 13 Oct 2016 08:17:32 GMT]]></title><description><![CDATA[<p>Du kannst alternativ testen, ob</p>
<pre><code>double d;
...
if (trunc(d) == d) 
  //ich bin ganzzahlig

//oder:
double dummy;
if (modf(d, &amp;dummy) == 0.0) 
  // ich bin ganzzahlig
</code></pre>
<p>Diese Tests auf Ganzzahligkeit bleiben beide innerhalb von double, konvertieren also nicht in int. Bei sehr großen Zahlen müsstest du eben noch zusätzlich testen, ob die überhaupt in den int passen.</p>
<p>Dein Code geht natürlich auch - wobei ich die exception doch etwas hart finde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2511423</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2511423</guid><dc:creator><![CDATA[wob]]></dc:creator><pubDate>Thu, 13 Oct 2016 08:17:32 GMT</pubDate></item><item><title><![CDATA[Reply to feststellen ob ein double eine natürliche Zahl ist on Thu, 13 Oct 2016 09:09:33 GMT]]></title><description><![CDATA[<pre><code>zahl == (double)(int)zahl?
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2511431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2511431</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 13 Oct 2016 09:09:33 GMT</pubDate></item><item><title><![CDATA[Reply to feststellen ob ein double eine natürliche Zahl ist on Fri, 14 Oct 2016 06:06:53 GMT]]></title><description><![CDATA[<p>Ich hätte jetzt mal das.<br />
wobei ich nicht weiss wie man die requirements auf template paramters legt, daher habe ich diese nur als Kommentar dazu geschrieben.</p>
<pre><code>template&lt;typename InT, typename OutT&gt;
// requires std::is_integral&lt;OutT&gt;::value
// requires is_floating_point&lt;InT&gt;::value
inline
OutT losslessConvert1 (InT in)
{
  InT converted = std::trunc (in);
  if(in - converted != 0.0)
    throw std::out_of_range(&quot;nein!&quot;) ;

  using limit = std::numeric_limits&lt;OutT&gt; ;
  if (converted &lt; limit::min() || converted &gt; limit::max())
    throw std::out_of_range(&quot;nein, aber fast!&quot;) ;

  return static_cast&lt;OutT&gt;(converted);    

}

template&lt;typename InT, typename OutT&gt;
// requires std::is_integral&lt;OutT&gt;::value
// requires is_floating_point&lt;InT&gt;::value
inline 
OutT losslessConvert2 (InT in)
{
  InT converted{0.0}; 
  InT fraction = std::modf (in, &amp;converted) ;

  if(fraction != 0.0)
    throw std::out_of_range(&quot;nein!&quot;) ;

  using limit = std::numeric_limits&lt;OutT&gt; ;
  if (converted &lt; limit::min() || converted &gt; limit::max())
    throw std::out_of_range(&quot;nein, aber fast!&quot;) ;

  return static_cast&lt;OutT&gt;(converted);    

}
</code></pre>
<p>die Exception wenn es nicht geht ist gewollt, wird aber wohl einen besseren Text bekommen.</p>
<p>wenn das so stimmt müsste ich nur noch rausfinden welche Version die schnellere ist.</p>
<p>das &quot;Test&quot; Programm</p>
<pre><code>int main()
{

  try {
    std::cout  &lt;&lt; losslessConvert1&lt;double, int&gt;(1.0) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }

  try {
    std::cout  &lt;&lt; losslessConvert1&lt;double, int&gt;(1.00001) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }

  try {
    std::cout  &lt;&lt; losslessConvert1&lt;double, int16_t&gt;(300.00) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }

  try {
    std::cout  &lt;&lt; losslessConvert1&lt;double, int8_t&gt;(300.00) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }

  try {
    std::cout  &lt;&lt; losslessConvert2&lt;double, int&gt;(1.0) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }

  try {
    std::cout  &lt;&lt; losslessConvert2&lt;double, int&gt;(1.00001) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }

  try {
    std::cout  &lt;&lt; losslessConvert2&lt;double, int16_t&gt;(300.00) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }

  try {
    std::cout  &lt;&lt; losslessConvert2&lt;double, int8_t&gt;(300.00) &lt;&lt; std::endl ;
  } catch (const std::exception&amp; e) {
    std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what () &lt;&lt; std::endl  ;  
  }
}
</code></pre>
<p>produziert</p>
<pre><code>1
Error: nein!
300
Error: nein, aber fast!
1
Error: nein!
300
Error: nein, aber fast!
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2511435</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2511435</guid><dc:creator><![CDATA[kurze_frage]]></dc:creator><pubDate>Fri, 14 Oct 2016 06:06:53 GMT</pubDate></item></channel></rss>