Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double precision floating point number contains null character when printed #193

Closed
cschreib opened this issue Nov 17, 2024 · 0 comments · Fixed by #194
Closed

Double precision floating point number contains null character when printed #193

cschreib opened this issue Nov 17, 2024 · 0 comments · Fixed by #194
Labels
bug:confirmed Something isn't working (confirmed)
Milestone

Comments

@cschreib
Copy link
Member

cschreib commented Nov 17, 2024

The append_constexpr() overload for floating point numbers incorrectly computes the number of digits required to print the float, resulting in some characters allocated in the temporary buffer not being written over. Since the temporary buffer is zero-initialised, these characters turn into null terminators.

When printed as a std::string_view (the default in snitch), the full number will be printed, but with a non-printable null character embedded in the middle of the string. When printed as a C string, the number is truncated before the exponent.

This only happens for a specific range of exponents: e-84 to e-99.

Note: this was spotted when running the snitch float-to-string code through the dtoa-benchmark, which includes some correctness checks. For fun, here was the result:

Benchmarking randomdigit ostringstream        ... [1054.500ns, 1156.900ns]
Benchmarking randomdigit ostrstream           ... [ 937.000ns, 1008.300ns]
Benchmarking randomdigit sprintf              ... [ 614.800ns,  686.300ns]
Benchmarking randomdigit snitch               ... [ 469.100ns,  491.500ns]
Benchmarking randomdigit doubleconv           ... [  61.400ns,  108.700ns]
Benchmarking randomdigit grisu2               ... [  61.300ns,   99.600ns]
Benchmarking randomdigit fpconv               ... [  57.300ns,   90.900ns]
Benchmarking randomdigit milo                 ... [  41.000ns,   88.900ns]
Benchmarking randomdigit emyg                 ... [  30.400ns,   69.000ns]
Benchmarking randomdigit floaxie              ... [  25.000ns,   67.200ns]
Benchmarking randomdigit null                 ... [   1.300ns,    1.400ns]

The correctness checks still spotted a few roundtrip issues, which are acceptable (only last digit is incorrect):

Error: roundtrip fail 1.0251092987768899e+304 -> '1.0251092987768898e+304' -> 1.0251092987768897e+304
Error: roundtrip fail 1.0894758796890851e+192 -> '1.0894758796890850e+192' -> 1.0894758796890849e+192
Error: roundtrip fail 1.0097235421977625e-199 -> '1.0097235421977624e-199' -> 1.0097235421977624e-199
Error: roundtrip fail -1.0896486723912265e+304 -> '-1.0896486723912264e+304' -> -1.0896486723912263e+304
Error: roundtrip fail 1.0372823149670409e-286 -> '1.0372823149670408e-286' -> 1.0372823149670407e-286
Error: roundtrip fail 1.0040738707345479e-258 -> '1.0040738707345478e-258' -> 1.0040738707345477e-258
Error: roundtrip fail 1.0612733881446836e-47 -> '1.0612733881446835e-47' -> 1.0612733881446834e-47
Error: roundtrip fail -1.0359632412158929e-199 -> '-1.0359632412158928e-199' -> -1.0359632412158927e-199
Error: roundtrip fail 1.1401842972637517e-94 -> '1.1401842972637516e-94' -> 1.1401842972637515e-94
@cschreib cschreib added the bug:confirmed Something isn't working (confirmed) label Nov 17, 2024
@cschreib cschreib added this to the v1.2.6 milestone Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:confirmed Something isn't working (confirmed)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant