abseil / abseil/abseil-cpp

from_chars: BigUnsigned::ReadDigits discards significant digits

Offen
#667 0 Kommentare 0 Reaktionen 1 zugewiesene Person Beansprucht von @vslashg Auf GitHub ansehen
Vorherrschende Sprache
C++
Sterne
18.1k
Forks
3.2k
Ø Merge
20 Std. 36 Min.
Gemergte PRs (30 T.)
1

Beschreibung

This is certainly an edge case, but the `ReadDigits` method may discard significant digits if the number is of the form `0.000[...]0ddd...` with lots of leading zeros in the fractional part and lots of significant digits `ddd...`.

The problem is that currently the leading 0's in the fractional part are counted as significant digits. A case where that happens is, e.g., the number 2^-1075 in fixed-point notation: This number has 323 leading zeros in the fractional part and 752 significant digits. The number is halfway between 0 and the smallest denormal 2^-1074 and should round to 0 in round-to-nearest-even mode. (The same number in scientific form is parsed correctly.)

I think a possible fix would be to add something like
```c++
int exponent_adjust = 0;
// Discard leading zeros after the decimal point if the number is of the
// form 0.000ddd.
if (begin < end && *begin == '.') {
after_decimal_point = true;
++begin;
while (begin < end && *begin == '0') {
++begin;
--exponent_adjust;
}
}
```
just after https://github.com/abseil/abseil-cpp/blob/b35973e3e35cb1eccb086d6a549c253c49579474/absl/strings/internal/charconv_bigint.cc#L199-L202

FWIW, here is 2^-1075 written out in fixed-point notation
```
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.