f**********e 发帖数: 1 | | r****t 发帖数: 10904 | 2 小数也要二进制?
【在 f**********e 的大作中提到】 : 整数不用转换直接就可以输出,小树呢?
| p**s 发帖数: 2707 | 3 整数怎么做?
【在 f**********e 的大作中提到】 : 整数不用转换直接就可以输出,小树呢?
| k**f 发帖数: 372 | 4 Integers and floating point numbers are all represented inside the memory in binary form. If you write to a text file, some kind of conversion is always needed, even for integers.
You can write the internal binary representation directly to a file with C function fwrite() and read them back with function fread().
If the binary file are used in different hardware platforms, you need also to understand the order of bytes for a multi-byte value, also know as the "endian" issue, too. | g*********8 发帖数: 53 | 5 举个例子
binary: 0.11111...
dec: 2^(-1)+2^(-2)+2^(-3)+2^(-4)+2^(-5)...
不是所有的小数都有与之对应的二进制的,有的是约等于,有精度损失的 | k**f 发帖数: 372 | 6
You are right. The textural decimal representation of the fractional part of
some floating point, e.g. 0.1, may not be precisely represented in binary.
But LP was asking output floating point in the memory to a text file.
Theoretically, if you allow enough digits after decimal points, the floating
point number can be precisely represented in decimal. But the run-time
library usually have a limit for the number of effective digits (17 in MS VC
++ 2008) in %f format using printf(). You can defini
【在 g*********8 的大作中提到】 : 举个例子 : binary: 0.11111... : dec: 2^(-1)+2^(-2)+2^(-3)+2^(-4)+2^(-5)... : 不是所有的小数都有与之对应的二进制的,有的是约等于,有精度损失的
|
|