-
Notifications
You must be signed in to change notification settings - Fork 93
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
Problem with getting std::numeric_limits<double>::infinity() #60
Comments
This is basically because property_tree/include/boost/property_tree/stream_translator.hpp Lines 181 to 183 in cf8189a
to translate the given string to your requested data type, which in this case is a #include <iostream>
#include <boost/property_tree/ptree.hpp>
int main()
{
boost::property_tree::ptree tree;
tree.put("number", 4.56);
auto number = tree.get_optional<int>("number").get();
std::cout << number << std::endl;
} Again, as explained above, boost is unable to convert the string "4.56" to an I am not sure whether I should do a PR for this, since I don't whether this was meant to be a design feature rather than a bug. That's why I'm pinging the maintainer (@madmongo1 Hey, Richard! Can I work on this?). Hope this helps. |
@DhruvMaroo , I think a good place to start would be to define the behaviour we would like to see and investigate whether that breaks any existing assumptions. Perhaps it's a good idea to summarise here? |
Thank you for responding @madmongo1, I suppose the expected behavior should be that stream extractor recognizes that |
For me the expected behaviour was that I could call I've not tested this but I suspect this will fail for NaN as well as +infinity and -infinity. |
We had a similar conversation in boost.json in the end it was decided that storing a nan or inf was an error since that's not representable in JSON. i.e.
is invalid json and will break any parser. Are we saying that in the case of
? This feels like an interface change to me (whether it's a welcome one or not). |
Ah, I was working with XML so I hadn't considered those values might not be representable in other formats. |
I’m not sure there are any elegant solutions when all formats dont support all data ranges :-S |
@madmongo1 I think we can set a special case for conversion of "inf" and "nan" to a |
The problem as I see it is that people use property_tree as a json parser and Dom type. (Whether rightly or wrongly, this is in the field) suddenly changing the behaviour to something nonstandard for json could break these people’s code. |
If you wanted to consider the idea further: One may change the default translator to use lexical_cast instead of stringstream, or just create a specialization of the translator that does the same. Based upon the FAQ it seems to be supported https://www.boost.org/doc/libs/1_76_0/doc/html/boost_lexical_cast/frequently_asked_questions.html Then we would update the JSON parser to catch the nan or inf. |
I don't see a fundamental problem with making the parser more accepting of "inf" or "nan". It seems to me that this is unlikely to break any code. Is there a strong use case for it? |
Example code:
Example output:
Expected output:
Changing the
std::string
in theget_value
todouble
results in:Problem also applies to
float
andlong double
types.Tested in https://hub.docker.com/r/zouzias/boost/
and https://tio.run/##hVDRSsMwFH3vV1wqSAtze89qHwY@@OLAyV5EStbezWCahDQZFPHb603aqZuCL2lz7jkn59zamJtacnUYhiuhaukbhELozlnkbZl8YztN4MJYbdC6vqI5LoTa68pw26GdvxrzH92E82/iEW0ntLocUgyhDj8RKVrhujJJEqEctFyoLIf3BKBzDWO19g6KAtJVcIXt6MogDeBqvd48Vdu7x839@iEAUYKqkUvSxxyMnSWma/hAOJYJkWIB412WKt/u0Kaz0YRuaEVdjemKRvudxJIxWpBQwvVZnkf9XlvIuHcarp/fsJ/BkUuPL8Cic06MiyJEin1g6hD58wO6Kv4VkTxtiRZxXors4ltjVrgd4wexNo72wuUp6VefPIyzfPlroZPHSXjKM8Hn734Mwyc
and a system running boost 1.60
The text was updated successfully, but these errors were encountered: