将浮点数写入磁盘 [英] Writing floating point number to disk

查看:67
本文介绍了将浮点数写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里,


我不懂ios_base :: precision的语法。假设我

有一个浮点:


const float f = 0.313244462;


我该怎么写是一个流?


const float f = 0.313244462;

std :: cout<< f<< std :: endl;


这会在

小数点之后将我的数字截断为任意位数。所有我想做的就是写出所有有意义的数字,所以

当从磁盘重读这个数字时,我可以找到*确切*

相同的浮点数(我是使用''float''类型而不是''double''

类型)。


谢谢

-Mathieu

hi there,

I do not understand the syntax for ios_base::precision. Let say I
have a floating point:

const float f = 0.313244462;

How do I write is to a stream ?

const float f = 0.313244462;
std::cout << f << std::endl;

This truncate my number to an arbitrary number of digit after the
decimal point. all I am trying to do is write all meaningfull digit so
that when rereading this number from disk, I can find *exactly* the
same floating point number (I am using ''float'' type and not ''double''
type).

Thanks
-Mathieu

推荐答案



const float f = 0.313244462;

std :: cout<< f<< std :: endl;

输出为0.313244

const float f = 0.313244462;

cout.precision(10);

std :: cout<< f<< std :: endl;

输出为0.313244462

9月17日上午8:27,mathieu< mathieu.malate ... @ gmail.comwrote:

const float f = 0.313244462;
std::cout << f << std::endl;
The output is 0.313244

const float f = 0.313244462;
cout.precision(10);
std::cout << f << std::endl;
The out put is 0.313244462
On Sep 17, 8:27 am, mathieu <mathieu.malate...@gmail.comwrote:

那里,


我不懂ios_base :: precision的语法。假设我

有一个浮点:


const float f = 0.313244462;


我该怎么写是一个流?


const float f = 0.313244462;

std :: cout<< f<< std :: endl;


这会在

小数点之后将我的数字截断为任意位数。所有我想做的就是写出所有有意义的数字,所以

当从磁盘重读这个数字时,我可以找到*确切*

相同的浮点数(我是使用''float''类型而不是''double''

类型)。


谢谢

-Mathieu
hi there,

I do not understand the syntax for ios_base::precision. Let say I
have a floating point:

const float f = 0.313244462;

How do I write is to a stream ?

const float f = 0.313244462;
std::cout << f << std::endl;

This truncate my number to an arbitrary number of digit after the
decimal point. all I am trying to do is write all meaningfull digit so
that when rereading this number from disk, I can find *exactly* the
same floating point number (I am using ''float'' type and not ''double''
type).

Thanks
-Mathieu


[以下发布的帖子]


我不想指定硬编码的10值我的程序,这个

应该从浮动的大小和重要的

数字推断出来。


9月17日,3:17 pm,moongeegee< moongee ... @ gmail.comwrote:
[following initiated top post]

I do not want to specify an hardcoded ''10'' value in my program, this
should be deduced from the size of float and the number of significant
digit.

On Sep 17, 3:17 pm, moongeegee <moongee...@gmail.comwrote:

const float f = 0.313244462;

std: :cout<< f<< std :: endl;

输出为0.313244

const float f = 0.313244462;

cout.precision(10);

std :: cout<< f<< std :: endl;

输出为0.313244462


9月17日上午8:27,mathieu< mathieu.malate ... @ gmail .comwrote:
const float f = 0.313244462;
std::cout << f << std::endl;
The output is 0.313244

const float f = 0.313244462;
cout.precision(10);
std::cout << f << std::endl;
The out put is 0.313244462

On Sep 17, 8:27 am, mathieu <mathieu.malate...@gmail.comwrote:

hi那里,
hi there,


我不明白ios_base的语法::精确。假设我

有浮点数:
I do not understand the syntax for ios_base::precision. Let say I
have a floating point:


const float f = 0.313244462;
const float f = 0.313244462;


如何写入流?
How do I write is to a stream ?


const float f = 0.313244462;

std :: cout<< f<<的std :: ENDL;
const float f = 0.313244462;
std::cout << f << std::endl;


这会将我的数字截断为

小数点后的任意位数。所有我想做的就是写出所有有意义的数字,所以

当从磁盘重读这个数字时,我可以找到*确切*

相同的浮点数(我是使用''float''类型而不是''double''

类型)。
This truncate my number to an arbitrary number of digit after the
decimal point. all I am trying to do is write all meaningfull digit so
that when rereading this number from disk, I can find *exactly* the
same floating point number (I am using ''float'' type and not ''double''
type).


谢谢

-Mathieu
Thanks
-Mathieu


mathieu写道:
mathieu wrote:

我不想在我的程序中指定硬编码的10值
I do not want to specify an hardcoded ''10'' value in my program



然后使用''100''或其他什么。 AFAIK ostream最多只输出许多小数点数。它不会输出尾随零。

Then use ''100'' or whatever. AFAIK ostream only outputs at most that
many decimals. It won''t output trailing zeros.


这篇关于将浮点数写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆