From 915dea01c96004b84588f084b6a4166aae2cd024 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 16 Feb 2024 15:34:24 +0200 Subject: [PATCH] LibC: fix printf %e for inf/nan values --- libc/printf_impl.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/libc/printf_impl.cpp b/libc/printf_impl.cpp index 040b786df1..85524a6f05 100644 --- a/libc/printf_impl.cpp +++ b/libc/printf_impl.cpp @@ -122,17 +122,6 @@ static void integer_to_string(char* buffer, T value, int base, bool upper, forma template static void floating_point_to_string(char* buffer, T value, bool upper, const format_options_t options) { - if (isnan(value)) - { - if (value < (T)0.0) - { - *buffer = '-'; - buffer++; - } - strcpy(buffer, upper ? "NAN" : "nan"); - return; - } - int percision = 6; if (options.percision != -1) percision = options.percision; @@ -150,6 +139,11 @@ static void floating_point_to_string(char* buffer, T value, bool upper, const fo else if (options.show_plus_sign_as_space) buffer[offset++] = ' '; + if (isnan(value)) + { + strcpy(buffer + offset, upper ? "NAN" : "nan"); + return; + } if (isinf(value)) { strcpy(buffer + offset, upper ? "INF" : "inf"); @@ -223,6 +217,17 @@ static void floating_point_to_exponent_string(char* buffer, T value, bool upper, else if (options.show_plus_sign_as_space) buffer[offset++] = ' '; + if (isnan(value)) + { + strcpy(buffer + offset, upper ? "NAN" : "nan"); + return; + } + if (isinf(value)) + { + strcpy(buffer + offset, upper ? "INF" : "inf"); + return; + } + // Calculate which number to put as exponent int exponent = 0; if (value != (T)0.0)