LibC: printf handles nan and inf

This commit is contained in:
Bananymous 2023-08-16 10:42:35 +03:00
parent 3a36c30e80
commit 569e76a848
1 changed files with 13 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <bits/printf.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
@ -106,6 +107,12 @@ static void integer_to_string(char* buffer, T value, int base, bool upper, forma
template<BAN::floating_point T>
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<T>(10.0, -percision);