MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / grisu2

Function grisu2

Source/Utils/json.hpp:15161–15212  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15159 */
15160 JSON_HEDLEY_NON_NULL(1)
15161 inline void grisu2(char* buf, int& len, int& decimal_exponent,
15162 diyfp m_minus, diyfp v, diyfp m_plus)
15163 {
15164 JSON_ASSERT(m_plus.e == m_minus.e);
15165 JSON_ASSERT(m_plus.e == v.e);
15166
15167 // --------(-----------------------+-----------------------)-------- (A)
15168 // m- v m+
15169 //
15170 // --------------------(-----------+-----------------------)-------- (B)
15171 // m- v m+
15172 //
15173 // First scale v (and m- and m+) such that the exponent is in the range
15174 // [alpha, gamma].
15175
15176 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
15177
15178 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
15179
15180 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
15181 const diyfp w = diyfp::mul(v, c_minus_k);
15182 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
15183 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
15184
15185 // ----(---+---)---------------(---+---)---------------(---+---)----
15186 // w- w w+
15187 // = c*m- = c*v = c*m+
15188 //
15189 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
15190 // w+ are now off by a small amount.
15191 // In fact:
15192 //
15193 // w - v * 10^k < 1 ulp
15194 //
15195 // To account for this inaccuracy, add resp. subtract 1 ulp.
15196 //
15197 // --------+---[---------------(---+---)---------------]---+--------
15198 // w- M- w M+ w+
15199 //
15200 // Now any number in [M-, M+] (bounds included) will round to w when input,
15201 // regardless of how the input rounding algorithm breaks ties.
15202 //
15203 // And digit_gen generates the shortest possible such number in [M-, M+].
15204 // Note that this does not mean that Grisu2 always generates the shortest
15205 // possible number in the interval (m-, m+).
15206 const diyfp M_minus(w_minus.f + 1, w_minus.e);
15207 const diyfp M_plus(w_plus.f - 1, w_plus.e);
15208
15209 decimal_exponent = -cached.k; // = -(-k) = k
15210
15211 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
15212 }
15213
15214 /*!
15215 v = buf * 10^decimal_exponent

Callers 1

json.hppFile · 0.85

Calls 2

grisu2_digit_genFunction · 0.85

Tested by

no test coverage detected