Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[core] Remove trailing zeros in float→string conversion - #4965

Closed
friedbunny wants to merge 6 commits into
masterfrom
fb-pretty-float-998
Closed

friedbunny wants to merge 6 commits into
masterfrom
fb-pretty-float-998

Conversation

@friedbunny

Copy link
Copy Markdown
Contributor

With the replacement of boost::lexical_cast with std:to_string in #4916, symbol labels that used floating point numbers could end up looking like “12345.000000” when the passed in number was actually just “12345”.

The fix here is two-fold:

  • Cast sufficiently int-like floats to int.
  • Otherwise, let std::to_string convert the float to a string, then trim trailing zeros and hanging decimal points.

If this passes muster, we should consider including it in iOS v3.2.2 and the next Android release.

Fixes #998.

/cc @jfirebaugh @1ec5 @tobrun

@friedbunny friedbunny self-assigned this May 6, 2016
@friedbunny friedbunny added bug Core The cross-platform C++ core, aka mbgl labels May 6, 2016
Comment thread test/util/number_conversions.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other things to test:

  • 0 → 0
  • -0 → -0 (not sure about this one)
  • 10 → 10
  • -10 → -10

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this in d09cdb4a2dbc53ff2515e09777b17beed2c57b8f.

friedbunny added 5 commits May 7, 2016 01:50
If a float looks sufficiently int-like, cast as int and be done. Otherwise, let std::to_string() convert the float to a string, then trim trailing zeros and hanging decimal points.
This allows smaller types to avoid unnecessary formatting, besides theoretically making these casts safer.
@friedbunny
friedbunny force-pushed the fb-pretty-float-998 branch from d09cdb4 to 272f75e Compare May 7, 2016 05:52
Avoid some manual text processing in hopes of being more robust. Updated tests to reflect a default precision of six significant figures.
@1ec5

1ec5 commented May 7, 2016 •

Copy link
Copy Markdown
Contributor

@jfirebaugh, would there be any disadvantage to using std::ostringstream instead of std::to_string for all numeric types, even bool? For numeric types, it defaults to the equivalent of the %g printf specifier, which seems to be what we want.

@1ec5

1ec5 commented May 7, 2016

Copy link
Copy Markdown
Contributor

Remember to update all the relevant changelogs.

@jfirebaugh

jfirebaugh commented May 9, 2016 •

Copy link
Copy Markdown
Contributor

I think using std::ostringstream would be fine. However, what this suggests to me is that toString is probably not a useful abstraction. What I propose instead is:

  • Drop toString.
  • Use std::to_string directly where we really don't care much about presentation (e.g. debug logging).
  • Create a special purpose formatter, using std::ostringstream or whatever we land on, in SymbolBucket.

@1ec5

1ec5 commented May 9, 2016

Copy link
Copy Markdown
Contributor

If this passes muster, we should consider including it in iOS v3.2.2 and the next Android release.

Has anyone been able to reproduce this issue in iOS SDK v3.2.1? If not, there’s no need to backport the fix, it may be too late for 3.2.2 anyways.

@friedbunny

Copy link
Copy Markdown
Contributor Author

We don’t need this for v3.2.x — I think I had a bork build at one point that did suffer from this issue, but subsequent builds and the RC work as expected.

@kkaefer

kkaefer commented May 13, 2016

Copy link
Copy Markdown
Member

Can we use RapidJSON's dtoa.h implementation instead of std::stringstream? AFAIK, dtoa.h is much faster than stringstream

@1ec5

1ec5 commented May 13, 2016

Copy link
Copy Markdown
Contributor

If we're concerned about performance, perhaps sprintf() or similar would be worth trying. It's literally the %g specifier that we're after.

@kkaefer

kkaefer commented May 13, 2016

Copy link
Copy Markdown
Member

@kkaefer

kkaefer commented May 13, 2016

Copy link
Copy Markdown
Member

I'm interested in optimizing this because I've seen this be a bottleneck in the past, because we're doing a lot of number => string conversions when parsing tiles.

@friedbunny friedbunny removed their assignment May 15, 2016
@friedbunny

Copy link
Copy Markdown
Contributor Author

Unassigned myself — this can be implemented more efficiently and precisely by someone else.

@1ec5

1ec5 commented May 17, 2016

Copy link
Copy Markdown
Contributor

Superseded by #5054.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Core The cross-platform C++ core, aka mbgl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text from floating point data rounding error

4 participants