Use stringstream instead of stod to work around locale issues.#42
Use stringstream instead of stod to work around locale issues.#42clalancette merged 5 commits intomasterfrom
Conversation
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
If I understood correctly the C++ documentation, the should avoid this problem (see gbionics/idyntree#288 (comment)). |
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Thanks for that. Done in 678ab93 |
This will be useful elsewhere (i.e. urdfdom). Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
|
@clalancette: minor, but none of the three commits seem to have been made using your gh registered email, so they are not attributed to your account. |
Ah, thanks. When we got the new "@openrobotics.org" domain, I updated my .gitconfig, but I had forgotten to verify with GitHub. Done now. |
sloretz
left a comment
There was a problem hiding this comment.
Looks good with a few includes. Think it's worth adding a test to this package?
| // thrown. | ||
| static inline double strToDouble(const char *in) | ||
| { | ||
| std::stringstream ss; |
| static inline double strToDouble(const char *in) | ||
| { | ||
| std::stringstream ss; | ||
| ss.imbue(std::locale::classic()); |
| ss >> out; | ||
|
|
||
| if (ss.fail() || !ss.eof()) { | ||
| throw std::runtime_error(""); |
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
|
I've fixed up all of the includes in caf2e8c. As far as testing goes, the only thing is that there is no test infrastructure in here. I could see about adding it (gtest or similar), but then this will grow into a much larger PR. I'm inclined to kick that to another PR, but I'll let you make the final decision. |
I am totally not a fan of the situation, but historically tests of |
|
@traversaro Thanks a lot for the pointer. I'll go ahead and add some tests over there. |
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
|
@scpeters Gentle ping; this PR (and the related ros/urdfdom#109) are ready for review and merging, and I'd like to get them in before Melodic. Thanks! |
| // for parsing, as that matches up with what the XSD for double specifies. | ||
| // On success, the double is returned; on failure, a std::runtime_error is | ||
| // thrown. | ||
| static inline double strToDouble(const char *in) |
There was a problem hiding this comment.
Why use const char * instead of const std::string &? Just curious, not asking you to change it, just hoping to learn something.
There was a problem hiding this comment.
There is a good (but sneaky) reason for it. I'm hoping that once we get this basic support in, we can use this more generally throughout the urdfdom libraries to avoid this problem. As one example, https://github.com/ros/urdfdom/blob/master/urdf_parser/src/link.cpp#L120 probably suffers from a similar problem (as do several other std::stod conversions in there). Several of those get a const char * from tinyxml, so I figured this interface was more generic rather than having to wrap those inside strings.
There was a problem hiding this comment.
Oh, right, I forgot I also did another PR to actually fix all of that as well: ros/urdfdom#105
I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary
I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary
I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary (cherry picked from commit 611f948)
I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary (cherry picked from commit 611f948)
I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary (cherry picked from commit 611f948)
… (#3072) I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary (cherry picked from commit 611f948) Co-authored-by: Matthew Elwin <10161574+m-elwin@users.noreply.github.com>
… (#3073) I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary (cherry picked from commit 611f948) Co-authored-by: Matthew Elwin <10161574+m-elwin@users.noreply.github.com>
… (#3074) I believe the bug with respect to locale was fixed a while back ros/urdfdom_headers#42 so setting LC_NUMERIC is no longer necessary (cherry picked from commit 611f948) Co-authored-by: Matthew Elwin <10161574+m-elwin@users.noreply.github.com>
The URDF schema (https://github.com/ros/urdfdom/blob/master/xsd/urdf.xsd#L47) defines doubles in URDF as xs:double. Looking at the XSD document at https://www.w3.org/TR/xmlschema-2/#double, it says that the mantissa for a double is represented as a "decimal" number. And looking at the XSD document at https://www.w3.org/TR/xmlschema-2/#decimal, a decimal number is a "finite-length sequence of decimal digits (#x30-#x39) separated by a period as a decimal indicator". Thus, the locale should have no effect on how the document is parsed, and using std::stod is incorrect. This patch switches the Vector3 class to using the stream operator, which seems to ignore locale.
fixes #41
Signed-off-by: Chris Lalancette clalancette@openrobotics.org