mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 05:35:52 +03:00
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:
parent
c7257ed827
commit
c925aaceb2
Notes:
sideshowbarker
2024-07-19 08:01:49 +09:00
Author: https://github.com/lnzero1dev Commit: https://github.com/SerenityOS/serenity/commit/c925aaceb24 Pull-request: https://github.com/SerenityOS/serenity/pull/1550
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user