<h3 dir="auto">Describe the issue:</h3>
<p dir="auto">We have the following code:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="I_wb = I_wb.astype(np.uint16)"><pre class="notranslate"><span class="pl-v">I_wb</span> <span class="pl-c1">=</span> <span class="pl-v">I_wb</span>.<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint16</span>)</pre></div>
<p dir="auto">I_wb was float32 before the above conversion.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/326807/148121640-5a047b35-ef45-456c-90d2-407030bc51c4.png"><img width="811" alt="Screen Shot 2022-01-04 at 12 31 16 PM" src="https://user-images.githubusercontent.com/326807/148121640-5a047b35-ef45-456c-90d2-407030bc51c4.png" style="max-width: 100%;"></a></p>
<p dir="auto">We noticed that when overflow happens, astype could behave differently on different platforms.</p>
<p dir="auto">as shown in the above image, when running the same code on mac, the value 68469.13 will be truncated to 65535, whereas on linux, the value is applied with a modulo operation . 68469.13 % 65535</p>
<p dir="auto">on both sides, the code uses numpy 1.21.2</p>
<p dir="auto">also tried 1.22.0, it's the same.</p>
<p dir="auto">note: The shape of the data matters,</p>
<p dir="auto">for example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="I2 = np.zeros((2448, 2048, 3), dtype=&quot;float32&quot;)
I2[1700,970] = [68469.13, 65472.,   65535.]"><pre class="notranslate"><span class="pl-v">I2</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-c1">2448</span>, <span class="pl-c1">2048</span>, <span class="pl-c1">3</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"float32"</span>)
<span class="pl-v">I2</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>] <span class="pl-c1">=</span> [<span class="pl-c1">68469.13</span>, <span class="pl-c1">65472.</span>,   <span class="pl-c1">65535.</span>]</pre></div>
<p dir="auto">The above can reproduce, the value 68469.13 gets truncated on certain platforms, but applied with modulo on other platforms.</p>
<p dir="auto">Whereas the following won't reproduce:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="I = np.array([68469.13, 65472.,   65535.], dtype=&quot;float32&quot;)"><pre class="notranslate"><span class="pl-v">I</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">68469.13</span>, <span class="pl-c1">65472.</span>,   <span class="pl-c1">65535.</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"float32"</span>)</pre></div>
<p dir="auto">on both platforms I tried, it behaves the same ( modulo)</p>
<p dir="auto">could this be due to the underlining cpp compiler and optimization level (clang vs. gcc) ?</p>
<h3 dir="auto">Reproduce the code example:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import sys

print(np.__version__ , sys.version)

I2 = np.zeros((2448, 2048, 3), dtype=&quot;float32&quot;)
I2[1700,970] = [68469.13, 65472.,   65535.]
print(&quot;pixel color: &quot;, I2[1700,970])
I2_wb = I2.astype(np.uint16)
print(I2_wb[1700,970])"><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-k">import</span> <span class="pl-s1">sys</span>

<span class="pl-en">print</span>(<span class="pl-s1">np</span>.<span class="pl-s1">__version__</span> , <span class="pl-s1">sys</span>.<span class="pl-s1">version</span>)

<span class="pl-v">I2</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-c1">2448</span>, <span class="pl-c1">2048</span>, <span class="pl-c1">3</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"float32"</span>)
<span class="pl-v">I2</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>] <span class="pl-c1">=</span> [<span class="pl-c1">68469.13</span>, <span class="pl-c1">65472.</span>,   <span class="pl-c1">65535.</span>]
<span class="pl-en">print</span>(<span class="pl-s">"pixel color: "</span>, <span class="pl-v">I2</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>])
<span class="pl-v">I2_wb</span> <span class="pl-c1">=</span> <span class="pl-v">I2</span>.<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint16</span>)
<span class="pl-en">print</span>(<span class="pl-v">I2_wb</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>])</pre></div>
<h3 dir="auto">Error message:</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="compare the reproduce code on different platforms, notice output difference even with matched numpy version."><pre class="notranslate">compare the reproduce code on different platforms, notice output difference even with matched numpy version.</pre></div>
<h3 dir="auto">NumPy/Python version information:</h3>
<p dir="auto">1.22.0<br>
1.21.2</p>
<p dir="auto">sample output on my mac (value truncated):</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="1.21.2 3.9.1 (default, Oct 15 2021, 13:26:04)
[Clang 13.0.0 (clang-1300.0.29.3)]
pixel color:  [68469.13 65472.   65535.  ]
[65535 65472 65535]"><pre class="notranslate">1.21.2 3.9.1 (default, Oct 15 2021, 13:26:04)
[Clang 13.0.0 (clang-1300.0.29.3)]
pixel color:  [68469.13 65472.   65535.  ]
[65535 65472 65535]</pre></div>
<p dir="auto">sample output on my linux (modulo)</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="1.21.2 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0]
pixel color:  [68469.13 65472.   65535.  ]
[ 2933 65472 65535]
"><pre class="notranslate">1.21.2 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0]
pixel color:  [68469.13 65472.   65535.  ]
[ 2933 65472 65535]
</pre></div>
<h3 dir="auto">Describe the issue:</h3>
<p dir="auto">We have the following code:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="I_wb = I_wb.astype(np.uint16)"><pre class="notranslate"><span class="pl-v">I_wb</span> <span class="pl-c1">=</span> <span class="pl-v">I_wb</span>.<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint16</span>)</pre></div>
<p dir="auto">I_wb was float32 before the above conversion.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/326807/148121640-5a047b35-ef45-456c-90d2-407030bc51c4.png"><img width="811" alt="Screen Shot 2022-01-04 at 12 31 16 PM" src="https://user-images.githubusercontent.com/326807/148121640-5a047b35-ef45-456c-90d2-407030bc51c4.png" style="max-width: 100%;"></a></p>
<p dir="auto">We noticed that when overflow happens, astype could behave differently on different platforms.</p>
<p dir="auto">as shown in the above image, when running the same code on mac, the value 68469.13 will be truncated to 65535, whereas on linux, the value is applied with a modulo operation . 68469.13 % 65535</p>
<p dir="auto">on both sides, the code uses numpy 1.21.2</p>
<p dir="auto">also tried 1.22.0, it's the same.</p>
<p dir="auto">note: The shape of the data matters,</p>
<p dir="auto">for example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="I2 = np.zeros((2448, 2048, 3), dtype=&quot;float32&quot;)
I2[1700,970] = [68469.13, 65472.,   65535.]"><pre class="notranslate"><span class="pl-v">I2</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-c1">2448</span>, <span class="pl-c1">2048</span>, <span class="pl-c1">3</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"float32"</span>)
<span class="pl-v">I2</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>] <span class="pl-c1">=</span> [<span class="pl-c1">68469.13</span>, <span class="pl-c1">65472.</span>,   <span class="pl-c1">65535.</span>]</pre></div>
<p dir="auto">The above can reproduce, the value 68469.13 gets truncated on certain platforms, but applied with modulo on other platforms.</p>
<p dir="auto">Whereas the following won't reproduce:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="I = np.array([68469.13, 65472.,   65535.], dtype=&quot;float32&quot;)"><pre class="notranslate"><span class="pl-v">I</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">68469.13</span>, <span class="pl-c1">65472.</span>,   <span class="pl-c1">65535.</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"float32"</span>)</pre></div>
<p dir="auto">on both platforms I tried, it behaves the same ( modulo)</p>
<p dir="auto">could this be due to the underlining cpp compiler and optimization level (clang vs. gcc) ?</p>
<h3 dir="auto">Reproduce the code example:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import sys

print(np.__version__ , sys.version)

I2 = np.zeros((2448, 2048, 3), dtype=&quot;float32&quot;)
I2[1700,970] = [68469.13, 65472.,   65535.]
print(&quot;pixel color: &quot;, I2[1700,970])
I2_wb = I2.astype(np.uint16)
print(I2_wb[1700,970])"><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-k">import</span> <span class="pl-s1">sys</span>

<span class="pl-en">print</span>(<span class="pl-s1">np</span>.<span class="pl-s1">__version__</span> , <span class="pl-s1">sys</span>.<span class="pl-s1">version</span>)

<span class="pl-v">I2</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-c1">2448</span>, <span class="pl-c1">2048</span>, <span class="pl-c1">3</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"float32"</span>)
<span class="pl-v">I2</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>] <span class="pl-c1">=</span> [<span class="pl-c1">68469.13</span>, <span class="pl-c1">65472.</span>,   <span class="pl-c1">65535.</span>]
<span class="pl-en">print</span>(<span class="pl-s">"pixel color: "</span>, <span class="pl-v">I2</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>])
<span class="pl-v">I2_wb</span> <span class="pl-c1">=</span> <span class="pl-v">I2</span>.<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint16</span>)
<span class="pl-en">print</span>(<span class="pl-v">I2_wb</span>[<span class="pl-c1">1700</span>,<span class="pl-c1">970</span>])</pre></div>
<h3 dir="auto">Error message:</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="compare the reproduce code on different platforms, notice output difference even with matched numpy version."><pre class="notranslate">compare the reproduce code on different platforms, notice output difference even with matched numpy version.</pre></div>
<h3 dir="auto">NumPy/Python version information:</h3>
<p dir="auto">1.22.0<br>
1.21.2</p>
<p dir="auto">sample output on my mac (value truncated):</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="1.21.2 3.9.1 (default, Oct 15 2021, 13:26:04)
[Clang 13.0.0 (clang-1300.0.29.3)]
pixel color:  [68469.13 65472.   65535.  ]
[65535 65472 65535]"><pre class="notranslate">1.21.2 3.9.1 (default, Oct 15 2021, 13:26:04)
[Clang 13.0.0 (clang-1300.0.29.3)]
pixel color:  [68469.13 65472.   65535.  ]
[65535 65472 65535]</pre></div>
<p dir="auto">sample output on my linux (modulo)</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="1.21.2 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0]
pixel color:  [68469.13 65472.   65535.  ]
[ 2933 65472 65535]
"><pre class="notranslate">1.21.2 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0]
pixel color:  [68469.13 65472.   65535.  ]
[ 2933 65472 65535]
</pre></div>