看板Programming
标 题Re: [问题] 请问是否有浮点数转二进制的函式(C)
发信站椰林风情 (Thu Nov 11 08:48:13 2010)
转信站ptt!news.ntu!Palmarama
==> [email protected] (Avril Lavigne) 提到:
> 想请问是否有函式可以浮点数转成32bit的
> thx
Visual Studio 2005 以上的版本的指令,
仅供参考
typedef __int64 Int64;
//! Check if a number is infinite.
inline bool isInfinite(double A)
{
// Representation of infinity for double precision number.
static const Int64 _kInfAsInt = 0x7FF0000000000000;
// An infinity has an exponent of 1023 (shift left 52 positions) and
// a zero mantissa. There are two infinities - positive and negative.
if ((*(Int64*)&A & 0x7FFFFFFFFFFFFFFF) == _kInfAsInt)
{
return true;
}
else
{
return false;
}
}
//! Check if a number is Not-a-Number.
inline bool isNan(double A)
{ // A NAN has an exponent of 1023 (shifted left 52 positions) and
// a non-zero mantissa. There are two Nan's - positive and negative.
Int64 _exp = *(Int64*)&A & 0x7FF0000000000000;
Int64 _mantissa = *(Int64*)&A & 0x7FFFFFFFFFFFF;
if(_exp == 0x7FF0000000000000 && _mantissa == 0)
{
return true;
}
else
{
return false;
}
}
--
只有充满感情的歌声能阻止世界末日发生,
音乐才是真正的第五元素.
--
☆ [Origin:椰林风情] [From: 58-115-151-138.cable.dynami] [Login: **] [Post: **]
1F:→ MOONRAKER:(1)用union(2)用一个int *在函数里面接 59.120.168.228 11/11 14:35
2F:→ MOONRAKER:就得了 59.120.168.228 11/11 14:35