From ed11ba3fe4ec7339c2ccd4f49e320e10e4d3764b Mon Sep 17 00:00:00 2001 From: John Richards Date: Thu, 15 Oct 2020 21:35:19 -0400 Subject: [PATCH] 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(''). --- bytecode.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/bytecode.cpp b/bytecode.cpp index 02849c3..0274808 100644 --- a/bytecode.cpp +++ b/bytecode.cpp @@ -272,8 +272,19 @@ void print_const(PycRef obj, PycModule* mod) { // Wrap any nan/inf values in float(''). double value = obj.cast()->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); }