jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
5.18k stars 1.87k forks source link

fix: prettier floating point numbers #1293

Closed SGSSGene closed 2 weeks ago

SGSSGene commented 4 months ago

fixes #1289.

This PR adds a new function fp_to_string. fp_to_string internally uses dragonbox to compute the required precision to print floating point numbers. This avoids uglification of floating point numbers that happen by default via std::stringstream.

Numbers like 34.34 will be converted to '34.340000000000003' as strings. With this version they will be converted to the string '34.34'.

jbeder commented 4 months ago

@jk-jeon, given your expertise, would you be willing to review this?

SGSSGene commented 4 months ago

@Anton3: Could you check that this PR is actually solving your issue?

@jk-jeon: Can you check if the license in changes in a include/yaml-cpp/contrib/dragonbox.h to your liking? (I have tripled licensed them).

I belive this PR is ready to go.

jk-jeon commented 4 months ago

@jk-jeon: Can you check if the license in changes in a include/yaml-cpp/contrib/dragonbox.h to your liking? (I have tripled licensed them).

Looks good. Thanks!

davidzchen commented 4 months ago

It looks like the indentation style of the new code is inconsistent with that of the rest of the codebase (4 vs 2 spaces). @SGSSGene can you make the style consistent?

SGSSGene commented 4 months ago

It looks like the indentation style of the new code is inconsistent with that of the rest of the codebase (4 vs 2 spaces). @SGSSGene can you make the style consistent?

Yes, very good points.

Should I also make a fp_to_string.cpp file? Most things don't really have to be in the header file.

SGSSGene commented 3 months ago

It looks like the indentation style of the new code is inconsistent with that of the rest of the codebase (4 vs 2 spaces). @SGSSGene can you make the style consistent?

Yes, very good points.

Should I also make a fp_to_string.cpp file? Most things don't really have to be in the header file.

I split fp_to_string.h into src/fptostring.cpp and include/yaml-cpp/fptostring.h. This also allowed to move dragonbox.h into src/contrib folder.

SGSSGene commented 3 months ago

@jbeder an you help me out with the current windows-shared-build errors? (I currently don't have a windows machine at hand). I thought adding YAML_CPP_API in fptostring.h would be enough. But it seems like the symbols are not being exported into the DLL?

jbeder commented 3 months ago

Honestly I don't know, I don't have a Windows machine any more either :)

Maybe ask on Stack Overflow? I'd be shooting in the dark also.

SGSSGene commented 3 months ago

Honestly I don't know, I don't have a Windows machine any more either :)

Maybe ask on Stack Overflow? I'd be shooting in the dark also.

Found the issue: the fptostring.cpp didn't include fptostring.h, which than didnt see the "YAML_CPP_API" declaration, which then didn't export the symbols into the dll. Fixed now!

Ready to go from my side :-)

Anton3 commented 3 months ago

@jbeder Could you give another round of review, please? Can't wait to stop uglifying my configs :)

SGSSGene commented 2 months ago

Hi @jbeder , any chance you can take another look?

SGSSGene commented 2 months ago

Hey @jbeder, a polite reminder that this is still open :-)

davidzchen commented 1 month ago

Sorry for the delay. Changes LGTM on my side.

@jbeder Can you please take a look at this?

davidzchen commented 1 month ago

@jbeder Are there any other blockers before this can be merged?

SGSSGene commented 1 month ago

@jbeder, any thing we can do, to motivate you looking at this PR?

jk-jeon commented 1 month ago

Also, has the owner of dragonbox approved this PR?

I approved the license notification. If you are talking about the implementation itself, I still think precision specification is fundamentally incompatible with the shortest roundtripping representation and don't recommend mixing them together, but if 100% correct rounding/roundtrip is not your goal than it seems okay.

SGSSGene commented 2 weeks ago

@jbeder Hey jbeder, friendly reminder, I made the adjustments you suggested to make.