![]() |
|
atof considers locale under linux? |
count
Member #5,401
January 2005
|
I recently tested my game under linux just to see that it doesn't work correctly. The problem now is that atof under linux seems to take the locale of my machine into account. I'm on a german windows xp so normally float values are written like 0,5 not like 0.5. This code works under windows: char * c = "0.5"; float f = atof(c); // f = 0.5 But on linux it behaves like this:
What did I miss?
|
Thomas Fjellstrom
Member #476
June 2000
![]() |
It seems atof and strtod are locale dependant. You'll probably want to set the locale to C in your program. setenv("LC_ALL", "C"); might be all you need. Or if you only want to change the numerics: setenv("LC_NUMERIC", "C"); editInf: libc might not notice env vars in a running program, I don't know, if it doesn't try: #include <locale.h> setlocale(LC_NUMERIC, "C");
-- |
count
Member #5,401
January 2005
|
strtod behaves exactly the same. EDIT: EDIT2:
|
Thomas Fjellstrom
Member #476
June 2000
![]() |
Christopher Bludau said: Did I understand you correctly that "C" is an own locale? Yup. Its a simplified locale that libc uses when nothing else is set. Its also the default fairly often if you don't select one on install. Quote: But why is it different for windows and linux? Probably because windows doesn't support "Locales". -- |
count
Member #5,401
January 2005
|
Thomas Fjellstrom said: Probably because windows doesn't support "Locales". Ok. Sounds reasonable. It is working now.
|
X-G
Member #856
December 2000
![]() |
Windows does support locales. We were bitten by this problem when some code that used sscanf() under MSVC stopped working mysteriously. -- |
Thomas Fjellstrom
Member #476
June 2000
![]() |
X-G said: Windows does support locales. Standard libc locale's? or its own notion? -- |
count
Member #5,401
January 2005
|
How did you solve it?
|
X-G
Member #856
December 2000
![]() |
Thomas Fjellstrom said: Standard libc locale's? or its own notion? Standard libc locales, yes. Christopher Bludau said: How did you solve it? setlocale(LC_ALL, "C"); as a hack, fixed it properly by getting rid of sscanf (which is a horrid function anyway). (EDIT, Protip: Under C++, you can imbue() an iostream with the locale you want.) -- |
count
Member #5,401
January 2005
|
X-G said: Protip: Under C++, you can imbue() an iostream with the locale you want. Nice! That's pretty useful.
|
|