base: print() functions for floating point types

Fixes #2055
This commit is contained in:
Johannes Schlatow 2016-08-09 16:54:13 +02:00 committed by Christian Helmuth
parent f81d4cfbbb
commit acd2a40076
3 changed files with 52 additions and 0 deletions

View File

@ -120,6 +120,16 @@ namespace Genode {
print(output, (int)value);
}
/**
* Print single-precision float
*/
void print(Output &output, float);
/**
* Print double-precision float
*/
void print(Output &output, double);
/**
* Helper for the hexadecimal output of integer values
*

View File

@ -108,6 +108,39 @@ static inline void out_unsigned(T value, unsigned base, int pad,
}
/**
* Output floating point value
*/
template <typename T, typename OUT_CHAR_FN>
static inline void out_float(T value, unsigned base, unsigned length, OUT_CHAR_FN const &out_char)
{
/* set flag if value is negative */
int neg = value < 0 ? 1 : 0;
/* get absolute value */
value = value < 0 ? -value : value;
uint64_t integer = (uint64_t)value;
if (neg)
out_char('-');
out_unsigned(integer, base, 0, out_char);
out_char('.');
if (length) {
do {
value -= integer;
value = value*base;
integer = (int64_t)value;
out_char(ascii(integer));
length--;
} while (length && (value > 0.0));
}
}
namespace Genode { template <size_t, typename> class Buffered_output; }

View File

@ -75,6 +75,15 @@ void Genode::print(Output &output, long long value)
out_signed<long long>(value, 10, [&] (char c) { output.out_char(c); });
}
void Genode::print(Output &output, float value)
{
out_float<float>(value, 10, 3, [&] (char c) { output.out_char(c); });
}
void Genode::print(Output &output, double value)
{
out_float<double>(value, 10, 6, [&] (char c) { output.out_char(c); });
}
void Genode::print(Output &output, Hex const &value)
{