Re: Reading and Writing float value of infinity to file.
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:0o1Si.3$4P3.1@newsfe06.lga...
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:Gc1Si.76$dr4.63@newsfe02.lga...
"James Kanze" <james.kanze@gmail.com> wrote in message
news:1192788360.167539.75570@i13g2000prf.googlegroups.com...
On Oct 19, 8:56 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
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?
[Snip discussion about C++ not supporing infinity]
Well, this is what I think I'll put into production, unless anyone can show
me any flaws in it. It seems to work fine as a float in my rudimentary
tests.
namespace jml
{
class Float
{
public:
Float( float Value = 0.0f ): Value( Value ) {}
operator float() { return Value; }
float Value;
};
std::istream& operator>>( std::istream& is, Float& mf )
{
if ( is >> mf.Value )
{
if ( mf.Value == 1.0f && is.peek() == '#' )
{
std::string Rest;
is >> Rest;
if ( Rest == "#INF" )
mf.Value = std::numeric_limits<float>::infinity();
}
}
return is;
}
};
I'll probably make Value private just because.