读取和写入infinity的浮点值到文件。 [英] Reading and Writing float value of infinity to file.

查看:83
本文介绍了读取和写入infinity的浮点值到文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序的输出是:

1.#INF

1


但是:

1.#INF

1.#INF


是预期和期望的。如何从流中读取无穷大的值?


#include< iostream>

#include< fstream>

#include< limits>


int main(无效)

{

浮动无限;

Infinity = std :: numeric_limits< float> :: infinity();

std :: cout<<无穷大<< " \ n";


std :: ofstream outf(" test.txt");

if(outf.is_open())

outf<< Infinity;


outf.close();


float值= 0.0f;

std :: ifstream inf(" test.txt");

if(inf.is_open())

inf>值;


inf.close();


std :: cout<<值<< " \ n";


返回0;

}

解决方案

< blockquote> 10月19日上午8:56,Jim Langston < tazmas ... @ rocketmail.comwrote:


以下程序的输出是:

1.#INF

1


但是:

1.#INF

1。 #INF


是预期和期望的。如何从流中读取无限值




根据当前的C ++标准,没有无穷大,所以

你是在实现定义的领域,如果不是undefined

行为。然而,据推测,标准的下一个版本

将使用C99作为参考,而不是C90;在C99,

无穷大需要输出为inf或者无穷大,

并且两者都应该在读取时转换为无穷大。


当前标准不允许这种行为,也不是

执行当前草案,因为它们不允许提取

inf读数时。因此,虽然实现I

通常使用所有输出inf,但是当

尝试将其读回到double时,所有报告都会报告读取错误。我手边知道的唯一解决方案是读取字符串,手动比较

inf和nan,并使用istringstream转换双重如果thos

测试失败。


-

James Kanze(GABI软件)电子邮件:ja ********* @gmail .com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''colele ,法国,+ 33(0)1 30 23 00 34


On 2007-10-19 02:56:07 -0400,Jim Langston ; < ta ******* @ rocketmail.comsaid:


>

std :: ofstream outf(" test .txt");

if(outf.is_open())

outf<< Infinity;


outf.close();



测试结束时没有换行符。 txt,所以从技术上讲,它不是一个有效的文本流。没有保证,但添加换行可能会有所帮助。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展的作者:教程和参考

www.petebecker.com/tr1book


" James Kanze" < ja ********* @ gmail.comwrote in message

news:11 ********************* @ i13g2000prf.googlegro ups.com ...

10月19日上午8:56,Jim Langston < tazmas ... @ rocketmail.comwrote:


以下程序的输出是:

1.#INF

1


但是:

1.#INF

1。 #INF


是预期和期望的。如何从流中读取无限值




根据当前的C ++标准,没有无穷大,所以

你是在实现定义的领域,如果不是undefined

行为。然而,据推测,标准的下一个版本

将使用C99作为参考,而不是C90;在C99,

无穷大需要输出为inf或者无穷大,

并且两者都应该在读取时转换为无穷大。


当前标准不允许这种行为,也不是

执行当前草案,因为它们不允许提取

inf读数时。因此,虽然实现I

通常使用所有输出inf,但是当

尝试将其读回到double时,所有报告都会报告读取错误。我手边知道的唯一解决方案是读取字符串,手动比较

inf和nan,并使用istringstream转换双重如果thos

测试失败。


---------------


嗯...我实际上是在寻找一个9'的规则号来表示没有价值。

但似乎无穷无法为我做这件事,特别是因为我需要

连续读取这些值中的3个。


我想我可以研究ifstream实现来读取1.#INF as

infinity,创建我自己的类并覆盖运算符<<。在这个特定的实现中读取数字可能就足够了,如果它是1,则查看

下一个值,如果它是#然后假设它将是#INF,读取字符串,

比较#INF,如果是,则加载无穷大。然后覆盖操作员浮动。


让我看看我能想出什么。


The output of the following program is:
1.#INF
1

But:
1.#INF
1.#INF

was expected and desired. How can I read a value of infinity from a stream?

#include <iostream>
#include <fstream>
#include <limits>

int main(void)
{
float Infinity;
Infinity = std::numeric_limits<float>::infinity();
std::cout << Infinity << "\n";

std::ofstream outf("test.txt");
if ( outf.is_open() )
outf << Infinity;

outf.close();

float Value = 0.0f;
std::ifstream inf("test.txt");
if ( inf.is_open() )
inf >Value;

inf.close();

std::cout << Value << "\n";

return 0;
}

解决方案

On Oct 19, 8:56 am, "Jim Langston" <tazmas...@rocketmail.comwrote:

The output of the following program is:
1.#INF
1

But:
1.#INF
1.#INF

was expected and desired. How can I read a value of infinity
from a stream?

According to the current C++ standard, there is no infinity, so
you''re in the realm of implementation defined, if not undefined
behavior. Presumably, however, the next version of the standard
will use C99 as the reference, rather than C90; in C99,
infinity is required to be output as either "inf" or "infinity",
and both should convert to infinity when read.

The current standard does not allow this behavior, however, nor
does the current draft, since they do not allow extraction of
"inf" when reading a number. So while the implementations I
usually use all output "inf", all report a read error when
attempting to read it back into a double. The only solution I
know of off hand is to read into a string, compare manually for
inf and nan, and use istringstream to convert the double if thos
tests fail.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


On 2007-10-19 02:56:07 -0400, "Jim Langston" <ta*******@rocketmail.comsaid:

>
std::ofstream outf("test.txt");
if ( outf.is_open() )
outf << Infinity;

outf.close();

There''s no newline at the end of "test.txt", so technically it''s not a
valid text stream. No guarantees, but adding a newline might help.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@i13g2000prf.googlegro ups.com...
On Oct 19, 8:56 am, "Jim Langston" <tazmas...@rocketmail.comwrote:

The output of the following program is:
1.#INF
1

But:
1.#INF
1.#INF

was expected and desired. How can I read a value of infinity
from a stream?

According to the current C++ standard, there is no infinity, so
you''re in the realm of implementation defined, if not undefined
behavior. Presumably, however, the next version of the standard
will use C99 as the reference, rather than C90; in C99,
infinity is required to be output as either "inf" or "infinity",
and both should convert to infinity when read.

The current standard does not allow this behavior, however, nor
does the current draft, since they do not allow extraction of
"inf" when reading a number. So while the implementations I
usually use all output "inf", all report a read error when
attempting to read it back into a double. The only solution I
know of off hand is to read into a string, compare manually for
inf and nan, and use istringstream to convert the double if thos
tests fail.

---------------

Hmmm.. I was actually looking for a 9''s rule number to indicate no value.
But it seems that infinity won''t do it for me then, especially since I need
to read 3 of these values in a row.

I guess can I can research an ifstream implementation to read 1.#INF as
infinity, create my own class and override operator<<. It might be enough
in this specific implementation to read the number, if it''s 1, peek at the
next value, if it''s # then presume it''s going to be #INF, read to string,
compare for #INF and if so load infinity. And then override operator float.

Let me see what I can come up with.


这篇关于读取和写入infinity的浮点值到文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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