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

DoubleAttribute return integer on some PC #160

Open
RazrFalcon opened this issue Feb 4, 2014 · 8 comments
Open

DoubleAttribute return integer on some PC #160

RazrFalcon opened this issue Feb 4, 2014 · 8 comments

Comments

@RazrFalcon
Copy link

At the first, here is simple code example:

XMLDocument doc;
doc.Parse("<root><elem attr=\"0.6\"/></root>");
XMLElement *child = doc.FirstChildElement()->FirstChildElement();
cout << child->DoubleAttribute("attr")  << child->Attribute("attr") << endl;

On my work PC(Gentoo Linux x86_64) I get:
0.6 0.6
But on my old notebook(Ubuntu Linux x86_64) I get:
0 0.6

Actually, any float number returned as integer. And this bug reproduced not only on one PC. But I still did not see any dependency.

@uliwitness
Copy link
Contributor

What happens if you printf("%f\n", child->DoubleAttribute("attr") )? Does that give you 0 as well?
What is the locale setting for the machines where it fails? Maybe they use decimal commas, and thus parsing terminates at the period? In that case, one would probably have to fix the conversion code to use a particular locale?
If it's not the locale and the printf gives the correct result, maybe it's an issue with the C++ library? Missing an operator overload for floats?

@RazrFalcon
Copy link
Author

printf return: "0,000000"
LANG=ru_RU.UTF-8

So, it's probably locale bug.

@uliwitness
Copy link
Contributor

The comma kinda gives it away, doesn't it? Have you tried using setlocale() to set the system locale and see if that fixes it?

@RazrFalcon
Copy link
Author

setlocale(LC_ALL, "en_US.UTF-8");
fix the problem, but I think it's not a real solution, because, Qt for example, use setlocale(LC_ALL, ""), so we need to overwrite it... And I don't know is it a good idea.

@leethomason
Copy link
Owner

Closing - a strange behavior, and please feel free to continue the discussion, but it doesn't point to an issue with TinyXML-2.

@uliwitness
Copy link
Contributor

Lee, I beg to differ. It means that, just because Windows and MacOS always run C with a fixed locale setting, TinyXML shouldn't assume that it can use internationalized parsing calls for parsing floating point numbers that are always supposed to be written the English way.

Is tinyxml2 supposed to be thread-safe? If not, I suppose you could just call setlocale() before parsing a float and afterwards restore the locale. Alternately, there's a Stackoverflow thread with other fixes here: http://stackoverflow.com/questions/1994658/locale-independent-strtod-implementation

@leethomason leethomason reopened this Feb 5, 2014
@leethomason
Copy link
Owner

I see your point...re-read the thread. And re-opening.

I have always avoided the local stuff in C because it isn't reliable. (Hello Irony! Good to see you.) We could get/set/restore the locale, but that potentially introduces a threading bug - although that's a total guess. Not clear to me what the thread model is from a quick scan of the docs.

There's no real XML behavior here - it's the convenience methods that aren't working - but they should work reliably.

@lp35
Copy link

lp35 commented Jul 1, 2021

Hi,

We also experiencing issues with local switch between computer/languages. Would it be possible to fix that?

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

4 participants