Skip to content

string_buffer::write(double) truncates to 6 decimals with no way to raise it #58

Description

@RedFox20

rpp::string_buffer::write(double) keeps 6 decimals and never uses scientific notation, so a
value that needs more precision is written wrong and the caller gets no warning.

_tostring(char* buffer, double value, int maxDecimals = 6) in rpp/strview.h sets that
limit. The limit is not reachable from string_buffer, because write(double) takes no
precision parameter.

rpp::string_buffer sb;
sb.write(59.43696123456789);  // -> "59.436961"     6 decimals, the rest is gone
sb.write(1e-9);               // -> "0.000000"      the value is lost
sb.write(1e300);              // no scientific notation

This is easy to walk into. A GPS latitude written through string_buffer loses about 1 cm of
resolution per lost decimal, and the text still looks correct.

Suggested fix

Add an overload that passes the precision through, the same way _tostring already accepts
it:

string_buffer& write(double value, int maxDecimals);

A shortest round-trip form would be better still, so write(double) returns text that parses
back to the same value. std::to_chars(first, last, value) gives that.

Found while replacing a JSON serializer in a downstream project. We had to use Qt for the
double output, because string_buffer could not write the settings file correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions