BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

IsNaN IsPosInf IsNegInf IsFinite

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language)
View previous topic :: View next topic  
Author Message
Tone
Guest





PostPosted: Wed Nov 17, 2004 6:16 pm    Post subject: IsNaN IsPosInf IsNegInf IsFinite Reply with quote



I searched around for these functions and found that I had to write my
own. They are here to save you some steps if you are also looking for
them.

If you should find anything wrong with any of these functions, do let
me know.



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

/*

Useful Links
============

IsNan
http://community.borland.com/article/0,1410,16716,00.html

INF+
http://homepages.borland.com/efg2lab/Library/Delphi/MathInfo/
http://www.efg2.com/Lab/Mathematics/NaN.htm

An explanation of the IEEE standard
http://groups.google.com/groups?hl=en&lr=&selm=3ccf93c1_1%40dnews

NaN means "Not a Number." Taking the sqrt of a
NaN gives a domain error.

GNU has a built-in function, and even handles the NaN != NaN
convention. Borland is just plain mean.

To compile under GNU:
gcc -O2 -Wall -o NaN.exe NaN.c

*/

#include <stdio.h>
#include <math.h>

#define is_nan(x) ((x) != (x)) // This doesn't work under
Borland

#define MANTISSA 0x8000000000000000

struct LongDouble {
// unsigned char mant[8]; // the mantissa is the lower 8 bytes
__int64 mant; // the mantissa is the lower 8 bytes
unsigned int exp; // the exponent is the upper 2 bytes
};

/*
This union allows us to write in a long double and read out a
struct LongDouble. Unions are really cool this way.
*/
union Real {
struct LongDouble sld;
long double ld;
};

/*
This function returns 1 if the parameter is any NaN type. A
float or double may be passed too, and the compiler will
promote them to long double so the test will still work.
*/
// Both NANs and INFs have an exponent field of all ones (15 ones).
// INFs have a mantissa field of 100000...0 (long doubles).
// NANs have any other mantissa.
int IsNaN (long double x) {
union Real real;
real.ld = x;
if ((real.sld.exp & 0x7FFF) == 0x7FFF & real.sld.mant != MANTISSA)
return 1; // ignore sign of exponent in
// high bit of exponent.
return 0;
}

// Both NANs and INFs have an exponent field of all ones (15 ones).
// INFs have a mantissa field of 100000...0 (long doubles).
// NANs have any other mantissa.
int IsPosInf (long double x) {
union Real real;
real.ld = x; // assign the value
if ( (real.sld.exp & 0xFFFF) != 0xFFFF & real.sld.mant == MANTISSA )
return 1;

return 0;
}

int IsNegInf (long double x) {
union Real real;
real.ld = x; // assign the value
if ( (real.sld.exp & 0xFFFF) == 0xFFFF & real.sld.mant == MANTISSA )
return 1;

return 0;
}

int IsInfinite (long double x) {
union Real real;
real.ld = x; // assign the value
if ( (real.sld.exp & 0x7FFF) == 0x7FFF & real.sld.mant == MANTISSA )
// disregard sign
return 1;

return 0;
}

int IsFinite (long double x) {

return 1 - IsInfinite ( x );
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.