-
Notifications
You must be signed in to change notification settings - Fork 79
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
Use stringstream instead of stod to work around locale issues. #42
Conversation
Signed-off-by: Chris Lalancette <[email protected]>
If I understood correctly the C++ documentation, the
should avoid this problem (see robotology/idyntree#288 (comment)). |
Signed-off-by: Chris Lalancette <[email protected]>
Thanks for that. Done in 678ab93 |
This will be useful elsewhere (i.e. urdfdom). Signed-off-by: Chris Lalancette <[email protected]>
@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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <sstream>
?
static inline double strToDouble(const char *in) | ||
{ | ||
std::stringstream ss; | ||
ss.imbue(std::locale::classic()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <locale>
?
ss >> out; | ||
|
||
if (ss.fail() || !ss.eof()) { | ||
throw std::runtime_error(""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <stdexcept>
?
Signed-off-by: Chris Lalancette <[email protected]>
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 <[email protected]>
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
… (#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 <[email protected]>
… (#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 <[email protected]>
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 [email protected]