| 15234 | */ |
| 15235 | JSON_HEDLEY_NON_NULL(1) |
| 15236 | inline void grisu2(char* buf, int& len, int& decimal_exponent, |
| 15237 | diyfp m_minus, diyfp v, diyfp m_plus) |
| 15238 | { |
| 15239 | JSON_ASSERT(m_plus.e == m_minus.e); |
| 15240 | JSON_ASSERT(m_plus.e == v.e); |
| 15241 | |
| 15242 | // --------(-----------------------+-----------------------)-------- (A) |
| 15243 | // m- v m+ |
| 15244 | // |
| 15245 | // --------------------(-----------+-----------------------)-------- (B) |
| 15246 | // m- v m+ |
| 15247 | // |
| 15248 | // First scale v (and m- and m+) such that the exponent is in the range |
| 15249 | // [alpha, gamma]. |
| 15250 | |
| 15251 | const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e); |
| 15252 | |
| 15253 | const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k |
| 15254 | |
| 15255 | // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma] |
| 15256 | const diyfp w = diyfp::mul(v, c_minus_k); |
| 15257 | const diyfp w_minus = diyfp::mul(m_minus, c_minus_k); |
| 15258 | const diyfp w_plus = diyfp::mul(m_plus, c_minus_k); |
| 15259 | |
| 15260 | // ----(---+---)---------------(---+---)---------------(---+---)---- |
| 15261 | // w- w w+ |
| 15262 | // = c*m- = c*v = c*m+ |
| 15263 | // |
| 15264 | // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and |
| 15265 | // w+ are now off by a small amount. |
| 15266 | // In fact: |
| 15267 | // |
| 15268 | // w - v * 10^k < 1 ulp |
| 15269 | // |
| 15270 | // To account for this inaccuracy, add resp. subtract 1 ulp. |
| 15271 | // |
| 15272 | // --------+---[---------------(---+---)---------------]---+-------- |
| 15273 | // w- M- w M+ w+ |
| 15274 | // |
| 15275 | // Now any number in [M-, M+] (bounds included) will round to w when input, |
| 15276 | // regardless of how the input rounding algorithm breaks ties. |
| 15277 | // |
| 15278 | // And digit_gen generates the shortest possible such number in [M-, M+]. |
| 15279 | // Note that this does not mean that Grisu2 always generates the shortest |
| 15280 | // possible number in the interval (m-, m+). |
| 15281 | const diyfp M_minus(w_minus.f + 1, w_minus.e); |
| 15282 | const diyfp M_plus (w_plus.f - 1, w_plus.e ); |
| 15283 | |
| 15284 | decimal_exponent = -cached.k; // = -(-k) = k |
| 15285 | |
| 15286 | grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus); |
| 15287 | } |
| 15288 | |
| 15289 | /*! |
| 15290 | v = buf * 10^decimal_exponent |
no test coverage detected