! @brief appends a decimal representation of e to buf @return a pointer to the element following the exponent. @pre -1000 < e < 1000 */
| 11822 | @pre -1000 < e < 1000 |
| 11823 | */ |
| 11824 | inline char* append_exponent(char* buf, int e) |
| 11825 | { |
| 11826 | assert(e > -1000); |
| 11827 | assert(e < 1000); |
| 11828 | |
| 11829 | if (e < 0) |
| 11830 | { |
| 11831 | e = -e; |
| 11832 | *buf++ = '-'; |
| 11833 | } |
| 11834 | else |
| 11835 | { |
| 11836 | *buf++ = '+'; |
| 11837 | } |
| 11838 | |
| 11839 | auto k = static_cast<std::uint32_t>(e); |
| 11840 | if (k < 10) |
| 11841 | { |
| 11842 | // Always print at least two digits in the exponent. |
| 11843 | // This is for compatibility with printf("%g"). |
| 11844 | *buf++ = '0'; |
| 11845 | *buf++ = static_cast<char>('0' + k); |
| 11846 | } |
| 11847 | else if (k < 100) |
| 11848 | { |
| 11849 | *buf++ = static_cast<char>('0' + k / 10); |
| 11850 | k %= 10; |
| 11851 | *buf++ = static_cast<char>('0' + k); |
| 11852 | } |
| 11853 | else |
| 11854 | { |
| 11855 | *buf++ = static_cast<char>('0' + k / 100); |
| 11856 | k %= 100; |
| 11857 | *buf++ = static_cast<char>('0' + k / 10); |
| 11858 | k %= 10; |
| 11859 | *buf++ = static_cast<char>('0' + k); |
| 11860 | } |
| 11861 | |
| 11862 | return buf; |
| 11863 | } |
| 11864 | |
| 11865 | /*! |
| 11866 | @brief prettify v = buf * 10^decimal_exponent |