Skip to content

real/double toString issues #602

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

Closed
dawesc opened this issue Apr 14, 2017 · 2 comments
Closed

real/double toString issues #602

dawesc opened this issue Apr 14, 2017 · 2 comments

Comments

@dawesc
Copy link
Contributor

dawesc commented Apr 14, 2017

Hi all, we are having an issue where we are trying to return 24.99 to the front end of our application (this is an amount for somebody to pay as we use a json representation of the payment standards ) which stipulate amount as a double (not a great idea but nothing we can do about it). The below code

#include <json/json.h>
#include <iostream>

using namespace std;
using namespace Json;

int main(int argc, char** argv) {
        Value myNode(24.99);
        std::cout << myNode.asString() << std::endl;
}

shows (on OS X) 24.989999999999998 (on Ubuntu we get 24.999999999999999 I think) anyhow this is a problem as it means unlike issue #85 we aren't round tripping to get the same value. We've no problem maintaining a branch to support this function but I've no idea what to do about this and am looking for some advice, we have thought about maybe storing precision with value and working that way but not convinced this would actually solve the problem. Any ideas would be massively appreciated!

Thanks

Christopher

@BillyDonahue
Copy link
Contributor

"...we aren't round tripping to get the same value."
How do you know that? There are many forms of literals that specify the quantity approximated by "24.99", which is inexact. The Json::Value constructor doesn't know you used "24.99". It just receives the bit pattern of the double that 24.99 creates, so there's no way to specify a "correct" asString() specification for it.

@dawesc
Copy link
Contributor Author

dawesc commented Apr 14, 2017

Hi Billy, oooo interesting result, sorry the library we are using on our head end is different and is interpreting this differently

#include <json/json.h>
#include <iostream>

using namespace std;
using namespace Json;

int main(int argc, char** argv) {
        double myDouble=24.99;
        Value myNode(myDouble);
        Value myNodeFromStr;
        Json::Reader reader;
        reader.parse(myNode.asString(), myNodeFromStr, false);
        std::cout << myDouble << std::endl;
        std::cout << myNode.asString() << std::endl;
        std::cout << myNodeFromStr.asDouble() << std::endl;
}

yields

24.99
24.989999999999998
24.99

so indeed using the same library all is well and in fact so it seems it works well from google chrome! ```
var x = 24.989999999999998; console.info("My test " + x.toString());

My test 24.99

I'll take this back to our terminal team! Thanks for making me extend the test!

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

No branches or pull requests

3 participants