由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++如何把小数转成二进制输出到文本文件?
相关主题
fread/fwrite有big/small endian问题吗?64位系统怎么调32位系统的存档 (转载)
有没有现成的模拟fread的bufferRead()?linux怎么读入一个超过有超过1B integer的binary file? (转载)
serialization 到底该怎么理解啊?来来来,我也问个题 (转载)
haskell怎么读取二进制数组?谁帮我解释一下这个代码
[合集] visual c++中读二进制图形文件的奇怪问题float vs decimal
[合集] 什么办法可以把一个文本文件map到一串数字一个dot net浮点运算的问题
今天不说微软的民工 (转载)贡献一下:本版上搜集的 Google 面试题 (转载)
ask a question about struct in C programmingpython读入文件疑问
相关话题的讨论汇总
话题: binary话题: floating话题: decimal话题: point
进入Programming版参与讨论
1 (共1页)
f**********e
发帖数: 1
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)...
: 不是所有的小数都有与之对应的二进制的,有的是约等于,有精度损失的

1 (共1页)
进入Programming版参与讨论
相关主题
python读入文件疑问[合集] visual c++中读二进制图形文件的奇怪问题
问一个C语言中整型和浮点运算的问题 (转载)[合集] 什么办法可以把一个文本文件map到一串数字
比较简单的Java数据输入验证问题今天不说微软的民工 (转载)
如何保存一个stl容器的完整内容ask a question about struct in C programming
fread/fwrite有big/small endian问题吗?64位系统怎么调32位系统的存档 (转载)
有没有现成的模拟fread的bufferRead()?linux怎么读入一个超过有超过1B integer的binary file? (转载)
serialization 到底该怎么理解啊?来来来,我也问个题 (转载)
haskell怎么读取二进制数组?谁帮我解释一下这个代码
相关话题的讨论汇总
话题: binary话题: floating话题: decimal话题: point