如何找到float
和double
类型的内存格式?
我指的是符号、指数和分数的位数。
最佳答案
I mean the numbers of bits for sign, exponent, and fraction.
您可以使用 std::numeric_limits<T>::digits
获得尾数位,std::numeric_limits<T>::is_signed
获取符号位。
你并从sizeof(T)*CHAR_BIT
中扣除他们的总和猜测指数数字,但如果类型有填充,这可能不正确。这是典型的 long double
例如。
Find float type's memory format
当然,重要的不仅是位数,还有顺序。这些功能对您没有帮助。他们还假设 std::numeric_limits<T>::radix == 2
.
对于确切的格式,您必须查阅 cpu 架构手册。
I want to save floating point numbers to hard disk and wonder if there is a good way to make the file cross platform.
最典型的解决方案是在保存到磁盘时将 float 转换为文本表示形式:0x1.8p+0
.它不是最有效的,但它是可移植的(尽管您必须决定文件将使用什么字符编码,如果这不是系统 native 的,则需要进行转换)。
关于c++ - 查找float类型的内存格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583753/