Refactors how we handle NaN and infinity

The previous approach didn't work properly under MSVC. The wrong
output would be produced for NaN values.

This new approach will directly print out "nan" and "inf" with the
appropriate sign, rather than just wrapping the value in float('').
This commit is contained in:
John Richards
2020-10-15 21:35:19 -04:00
parent 870ecdc430
commit ed11ba3fe4

View File

@@ -272,8 +272,19 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
{
// Wrap any nan/inf values in float('').
double value = obj.cast<PycCFloat>()->value();
if (std::isnan(value) || std::isinf(value)) {
fprintf(pyc_output, "float('%g')", value);
bool is_negative = std::signbit(value);
if (std::isnan(value)) {
if (is_negative) {
fprintf(pyc_output, "float('-nan')");
} else {
fprintf(pyc_output, "float('nan')");
}
} else if (std::isinf(value)) {
if (is_negative) {
fprintf(pyc_output, "float('-inf')");
} else {
fprintf(pyc_output, "float('inf')");
}
} else {
fprintf(pyc_output, "%g", value);
}