Unexpected behavior when turing float tensor to int or long

xyzhang77: <p>Hi, I used .long() or torch.floor to convert float tensor to int, but got out unexpected behavior :</p>
<pre><code class="lang-python">import torch

origin = torch.tensor([2.84, 2.84, 2.84], device='cuda')
v_coords = torch.stack(torch.meshgrid([torch.arange(10, device='cuda')]*3)).reshape(3, -1).T.float()
voxel_size = 0.04
v_coords = v_coords[:5]
print(v_coords, v_coords.dtype)
w_coords = v_coords * voxel_size + origin
v_coords =(w_coords - origin) / voxel_size
print(v_coords, v_coords.dtype)

test_1 = torch.floor(v_coords)
print(test_1, test_1.dtype)
test_1 = test_1.long()
print(test_1, test_1.dtype)
test_2 = v_coords.long()
print(test_2, test_2.dtype)

test_3 = torch.floor_divide(w_coords - origin, voxel_size)
print(test_3, test_3.dtype)
</code></pre>
<p>Here’s the result:</p>
<pre><code class="lang-auto">tensor([[0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 3.],
        [0., 0., 4.]], device='cuda:0') torch.float32
tensor([[0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 1.0000],
        [0.0000, 0.0000, 2.0000],
        [0.0000, 0.0000, 3.0000],
        [0.0000, 0.0000, 4.0000]], device='cuda:0') torch.float32
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 4.]], device='cuda:0') torch.float32
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 1],
        [0, 0, 2],
        [0, 0, 4]], device='cuda:0') torch.int64
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 1],
        [0, 0, 2],
        [0, 0, 4]], device='cuda:0') torch.int64
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 4.]], device='cuda:0') torch.float32
</code></pre>
<p>If I set the origin is <code>float64</code>:</p>
<pre><code class="lang-auto">tensor([[0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 3.],
        [0., 0., 4.]], device='cuda:0') torch.float32
tensor([[0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 1.0000],
        [0.0000, 0.0000, 2.0000],
        [0.0000, 0.0000, 3.0000],
        [0.0000, 0.0000, 4.0000]], device='cuda:0', dtype=torch.float64) torch.float64
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 3.]], device='cuda:0', dtype=torch.float64) torch.float64
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 1],
        [0, 0, 2],
        [0, 0, 3]], device='cuda:0') torch.int64
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 1],
        [0, 0, 2],
        [0, 0, 3]], device='cuda:0') torch.int64
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 3.]], device='cuda:0', dtype=torch.float64) torch.float64
</code></pre>

ptrblck: <p>Could you explain what’s unexpected, as I’m getting the same results on the CPU and the GPU:</p>
<pre><code class="lang-python">tensor([[0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 3.],
        [0., 0., 4.]], device='cuda:0') torch.float32
tensor([[0.0000000000, 0.0000000000, 0.0000000000],
        [0.0000000000, 0.0000000000, 0.9999990463],
        [0.0000000000, 0.0000000000, 1.9999980927],
        [0.0000000000, 0.0000000000, 2.9999971390],
        [0.0000000000, 0.0000000000, 4.0000019073]], device='cuda:0') torch.float32
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 4.]], device='cuda:0') torch.float32
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 1],
        [0, 0, 2],
        [0, 0, 4]], device='cuda:0') torch.int64
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 1],
        [0, 0, 2],
        [0, 0, 4]], device='cuda:0') torch.int64
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 1.],
        [0., 0., 2.],
        [0., 0., 4.]], device='cuda:0') torch.float32
</code></pre>

xyzhang77: <p>Hi, thanks for your reply.<br>
I mean I expect to get the same result between the original <code>v_coords</code> and <code>test_1,2,3</code>. When I use numpy with float64,  they are all the same, but with float32, the results above occurs.<br>
And “unexpected behavior” probably has something to do with floating point numbers, not <code>floor</code>. Apologize.</p>