Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly print out sign of near-zero double values #47081

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion aten/src/ATen/core/ivalue.cpp
Expand Up @@ -468,12 +468,16 @@ std::ostream& IValue::repr(
if ((c == FP_NORMAL || c == FP_ZERO ) && std::abs(d) < 1e10) {
int64_t i = int64_t(d);
if (double(i) == d) {
// -0.0 (signed zero) needs to be parsed as -0.
if (i == 0 && std::signbit(d)) {
return out << "-" << i << ".";
}
return out << i << ".";
}
}
auto orig_prec = out.precision();
return out << std::setprecision(std::numeric_limits<double>::max_digits10)
<< v.toDouble() << std::setprecision(orig_prec);
<< d << std::setprecision(orig_prec);
}
case IValue::Tag::Int:
return out << v.toInt();
Expand Down
12 changes: 12 additions & 0 deletions test/test_jit.py
Expand Up @@ -15404,6 +15404,18 @@ def fn(x):
ref = a * b
self.assertEqual(test, ref)

def test_signed_float_zero(self):

class MyModule(torch.nn.Module):
def __init__(self):
super(MyModule, self).__init__()

def forward(self, x):
return torch.div(x, -0.)

inp = torch.ones(1)
self.checkModule(MyModule(), inp)

# known to be failing in tracer
EXCLUDE_TRACED = {
# The following fail due to #12024.
Expand Down