|
CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
|
This file provides routines to help convert from floating-point values to strings. More...
Classes | |
| struct | FloatParts |
| A floating-point value decomposed into components for printing. More... | |
Concepts | |
| concept | SupportedFloatingPointType |
| Set of supported floating-point types. | |
Functions | |
| template<SupportedFloatingPointType T> | |
| int | normalize (T &value) |
| Scale the floating-point value up or down in powers of ten so that the decimal point will end up in a sensible place. | |
| constexpr uint32_t | pow10 (int exponent) |
| template<SupportedFloatingPointType T> | |
| FloatParts | decompose_float (T value, int8_t decimalPlaces) |
Variables | |
| constexpr size_t | NumberOfPowersOfTen = 9 |
| Number of powers of ten required to represent all values that fit in any supported floating-point type. | |
| template<typename T> | |
| constexpr size_t | NecessaryNumberOfPowersOfTen |
| Helper that computes the necessary value of NumberOfPowersOfTen for a given type. | |
| template<typename T> | |
| constexpr std::array< T, NumberOfPowersOfTen > | PositiveBinaryPowersOfTen |
| Table of powers of ten. | |
| template<typename T> | |
| constexpr std::array< T, NumberOfPowersOfTen > | NegativeBinaryPowersOfTen |
| Table of negative powers of ten. | |
| template<typename T> | |
| constexpr T | PositiveExponentiationThreshold = 1e7 |
| Largest value that is printed without an exponent. | |
| template<typename T> | |
| constexpr T | NegativeExponentiationThreshold = 1e-5 |
| Smallest value that is printed without an exponent. | |
This file provides routines to help convert from floating-point values to strings.
This uses the algorithm described here:
https://blog.benoitblanchon.fr/lightweight-float-to-string/
This approach is optimised for code size, not performance or accuracy. It will give more rounding errors than common implementations but is a fraction of the code size.
| FloatParts anonymous_namespace{float_to_string_helpers.hh}::decompose_float | ( | T | value, |
| int8_t | decimalPlaces ) |
Definition at line 171 of file float_to_string_helpers.hh.
|
inline |
Scale the floating-point value up or down in powers of ten so that the decimal point will end up in a sensible place.
The return value is the number of powers of ten (positive or negative) that the value was scaled by.
This uses binary exponentiation, see the blog post linked at the top of the file for more details.
Definition at line 107 of file float_to_string_helpers.hh.
References NecessaryNumberOfPowersOfTen, NegativeBinaryPowersOfTen, NegativeExponentiationThreshold, PositiveBinaryPowersOfTen, and PositiveExponentiationThreshold.
|
constexpr |
Definition at line 165 of file float_to_string_helpers.hh.
|
constexpr |
Helper that computes the necessary value of NumberOfPowersOfTen for a given type.
Definition at line 40 of file float_to_string_helpers.hh.
Referenced by normalize().
|
constexpr |
Table of negative powers of ten.
These are expensive to compute dynamically.
Definition at line 77 of file float_to_string_helpers.hh.
Referenced by normalize().
|
constexpr |
Smallest value that is printed without an exponent.
Definition at line 94 of file float_to_string_helpers.hh.
Referenced by normalize().
|
constexpr |
Number of powers of ten required to represent all values that fit in any supported floating-point type.
9 is sufficient for IEEE 754 64-bit floating-point values. Currently, CHERIoT does not provide a long double type.
Definition at line 33 of file float_to_string_helpers.hh.
|
constexpr |
Table of powers of ten.
These are expensive to compute dynamically.
Definition at line 61 of file float_to_string_helpers.hh.
Referenced by normalize().
|
constexpr |
Largest value that is printed without an exponent.
Definition at line 90 of file float_to_string_helpers.hh.
Referenced by normalize().