3 ms·
Scanning a floating-point value with strtod depends on locale. If it's in some locale where the decimal point is a comma, it may stop recognizing the standard 1
by kazinator 3y ago
Scanning a floating-point value with strtod depends on locale. If it's in some locale where the decimal point is a comma, it may stop recognizing the standard 123.456+EE notation.
The fix is never to call setlocale(); calling setlocale is like asking "f___ my C program".
ISO C localization was designed back in the 1980s, when nobody had any real experience with localizing. In a greenfield C program, it's best to do it all yourself from scratch and stay away from C localization, so you can depend on strtod and isspace to do what they are supposed to.
- bitwize 3y agos/greenfield C program/greenfield program/ s/from C localization.*$/from C./
- pavlov 3y agoIt's useful to remember that the 1980s approach to localization preceded the global Internet. The way most software worked was that you bought it from a local reseller. It came localized for your country (perhaps by the reseller or importer rather than the original authors of the software), and then you'd use it to conduct your local business. Data interchange wasn't that common. Desktop printers were hugely important because a hard copy was how you'd share anything. If you needed to get the information somewhere fast, you'd then fax it. Rarely when you did need to exchange files, you'd use floppies. Maybe you'd take your WordPerfect document to a professional print shop so they would do a layout using cutting-edge desktop publishing technology. So the notion that somebody in Germany might receive American files, or vice versa, wasn't really a primary concern. It was considered far more important that the Germans, and everybody else, would be able to work with their data with the number formatting that was preferred (and sometimes legally mandated).
- orf 3y agoCool but that was over 40 years ago. Who cares, and why hasn’t it been improved since then?
- IshKebab 3y agoI guess anyone that develops in C is happy living in the 80s amongst the footguns. Anyone who isn't has moved on to other languages where it has been improved.
- pvh 3y agoTo the former, curious people with an interest in how and why the world came to be as it is. To the latter, obviously "it" has improved, but ecosystem effects make certain changes very difficult and expensive to coordinate and what we see here is the scars from that process. Everything you see in the world grew out of things that came before, and was made by fallible people working with limited time, energy, and perspective. Honestly, I'm a bit surprised someone with a three letter handle wouldn't already recognize this. Surely you have been around here for a while.
- JdeBP 3y agoIt actually has been improved. See discssion of isspace_l() elsewhere on this page.
- nwellnhof 3y agoUnfortunately, not all locale-dependent functions have an *_l version, at least on some platforms. glibc doesn't have sprintf_l, for example, unlike BSD and MSVCRT.
- JdeBP 3y agoLuckily, sprintf() is irrelevant to the headlined case, that involves isspace_l(), which is in the BSD, GNU, and even musl C libraries.
- ridiculous_fish 3y agoHow would one implement locale-aware strtod() all by yourself from scratch?
- kazinator 3y agoUse the locale-unaware strtod. If the locale's decimal separator character isn't . (period) then filter the output of that to replace the period with that character.