diff --git a/repos/base/include/base/output.h b/repos/base/include/base/output.h index 8a69af56a6..e048dbc9b6 100644 --- a/repos/base/include/base/output.h +++ b/repos/base/include/base/output.h @@ -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 * diff --git a/repos/base/src/include/base/internal/output.h b/repos/base/src/include/base/internal/output.h index 479d1306c6..43c51e467f 100644 --- a/repos/base/src/include/base/internal/output.h +++ b/repos/base/src/include/base/internal/output.h @@ -108,6 +108,39 @@ static inline void out_unsigned(T value, unsigned base, int pad, } +/** + * Output floating point value + */ +template +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 class Buffered_output; } diff --git a/repos/base/src/lib/base/output.cc b/repos/base/src/lib/base/output.cc index f8308275c8..c3fee71b0c 100644 --- a/repos/base/src/lib/base/output.cc +++ b/repos/base/src/lib/base/output.cc @@ -75,6 +75,15 @@ void Genode::print(Output &output, long long value) out_signed(value, 10, [&] (char c) { output.out_char(c); }); } +void Genode::print(Output &output, float value) +{ + out_float(value, 10, 3, [&] (char c) { output.out_char(c); }); +} + +void Genode::print(Output &output, double value) +{ + out_float(value, 10, 6, [&] (char c) { output.out_char(c); }); +} void Genode::print(Output &output, Hex const &value) {