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 :^)
This commit is contained in:
Emanuel Sprung 2020-03-31 00:40:22 +02:00 committed by Andreas Kling
parent c7257ed827
commit c925aaceb2
Notes: sideshowbarker 2024-07-19 08:01:49 +09:00

View File

@ -177,6 +177,26 @@ template<typename PutChFunc>
return fieldWidth;
}
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)
{
@ -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