microsoft / microsoft/STL

<charconv>: Can general shortest always use Ryu?

Open
#327 0 comments 1 reaction 1 assignee View on GitHub

@StephanTLavavej is already working on this.

Since Nov 26, 2019.

performance
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

When to_chars() is formatting in general shortest mode, it runs Ryu and then switches between fixed and scientific according to the following criterion:

https://github.com/microsoft/STL/blob/8f4c8163775d665d80642044ce27c4bc696127ce/stl/inc/xcharconv_ryu.h#L1333-L1344

When scientific is selected, we directly print Ryu's result. When fixed is selected, we run additional code:

https://github.com/microsoft/STL/blob/8f4c8163775d665d80642044ce27c4bc696127ce/stl/inc/xcharconv_ryu.h#L1389-L1440

When _Can_use_ryu is true, we can use Ryu's result, with a suitably-placed decimal point and zero digits. When _Can_use_ryu is false, we're dealing with a large integer where Ryu rounds away some digits but we need to print all of them exactly, so we activate a fallback algorithm (long division for float, Ryu Printf for double).

When fixed shortest is originally requested, the fallback algorithm is absolutely necessary for some inputs. (The _Max_shifted_mantissa logic is an attempted performance optimization - it precisely detects cases where we can avoid the fallback algorithm. It would be equally correct, but perhaps slower or faster, to activate the fallback algorithm whenever if (_Ryu_exponent > 0) { // case "172900" is true.)

However, when general shortest is originally requested, that's different from unconditionally requesting fixed. I believe that the criterion _Scientific_exponent < 6 implies that we'll never need the fallback algorithm. We need to verify this and adjust the code accordingly.

(Note: plain shortest needs the fallback algorithm because its criterion inspects the Ryu exponent instead of the scientific exponent. For example, the exactly-representable integer 12345678901234567168 is 1.2345678901234567e+19 in scientific shortest. Fixed notation is fewer characters, so plain shortest must use the fallback algorithm for that value.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.