From c925aaceb24fd431c8deec6675c02c9819296f8c Mon Sep 17 00:00:00 2001 From: Emanuel Sprung Date: Tue, 31 Mar 2020 00:40:22 +0200 Subject: [PATCH] AK: Print double numbers with printf This patchset allows double numbers to be printed with the printf function. The fraction will always be printed as 6 digit number. This can be improved :^) --- AK/PrintfImplementation.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h index 34a7c7106a..cab0e735b5 100644 --- a/AK/PrintfImplementation.h +++ b/AK/PrintfImplementation.h @@ -177,6 +177,26 @@ template return fieldWidth; } +template +[[gnu::always_inline]] inline int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth) +{ + int length = 0; + + if (number < 0) { + putch(bufptr, '-'); + length++; + number = 0 - number; + } + + length = print_u64(putch, bufptr, (i64)number, leftPad, zeroPad, fieldWidth); + putch(bufptr, '.'); + length++; + double fraction = number - (i64)number; + // FIXME: Allow other fractions + fraction = fraction * 1000000; + return length + print_u64(putch, bufptr, (i64)fraction, leftPad, true, 6); +} + template [[gnu::always_inline]] inline int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool leftPad, bool zeroPad, u32 fieldWidth) { @@ -361,8 +381,7 @@ template #if !defined(BOOTSTRAPPER) && !defined(KERNEL) case 'g': case 'f': - // FIXME: Print as float! - ret += print_i64(putch, bufptr, (u64)va_arg(ap, double), left_pad, zeroPad, fieldWidth); + ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zeroPad, fieldWidth); break; #endif