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

Started by
4 comments, last by Floating 21 years, 7 months ago
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
Advertisement
If overflow is really an issue, why not use doubles?

[twitter]warrenm[/twitter]

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;				}			}  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

      #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]
did any of the above help?
Thanks, that''s perfect! (haven''t implemented it yet but looks promising)

This topic is closed to new replies.

Advertisement