Avoiding my float to get the value 1.#INF0...
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
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; } }
#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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement