libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
writer_float_runtime.hpp
1#pragma once
2
15constexpr bool Writer::UsesFloatTextBackend(FormatType type)
16{
17 switch (type)
18 {
19 case FormatType::FloatScientific:
20 case FormatType::DoubleScientific:
21 case FormatType::LongDoubleScientific:
22 case FormatType::FloatGeneral:
23 case FormatType::DoubleGeneral:
24 case FormatType::LongDoubleGeneral:
25 case FormatType::FloatFixed:
26 case FormatType::DoubleFixed:
27 case FormatType::LongDoubleFixed:
28 return true;
29 default:
30 return false;
31 }
32}
33
41constexpr bool Writer::FloatEnabled(FormatType type)
42{
43 switch (type)
44 {
45 case FormatType::FloatFixed:
46 return Config::enable_float_fixed;
47 case FormatType::FloatScientific:
48 return Config::enable_float_scientific;
49 case FormatType::FloatGeneral:
50 return Config::enable_float_general;
51 case FormatType::DoubleFixed:
52 return Config::enable_float_double && Config::enable_float_fixed;
53 case FormatType::DoubleScientific:
54 return Config::enable_float_double && Config::enable_float_scientific;
55 case FormatType::DoubleGeneral:
56 return Config::enable_float_double && Config::enable_float_general;
57 case FormatType::LongDoubleFixed:
58 return Config::enable_float_long_double && Config::enable_float_fixed;
59 case FormatType::LongDoubleScientific:
60 return Config::enable_float_long_double && Config::enable_float_scientific;
61 case FormatType::LongDoubleGeneral:
62 return Config::enable_float_long_double && Config::enable_float_general;
63 default:
64 return false;
65 }
66}
67
73constexpr uint8_t Writer::DefaultFloatPrecision()
74{
75 return Config::max_float_precision < 6 ? Config::max_float_precision : 6;
76}
77
88template <typename Float>
89bool Writer::ExceedsFixedIntegerDigits(Float value, uint8_t precision)
90{
91 if (!std::isfinite(value))
92 {
93 return false;
94 }
95
96 Float rounded = RoundDecimal(value, precision);
97 if (!std::isfinite(rounded))
98 {
99 // RoundDecimal returns +infinity when value * 10^precision overflows.
100 // Treat this as exceeding the integer digit limit.
101 return true;
102 }
103 if (rounded == 0)
104 {
105 return 1 > Config::max_float_integer_digits;
106 }
107
108 auto normalized = NormalizeDecimal(rounded);
109 size_t integer_digits =
110 (normalized.exponent >= 0) ? static_cast<size_t>(normalized.exponent) + 1U : 1U;
111 return integer_digits > Config::max_float_integer_digits;
112}
113