forked from bblanchon/ArduinoJson
Keep only one implementation of make_float()
This commit is contained in:
@ -29,21 +29,6 @@ struct FloatTraits<T, 8 /*64bits*/> {
|
|||||||
typedef int16_t exponent_type;
|
typedef int16_t exponent_type;
|
||||||
static const exponent_type exponent_max = 308;
|
static const exponent_type exponent_max = 308;
|
||||||
|
|
||||||
template <typename TExponent>
|
|
||||||
static T make_float(T m, TExponent e) {
|
|
||||||
auto powersOfTen =
|
|
||||||
e > 0 ? positiveBinaryPowersOfTen() : negativeBinaryPowersOfTen();
|
|
||||||
if (e <= 0)
|
|
||||||
e = TExponent(-e);
|
|
||||||
|
|
||||||
for (uint8_t index = 0; e != 0; index++) {
|
|
||||||
if (e & 1)
|
|
||||||
m *= powersOfTen[index];
|
|
||||||
e >>= 1;
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
static pgm_ptr<T> positiveBinaryPowersOfTen() {
|
static pgm_ptr<T> positiveBinaryPowersOfTen() {
|
||||||
ARDUINOJSON_DEFINE_PROGMEM_ARRAY( //
|
ARDUINOJSON_DEFINE_PROGMEM_ARRAY( //
|
||||||
uint64_t, factors,
|
uint64_t, factors,
|
||||||
@ -145,21 +130,6 @@ struct FloatTraits<T, 4 /*32bits*/> {
|
|||||||
typedef int8_t exponent_type;
|
typedef int8_t exponent_type;
|
||||||
static const exponent_type exponent_max = 38;
|
static const exponent_type exponent_max = 38;
|
||||||
|
|
||||||
template <typename TExponent>
|
|
||||||
static T make_float(T m, TExponent e) {
|
|
||||||
auto powersOfTen =
|
|
||||||
e > 0 ? positiveBinaryPowersOfTen() : negativeBinaryPowersOfTen();
|
|
||||||
if (e <= 0)
|
|
||||||
e = TExponent(-e);
|
|
||||||
|
|
||||||
for (uint8_t index = 0; e != 0; index++) {
|
|
||||||
if (e & 1)
|
|
||||||
m *= powersOfTen[index];
|
|
||||||
e >>= 1;
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
static pgm_ptr<T> positiveBinaryPowersOfTen() {
|
static pgm_ptr<T> positiveBinaryPowersOfTen() {
|
||||||
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors,
|
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors,
|
||||||
{
|
{
|
||||||
@ -252,4 +222,21 @@ struct FloatTraits<T, 4 /*32bits*/> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename TFloat, typename TExponent>
|
||||||
|
inline TFloat make_float(TFloat m, TExponent e) {
|
||||||
|
using traits = FloatTraits<TFloat>;
|
||||||
|
|
||||||
|
auto powersOfTen = e > 0 ? traits::positiveBinaryPowersOfTen()
|
||||||
|
: traits::negativeBinaryPowersOfTen();
|
||||||
|
if (e <= 0)
|
||||||
|
e = TExponent(-e);
|
||||||
|
|
||||||
|
for (uint8_t index = 0; e != 0; index++) {
|
||||||
|
if (e & 1)
|
||||||
|
m *= powersOfTen[index];
|
||||||
|
e >>= 1;
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -137,7 +137,7 @@ inline bool parseNumber(const char* s, VariantData& result) {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
JsonFloat final_result =
|
JsonFloat final_result =
|
||||||
traits::make_float(static_cast<JsonFloat>(mantissa), exponent);
|
make_float(static_cast<JsonFloat>(mantissa), exponent);
|
||||||
|
|
||||||
result.setFloat(is_negative ? -final_result : final_result);
|
result.setFloat(is_negative ? -final_result : final_result);
|
||||||
return true;
|
return true;
|
||||||
|
Reference in New Issue
Block a user