【问题标题】:double datatype casting works on windows but not on linux双数据类型转换适用于 Windows 但不适用于 linux
【发布时间】:2013-03-24 20:19:32
【问题描述】:

我在 C 中有一个包含 bigint 数据的字符串指针。

例如92233720368547758072^63

我想将其转换为 double,但如您所知,double 有 15/16 位可用于存储小数部分,其余位将被丢弃。因此,上述非常大的数字将转换为 9.22337203685476E+18922337203685476000.

这使得比较原始值和转换值不匹配。这通常发生在 Linux 平台上。问题是为什么这不会在 Windows 上发生?

它是依赖于编译器还是我不知道的东西。 ?

【问题讨论】:

    标签: casting double


    【解决方案1】:

    该值是2^63 - 1,不能用double 精确表示。可以表示的最接近的值是2^63。如果你使用例如,这就是你得到的。 sscanfatof:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        const char *str = "9223372036854775807";
        double d;
        double e;
        sscanf(str, "%lf", &d);
        e = atof(str);
        printf("%f\n", d);    // 9223372036854775808.000000
        printf("%f\n", e);    // 9223372036854775808.000000
    }
    

    http://ideone.com/zz2NIF

    【讨论】:

    • 我同意这个数字不能用 double 表示,但是在 windows 中比较它是成功的,而在 linux 中是失败的?
    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2016-03-01
    • 2021-05-02
    相关资源
    最近更新 更多