Skip to content

floor_divide truncates instead of flooring #43874

@jackm321

Description

@jackm321

🐛 Bug

PyTorch's floor_divide (//) operator doesn't match Python's behavior on negative numbers. Python floors (rounds down) while PyTorch truncates (rounds towards zero).

To Reproduce

Steps to reproduce the behavior:

import torch
import numpy as np

print("python -3//2 =", -3 // 2)
print("numpy -3//2 =", np.array((-3)) // 2)
print("pytorch -3//2 =", torch.tensor([-3]) // 2)

# output:
# python -3//2 = -2
# numpy -3//2 = -2
# pytorch -3//2 = tensor([-1])

Expected behavior

Expect PyTorch's floor_divide to match Python // operator by flooring division results instead of truncating them.

Environment


 - PyTorch Version (e.g., 1.0): master
 - OS (e.g., Linux): Linux
 - How you installed PyTorch (`conda`, `pip`, source): source
 - Build command you used (if compiling from source): 
 - Python version: Python 3.7.5
 - CUDA/cuDNN version: n/a
 - GPU models and configuration: n/a
 - Any other relevant information:

cc @ezyang @gchanan @zou3519 @bdhirsh @jbschlosser

Metadata

Metadata

Assignees

Labels

high prioritymodule: correctness (silent)issue that returns an incorrect result silentlymodule: deprecationtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions