FastLED 3.10.6
Loading...
Searching...
No Matches

◆ format_float_scientific()

fl::string fl::printf_detail::format_float_scientific ( float value,
int precision )

Definition at line 15 of file stdio.cpp.hpp.

15 {
16 const bool negative = value < 0.0f;
17 float magnitude = negative ? -value : value;
18 int exponent = 0;
19 while (magnitude >= 10.0f) {
20 magnitude /= 10.0f;
21 ++exponent;
22 }
23 while (magnitude > 0.0f && magnitude < 1.0f) {
24 magnitude *= 10.0f;
25 --exponent;
26 }
27
28 // Normalising put the mantissa in [1, 10), but *rounding* it to the
29 // requested precision can carry it back out: 9.999e18 at precision 2
30 // rounds to 10.00, and "10.00e+18" is not scientific notation. Checked
31 // before formatting rather than patched after, so there is one place
32 // where the exponent is decided.
33 float half_step = 0.5f;
34 for (int digit = 0; digit < precision; ++digit) {
35 half_step /= 10.0f;
36 }
37 if (magnitude >= 10.0f - half_step) {
38 magnitude /= 10.0f;
39 ++exponent;
40 }
41
42 sstream stream;
43 if (negative) {
44 stream << "-";
45 }
46 // The mantissa is in [1, 10) now, so the ordinary path renders it.
47 stream << format_float(magnitude, precision);
48 stream << "e";
49 if (exponent < 0) {
50 stream << "-";
51 exponent = -exponent;
52 } else {
53 stream << "+";
54 }
55 if (exponent < 10) {
56 stream << "0";
57 }
58 stream << exponent;
59 return stream.str();
60}
fl::string format_float(float value, int precision) FL_NO_EXCEPT
Definition stdio.h:341
constexpr int type_rank< T >::value

References fl::FL_NO_EXCEPT, format_float(), fl::sstream::str(), and fl::type_rank< T >::value.

Referenced by format_float().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: