jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
4.91k stars 1.77k forks source link

Floating-point numbers are uglified, a.k.a. write shortest floating-point representation with round-trip guarantee #1289

Open Anton3 opened 1 week ago

Anton3 commented 1 week ago

In the last few years, libraries like fmt have mastered printing of floating-point numbers. They use shortest representation with round-trip guarantee.

Meanwhile, yaml-cpp, after #649, started to uglify numbers in my configs (I use yaml-cpp to patch them).

For example, before:

latitude: 34.34
longitude: 56.56
altitude: 12.12
heading: 78.78

After:

latitude: 34.340000000000003
longitude: 56.560000000000002
altitude: 12.119999999999999
heading: 78.780000000000001
davidzchen commented 1 day ago

+1 I am running into this issue too. Emitting a float 678.9 results in 678.900024, and emitting a double 890.1 results in 890.10000000000002.

@jbeder Can you take a look?

jbeder commented 1 day ago

This sounds reasonable, open to PRs. But you'll have to be careful about the API here, you don't want unintended consequences.