Is scientific notation safe for integer constants in C?

前端 未结 6 730
心在旅途
心在旅途 2021-02-03 18:04

For a while, I\'ve been representing large powers of 10 in constants using scientific notation, just so I don\'t have to count the zeros. e.g.

#define DELAY_USE         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-03 18:43

    In theory, no. Neither language specifies how floating point values are represented, or which values can be represented exactly. (UPDATE: apparently, C11 does recommend a representation. C++, and older C dialects, don't).

    In practice, yes, for quite a large range of values. Any implementation you're remotely likely to encounter will use a 64-bit IEEE representation for double. This can represent any integer value up to 253 (approximately 9x1015) exactly. It can certainly represent anything representable by a 32-bit integer type.

提交回复
热议问题