Are IEEE float and double guaranteed to be the same size on any OS?

前端 未结 4 1071
北恋
北恋 2020-12-11 11:00

I\'m working on a OS portable database system. I want our database files to be OS portable so that customers can move their database files to other kinds of OS\'s at their d

4条回答
  •  春和景丽
    2020-12-11 11:18

    std::numeric_limits::is_iec559

    Determines if a given type follows IEC 559, which is another name for IEEE 754.

    This serves as further evidence that IEEE is optional, and offers a way for you to check if it is used or not.

    C++11 N3337 standard draft 18.3.2.4 numeric_limits members:

    static constexpr bool is_iec559;

    56 True if and only if the type adheres to IEC 559 standard. (217)

    57 Meaningful for all floating point types.

    (217) International Electrotechnical Commission standard 559 is the same as IEEE 754.

    Sample code:

    #include 
    #include 
    
    int main() {
        std::cout << std::numeric_limits::is_iec559 << std::endl;
        std::cout << std::numeric_limits::is_iec559 << std::endl;
        std::cout << std::numeric_limits::is_iec559 << std::endl;
    }
    

    Outputs:

    1
    1
    1
    

    on Ubuntu 16.04 x86-64.

    __STDC_IEC_559__ is an analogous macro for C: https://stackoverflow.com/a/31967139/895245

    Rationale

    This is an interesting article that describes the rationale behind not fixing sizes, and hot to get around it: http://yosefk.com/blog/consistency-how-to-defeat-the-purpose-of-ieee-floating-point.html

提交回复
热议问题