MCPcopy Create free account
hub / github.com/AllentDan/LibtorchTutorials / grisu2

Function grisu2

lesson6-Segmentation/json.hpp:15162–15213  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

json.hppFile · 0.85

Calls 2

grisu2_digit_genFunction · 0.85

Tested by

no test coverage detected