mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00: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
1 changed files with 21 additions and 2 deletions
|
@ -177,6 +177,26 @@ template<typename PutChFunc>
|
||||||
return fieldWidth;
|
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>
|
template<typename PutChFunc>
|
||||||
[[gnu::always_inline]] inline int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool leftPad, bool zeroPad, u32 fieldWidth)
|
[[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)
|
#if !defined(BOOTSTRAPPER) && !defined(KERNEL)
|
||||||
case 'g':
|
case 'g':
|
||||||
case 'f':
|
case 'f':
|
||||||
// FIXME: Print as float!
|
ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zeroPad, fieldWidth);
|
||||||
ret += print_i64(putch, bufptr, (u64)va_arg(ap, double), left_pad, zeroPad, fieldWidth);
|
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue