| 17733 | */ |
| 17734 | JSON_HEDLEY_NON_NULL(1) |
| 17735 | inline void grisu2(char* buf, int& len, int& decimal_exponent, |
| 17736 | diyfp m_minus, diyfp v, diyfp m_plus) |
| 17737 | { |
| 17738 | JSON_ASSERT(m_plus.e == m_minus.e); |
| 17739 | JSON_ASSERT(m_plus.e == v.e); |
| 17740 | |
| 17741 | // --------(-----------------------+-----------------------)-------- (A) |
| 17742 | // m- v m+ |
| 17743 | // |
| 17744 | // --------------------(-----------+-----------------------)-------- (B) |
| 17745 | // m- v m+ |
| 17746 | // |
| 17747 | // First scale v (and m- and m+) such that the exponent is in the range |
| 17748 | // [alpha, gamma]. |
| 17749 | |
| 17750 | const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e); |
| 17751 | |
| 17752 | const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k |
| 17753 | |
| 17754 | // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma] |
| 17755 | const diyfp w = diyfp::mul(v, c_minus_k); |
| 17756 | const diyfp w_minus = diyfp::mul(m_minus, c_minus_k); |
| 17757 | const diyfp w_plus = diyfp::mul(m_plus, c_minus_k); |
| 17758 | |
| 17759 | // ----(---+---)---------------(---+---)---------------(---+---)---- |
| 17760 | // w- w w+ |
| 17761 | // = c*m- = c*v = c*m+ |
| 17762 | // |
| 17763 | // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and |
| 17764 | // w+ are now off by a small amount. |
| 17765 | // In fact: |
| 17766 | // |
| 17767 | // w - v * 10^k < 1 ulp |
| 17768 | // |
| 17769 | // To account for this inaccuracy, add resp. subtract 1 ulp. |
| 17770 | // |
| 17771 | // --------+---[---------------(---+---)---------------]---+-------- |
| 17772 | // w- M- w M+ w+ |
| 17773 | // |
| 17774 | // Now any number in [M-, M+] (bounds included) will round to w when input, |
| 17775 | // regardless of how the input rounding algorithm breaks ties. |
| 17776 | // |
| 17777 | // And digit_gen generates the shortest possible such number in [M-, M+]. |
| 17778 | // Note that this does not mean that Grisu2 always generates the shortest |
| 17779 | // possible number in the interval (m-, m+). |
| 17780 | const diyfp M_minus(w_minus.f + 1, w_minus.e); |
| 17781 | const diyfp M_plus (w_plus.f - 1, w_plus.e ); |
| 17782 | |
| 17783 | decimal_exponent = -cached.k; // = -(-k) = k |
| 17784 | |
| 17785 | grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus); |
| 17786 | } |
| 17787 | |
| 17788 | /*! |
| 17789 | v = buf * 10^decimal_exponent |
no test coverage detected