Archived

This topic is now archived and is closed to further replies.

Avoiding my float to get the value 1.#INF0...

This topic is 5583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Consider the following code: float a=FLT_MAX; a=a*10.0f; In the first line I assigne the biggest possible value to a. In the second line I create an overflow and ''a'' contains now the ''value'' 1.#INF0. My questions are following: 1. Is there a mechanism wich allows in that case just to keep the biggest value instead of giving it a ''no number''-value? (of course I could always check if the calculation would cause an overflow, but that''s too troublesome and slow... I cannot check all multiplications throughout my program). 2. How can I detect that my value is 1.#INF0 ??? Thanks

Share this post


Link to post
Share on other sites
I wrote my own float to string convert a while back, and this is some code from it that detects the Inf case:


  

union Float
{
float f;
struct
{
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int sign:1;
};
};


//...

if(0xff==f.exponent)
{//Special Case

if(f.mantissa)
{//Not A Number

if(f.mantissa&0x00400000)
*s = ''Q'';//Undefined Result

else
*s= ''S'';//Invalid Operand/Operation

s++;
strcpy(s, "NaN");
return;
}
else
{//Infinity

if(f.sign)
{
*s = ''-'';
s++;
}
strcpy(s, "Inf");
return;
}
}

Share this post


Link to post
Share on other sites

      

#include <iostream>

bool checkINF( float value )
{

unsigned short int axreg;

__asm
{

fld value // load float value

fxam // check it

fstsw ax // get status word


mov axreg, ax // let us access in c++


}

if ( ( axreg & 0x500 ) != 0x500 ) // check for INF

return false;

return true;

}


int main( void )
{

float a= FLT_MAX;
a=a*10.0f;

if ( checkINF( a ) )
std::cout << "Number is infinite...." << std::endl;
else
std::cout << "Number is finite....." << std::endl;

getchar( );

return 0;

}




[edited by - Jx on August 28, 2002 10:30:42 PM]

Share this post


Link to post
Share on other sites