C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Low-level float parsing functions

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 28 Apr 2025 13:03:27 -0700
On Monday, 28 April 2025 12:41:17 Pacific Daylight Time Jan Schultke via Std-
Proposals wrote:
> Also you don't need to reallocate in any case. There is a relatively small
> upper limit to how many characters a float could need to be represented
> with no loss of information, although it's not easy to determine that upper
> bound without pessimizing.

That's for std::to_chars.

For std::from_chars, there is no upper limit on the buffer size to be due to
the problem of leading zeroes, like 0.00000000000000000000001e20[*]. To shrink
it to relevant data and fit a maximum-size buffer, you'll need to parse a great
deal of the floating point number anyway and not just searching for tokens like
sign, 0x prefix, the decimal separator and the exponent one.

[*] this example would fit a maximum-buffer for DBL_MAX of 309 characters, but I
could definitely write 0.<310 zeroes>1e310
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2025-04-28 20:03:31