From 9a63d3b2da20d6104accccbf13c1275cba6a7ce8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 16 Aug 2023 10:42:35 +0300 Subject: [PATCH] LibC: printf handles nan and inf --- libc/printf_impl.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libc/printf_impl.cpp b/libc/printf_impl.cpp index a4d788f2..58b8ec24 100644 --- a/libc/printf_impl.cpp +++ b/libc/printf_impl.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -106,6 +107,12 @@ 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)) + { + strcpy(buffer, "-nan"); + return; + } + int percision = 6; if (options.percision != -1) percision = options.percision; @@ -123,6 +130,12 @@ 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 (isinf(value)) + { + strcpy(buffer + offset, "inf"); + return; + } + // Round last digit value += (T)0.5 * BAN::Math::pow(10.0, -percision);