LibC: fix printf %e for inf/nan values

This commit is contained in:
Bananymous 2024-02-16 15:34:24 +02:00
parent 566bb73897
commit 915dea01c9
1 changed files with 16 additions and 11 deletions

View File

@ -122,17 +122,6 @@ static void integer_to_string(char* buffer, T value, int base, bool upper, forma
template<BAN::floating_point T> template<BAN::floating_point T>
static void floating_point_to_string(char* buffer, T value, bool upper, const format_options_t options) 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; int percision = 6;
if (options.percision != -1) if (options.percision != -1)
percision = options.percision; 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) else if (options.show_plus_sign_as_space)
buffer[offset++] = ' '; buffer[offset++] = ' ';
if (isnan(value))
{
strcpy(buffer + offset, upper ? "NAN" : "nan");
return;
}
if (isinf(value)) if (isinf(value))
{ {
strcpy(buffer + offset, upper ? "INF" : "inf"); 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) else if (options.show_plus_sign_as_space)
buffer[offset++] = ' '; 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 // Calculate which number to put as exponent
int exponent = 0; int exponent = 0;
if (value != (T)0.0) if (value != (T)0.0)