Skip to content
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] Current place in code to change floating point resolution #1191

Closed
DeannaGelbart opened this issue Aug 13, 2018 · 6 comments
Closed

Comments

@DeannaGelbart
Copy link

I'd like to reduce the number of floating point digits produced by dump().

Where in json.hpp do I do that?

This old issue #768 says to replace std::numeric_limits<number_float_t>::digits10 at line 6701 but digits10 is no longer at that line. I tried replacing this symbol in dump_float() but it didn't do the trick and when I tried replacing every occurrence of this symbol in json.hpp I got assertion failures.

Thanks!

@DeannaGelbart
Copy link
Author

DeannaGelbart commented Aug 13, 2018

I think I found a workaround, if I switch from float to double and then do this based on https://stackoverflow.com/a/11209187 before calling dump()

double ToTwoDecimalPlaces(double d) {

  int i;

  if (d >= 0)

    i = static_cast<int>(d * 100 + 0.5);

  else

    i = static_cast<int>(d * 100 - 0.5);


  return (i / 100.0);

}

@RokerHRO
Copy link

doubleliterals don't have any suffix in C nor C++. Is that suffix "d" an extension of your compiler?
https://en.cppreference.com/w/cpp/language/floating_literal

@DeannaGelbart
Copy link
Author

DeannaGelbart commented Aug 14, 2018

I've removed the d suffix from the floating point literals in the code snippet since as you point out, it's meaningless.

@DeannaGelbart
Copy link
Author

Unfortunately, while this workaround has been working for me, it's not working reliably for my colleague. Presumably due to some numbers not being representable exactly enough as a double.

@DeannaGelbart
Copy link
Author

It turns out my colleague just made a typo.

Yesterday I tried about 50,000 floating point numbers with my code and they all were formatted with the correct number of decimal places

However these numbers are all in the range from -60 to 60 so I don't know for sure that my code works reliably for every number.

@DeannaGelbart
Copy link
Author

It's possible that once numbers get large enough that there is considerably less floating precision left to represent the part after the decimal point, my code starts failing. This could be tested with a loop that covers the representation range of the double type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants