You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS 13.3.1中strtod()不遵循uselocale()设置,该Bug已在14.1修复

macOS 13.3.1中strtod()不遵循uselocale()区域设置的问题

更新说明:这是macOS的一个Bug,已在14.1版本中修复。


在macOS 13.3.1系统中,strtod()函数似乎不遵循通过uselocale()设置的区域设置,无法正确处理十进制点/逗号。这是否是macOS的Bug?是否存在解决方法?

以下测试程序可证明printf()和scanf()能遵循十进制设置,但strtod()无法做到:

#include <locale.h>
#include <xlocale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int main() {
    double x;
    const char *num = "12.3";
    char *end;

    // 首先全局设置使用十进制逗号而非小数点的区域设置
    const char *locname = setlocale(LC_ALL, "de_DE");
    struct lconv *lc = localeconv();
    if (strcmp(lc->decimal_point, ",")) {
        // 如果小数点不是逗号,说明请求的区域不可用,输出信息后退出
        fprintf(stderr, "setlocale() returned '%s', decimal point is '%s'\n",
                locname ? locname : "NULL",
                lc->decimal_point);
        abort();
    }

    // 在此区域设置下,printf()使用十进制逗号,scanf()和strtod()读取数字时也会默认使用逗号
    // 因此读取"12.3"时,两个函数都会在遇到'.'时停止解析
    printf("Using locale with decimal comma:\n");
    x = 0.0;
    printf("%g\n", 12.3);
    sscanf(num, "%lf", &x);
    printf("%g\n", x);
    x = strtod(num, &end);
    printf("%g\n", x);

    // 现在通过非标准的uselocale()以线程本地方式设置数字区域为使用小数点
    locale_t c_locale = newlocale(LC_NUMERIC_MASK, "C", NULL);
    assert(c_locale != NULL);
    uselocale(c_locale);

    // 此时scanf()和strtod()都应该默认使用小数点,能完整读取"12.3"
    printf("\nUsing locale with decimal point:\n");
    x = 0.0;
    printf("%g\n", 12.3);
    sscanf("12.3", "%lf", &x);
    printf("%g\n", x);
    x = strtod(num, &end);
    printf("%g\n", x);

    return 0;
}

macOS 10.14.6上的输出符合预期:

Using locale with decimal comma:
12,3
12
12

Using locale with decimal point:
12.3
12.3
12.3

macOS 13.3.1上的输出:

Using locale with decimal comma:
12,3
12
12

Using locale with decimal point:
12.3
12.3
12

注意strtod()未读取十进制点后的内容。


该问题因导致igraph测试套件失败而被发现。

内容的提问来源于stack exchange,提问作者Szabolcs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 18:53:10