diff options
author | Emanuel Sprung <emanuel.sprung@gmail.com> | 2020-03-31 00:40:22 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-31 13:42:39 +0200 |
commit | c925aaceb24fd431c8deec6675c02c9819296f8c (patch) | |
tree | 4ab3670a7416f77e068338a469ee351cf1bc7d31 | |
parent | c7257ed827da8d7c329f9ca9d9e3f9512234644b (diff) | |
download | serenity-c925aaceb24fd431c8deec6675c02c9819296f8c.zip |
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 :^)
-rw-r--r-- | AK/PrintfImplementation.h | 23 |
1 files 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 @@ -178,6 +178,26 @@ template<typename PutChFunc> } template<typename PutChFunc> +[[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<typename PutChFunc> [[gnu::always_inline]] inline int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool leftPad, bool zeroPad, u32 fieldWidth) { if (number < 0) { @@ -361,8 +381,7 @@ template<typename PutChFunc> #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 |