<p dir="auto"><strong>List of Python 8 byte integers are not always correctly converted to np.uint64 arrays.</strong></p>
<p dir="auto">Lists of 64 bit integers are inconsistently converted into np.arrays.  For example, If you convert the list  "test" below, the values in the array are not the same as the values in the original list.  If, however, you take the first 3 items they do get converted correctly.  If you convert the items individually you also get the correct results.</p>
<p dir="auto">Any views?</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
test =[2573020360226874006, 3380361604350789900, 4859923904345895640, 13687853399390180967]


print(&quot;A list of 4 integers - Note: they are not identical&quot;)
print(test)
converted_numbers = np.array(test).astype(np.uint64)
print(converted_numbers)
print(&quot; &quot;)

print(&quot;The first 3 integers - Note they are identical&quot;)
test1 = test[:-1]
print(test1)
converted_numbers1 = np.array(test1).astype(np.uint64)
print(converted_numbers1)
print(&quot; &quot;)

print(&quot;The last integer&quot;)
test2 = test[-1]
print(test2)
converted_numbers2 = np.array([test2]).astype(np.uint64)
print(converted_numbers2)

print(&quot;\n&quot;,&quot;The integers converted individually&quot;)
for num in test:
	z = np.uint64(num)
	print(num,z)
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">test</span> <span class="pl-c1">=</span>[<span class="pl-c1">2573020360226874006</span>, <span class="pl-c1">3380361604350789900</span>, <span class="pl-c1">4859923904345895640</span>, <span class="pl-c1">13687853399390180967</span>]


<span class="pl-en">print</span>(<span class="pl-s">"A list of 4 integers - Note: they are not identical"</span>)
<span class="pl-en">print</span>(<span class="pl-s1">test</span>)
<span class="pl-s1">converted_numbers</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>(<span class="pl-s1">test</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint64</span>)
<span class="pl-en">print</span>(<span class="pl-s1">converted_numbers</span>)
<span class="pl-en">print</span>(<span class="pl-s">" "</span>)

<span class="pl-en">print</span>(<span class="pl-s">"The first 3 integers - Note they are identical"</span>)
<span class="pl-s1">test1</span> <span class="pl-c1">=</span> <span class="pl-s1">test</span>[:<span class="pl-c1">-</span><span class="pl-c1">1</span>]
<span class="pl-en">print</span>(<span class="pl-s1">test1</span>)
<span class="pl-s1">converted_numbers1</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>(<span class="pl-s1">test1</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint64</span>)
<span class="pl-en">print</span>(<span class="pl-s1">converted_numbers1</span>)
<span class="pl-en">print</span>(<span class="pl-s">" "</span>)

<span class="pl-en">print</span>(<span class="pl-s">"The last integer"</span>)
<span class="pl-s1">test2</span> <span class="pl-c1">=</span> <span class="pl-s1">test</span>[<span class="pl-c1">-</span><span class="pl-c1">1</span>]
<span class="pl-en">print</span>(<span class="pl-s1">test2</span>)
<span class="pl-s1">converted_numbers2</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-s1">test2</span>]).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint64</span>)
<span class="pl-en">print</span>(<span class="pl-s1">converted_numbers2</span>)

<span class="pl-en">print</span>(<span class="pl-s">"<span class="pl-cce">\n</span>"</span>,<span class="pl-s">"The integers converted individually"</span>)
<span class="pl-k">for</span> <span class="pl-s1">num</span> <span class="pl-c1">in</span> <span class="pl-s1">test</span>:
	<span class="pl-s1">z</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">uint64</span>(<span class="pl-s1">num</span>)
	<span class="pl-en">print</span>(<span class="pl-s1">num</span>,<span class="pl-s1">z</span>)</pre></div>
<p dir="auto"><strong>Output from the above</strong><br>
A list of 4 integers - Note: they are not identical<br>
[2573020360226874006, 3380361604350789900, 4859923904345895640, 13687853399390180967]<br>
[ 2573020360226873856  3380361604350790144  4859923904345895936 13687853399390181376]</p>
<p dir="auto">The first 3 integers - Note they are identical<br>
[2573020360226874006, 3380361604350789900, 4859923904345895640]<br>
[2573020360226874006 3380361604350789900 4859923904345895640]</p>
<p dir="auto">The last integer<br>
13687853399390180967<br>
[13687853399390180967]</p>
<p dir="auto">The integers converted individually<br>
2573020360226874006 2573020360226874006<br>
3380361604350789900 3380361604350789900<br>
4859923904345895640 4859923904345895640<br>
13687853399390180967 13687853399390180967</p>
<h3 dir="auto">Numpy/Python version information:</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/41059217/79859329-be711000-83c8-11ea-8e37-f0e6d7b80fc5.png"><img src="https://user-images.githubusercontent.com/41059217/79859329-be711000-83c8-11ea-8e37-f0e6d7b80fc5.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><code class="notranslate">math.floor()</code> and <code class="notranslate">math.ceil()</code> can produce incorrect result for <code class="notranslate">float128</code> due to double rounding. <code class="notranslate">math.trunc()</code> doe not work with <code class="notranslate">float128</code> (but <code class="notranslate">int()</code> works).</p>
<h3 dir="auto">Reproducing code example:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; import math
&gt;&gt;&gt; x = np.float128('0.99999999999999999995')
&gt;&gt;&gt; x &lt; 1
True
&gt;&gt;&gt; np.floor(x)
0.0
&gt;&gt;&gt; math.floor(x)
1
&gt;&gt;&gt; np.trunc(x)
0.0
&gt;&gt;&gt; y = np.float128('1.0000000000000000001')
&gt;&gt;&gt; y &gt; 1
True
&gt;&gt;&gt; np.ceil(y)
2.0
&gt;&gt;&gt; math.ceil(y)
1
&gt;&gt;&gt; z = np.float128()
&gt;&gt;&gt; np.trunc(z)
0.0
&gt;&gt;&gt; math.trunc(z)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
TypeError: type numpy.float128 doesn't define __trunc__ method
&gt;&gt;&gt; int(z)
0"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">math</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">float128</span>(<span class="pl-s">'0.99999999999999999995'</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span> <span class="pl-c1">&lt;</span> <span class="pl-c1">1</span>
<span class="pl-c1">True</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">floor</span>(<span class="pl-s1">x</span>)
<span class="pl-c1">0.0</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">math</span>.<span class="pl-en">floor</span>(<span class="pl-s1">x</span>)
<span class="pl-c1">1</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">trunc</span>(<span class="pl-s1">x</span>)
<span class="pl-c1">0.0</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">float128</span>(<span class="pl-s">'1.0000000000000000001'</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">y</span> <span class="pl-c1">&gt;</span> <span class="pl-c1">1</span>
<span class="pl-c1">True</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">ceil</span>(<span class="pl-s1">y</span>)
<span class="pl-c1">2.0</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">math</span>.<span class="pl-en">ceil</span>(<span class="pl-s1">y</span>)
<span class="pl-c1">1</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">z</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">float128</span>()
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">trunc</span>(<span class="pl-s1">z</span>)
<span class="pl-c1">0.0</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">math</span>.<span class="pl-en">trunc</span>(<span class="pl-s1">z</span>)
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
<span class="pl-v">TypeError</span>: <span class="pl-s1">type</span> <span class="pl-s1">numpy</span>.<span class="pl-s1">float128</span> <span class="pl-s1">doesn</span>'<span class="pl-s1">t</span> <span class="pl-s1">define</span> <span class="pl-s1">__trunc__</span> <span class="pl-s1">method</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">int</span>(<span class="pl-s1">z</span>)
<span class="pl-c1">0</span></pre></div>
<p dir="auto">This can be solved by implementing <code class="notranslate">__floor__</code>, <code class="notranslate">__ceil__</code> and <code class="notranslate">__trunc__</code> in <code class="notranslate">float128</code>.</p>
<h3 dir="auto">Numpy/Python version information:</h3>
<p dir="auto">1.13.3 3.6.8 (default, Oct  7 2019, 12:59:55)<br>
[GCC 8.3.0]</p>