-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[Question] Floating point resolution config during dump() ? #768
Comments
The precision is set in https://github.com/nlohmann/json/blob/develop/src/json.hpp#L6701 and uses |
Thanks a lot for the quick reply and proposed fix! I understand the strong requirements behind round trip conversions especially for floating point values. As a follow up, do you believe it would bring some added value to the API to have a specialized dump() interface for the case I have outlined above ? |
With the current interface of |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi there! std::numeric_limits<FloatType>::digits10 and std::numeric_limits<number_float_t>::max_digits10 but that didn't change the output of |
This is not possible any longer as the library is using the Grisu2 algorithm rather than |
That's sad for me. |
|
Well as stated above: The full precision also reflects very small changes in floats, that in my usecase are irrelevant. In my case they come from different optimization paths of different compilers or even the same compiler on slightly different source code (that mathematically would lead to the same results but doesn't on floating point numbers). Tuning down the precision means that I could actually diff the results of two versions and would get no changes, if my code was correct. Now I have to read the result jsons in a compare-function that declares numbers that differ only up to 10^(-12) to be equivalent or have to make sure that my program only saves rounded numbers into the json. Both possible but much less convenient for me compared to someone having done the hardwork here, that's why I asked.
If its possible to group all parameters into a common class, they could be in a container and have an id (an int?), such that older versions of dump can deny using parameters that are too new for them. |
Since I am not really finding a solution that I am happy with, I want to give my reason why I want to change the resolution of the output string. My Application has a websocket connection to a website used as a GUI. I want to display the values of the application with 3 decimal places to the user. I don't see the point of sending a string with the full precision and have the browser do the shorting. I don't like to make a second copy in my application with a shortend double of every value I want to display. for now I will do something like this:
|
Hello,
First of all I would like to thank for doing such a nice job in developing and maintaining a top-notch C++ JSON code base!
I am a new user of this library, so maybe my question may be well documented somewhere, but I might have overlooked the explanation (if this is the case apologies beforehand but please point it out to me).
Is there any functionality exposed to limit the floating point resolution during dump() ?
Context: I am asking this as I am not using the basic output streams or its derivatives but an MQTT publisher. So basically I just dump the JSON objects that I have to the MQTT interface as a string. As of now, the float data fields have by default 10 or so decimal places as explained in the documentation, but is there a flag / API to restrict that to a configurable number, e.g. 2 decimals only ?
Best,
Andrei
The text was updated successfully, but these errors were encountered: