为Inf或NaN检查双倍 - 如何? [英] checking double for Inf or NaN - how?

查看:79
本文介绍了为Inf或NaN检查双倍 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我发现以下代码来自
http://www.blitzbasic.com/Community/...hp?topic=55633


- -----

''检查号码是否有限。

本地u:Double = 2''也适用于花车


对于本地n = 1到12

打印isfinite(u)+" " + u

u:* u

下一页


打印

打印NaN怎么样?

打印


u = u - u

打印isfinite(u)+" " + u

函数isfinite(x:Double)''假定英特尔字节顺序


Const EXP_BITS =%01111111111100000000000000000000


本地位=(Int Ptr Varptr x)[1]'[0]对于Mac?


返回(位& EXP_BITS)<>对于

无穷大或者NaN


结束函数

------


我需要类似的东西 - 但作者写道这只是英特尔

。有人想让它成为多平台吗?这应该不难,

但我没有Mac。


上面的代码看起来并不像标准C对我来说......但是我希望

这个功能在linux,mac和windows下工作*首选*

pc'的......

我的猜测是代码应改为:


int(double testval)

{

Const EXP_BITS =%01111111111100000000000000000000

return(testval&& EXP_BITS); / *返回0或1? * /

}


但我认为EXP_BITS是完全错误定义的,所以如何让它工作

- 并且在那里更好的现有方法来检查Inf或NaN ???


这将是很好的2个功能:一个测试加/减Inf和

一个检查NaN,如果有任何区别?我真的不知道
了解NaN和Inf之间的位模式差异......

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

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

Martin J?rgensen的主页 - http://www.martinjoergensen。 dk

解决方案



" Martin J?rgensen" <未********* @ spam.jay.net>在消息中写道

news:lb ************ @ news.tdc.dk ...


<我找到了以下代码来自
http ://www.blitzbasic.com/Community/...hp?topic = 55633

------
''检查号码是否有限。

本地u:Double = 2''也适用于花车

适用于本地n = 1至12
打印isfinite(u)+" " + u
你:* u
下一页

打印
打印NaN怎么样?
打印

u = u - u
打印isfinite(u)+ " + u

函数isfinite(x:Double)''假定英特尔字节顺序

Const EXP_BITS =%01111111111100000000000000000000

本地位=(Int Ptr Varptr x)[1]'[0]适用于Mac?

返回(位& EXP_BITS)<> EXP_BITS''指数是无穷大的全部1s
或NaN

结束功能

------

我需要一些东西这样 - 但作者写道这只是英特尔。有人想让它成为多平台吗?这应该不难,
但我没有Mac。

上面的代码看起来并不像标准C。对我来说...但我希望这个功能能够在linux,mac和windows下工作*首选*在电脑上......

我的猜测是应该将代码更改为:

int(double testval)
{EXP EXP_BITS =%01111111111100000000000000000000

返回(testval& & EXP_BITS); / *返回0或1? * /
}

但我认为EXP_BITS是完全错误定义的,所以如何让它工作 -
并且有更好的现有方法来检查Inf或NaN ???

对于2个函数来说会很好:一个测试加/减Inf和一个检查NaN的函数,如果有任何区别的话?我真的没有理解NaN和Inf之间的位模式差异...




源代码片段可能与我不同,但似乎是为了成为C. joe


Martin J?rgensen写道:

缺少十个代币。
-snip-

这将是两个函数的好处:一个测试正/负Inf和
一个检查NaN,如果有''有什么不同吗?我并没有真正理解NaN和Inf之间的位模式差异......




基本上我有一个包含-1的变量。根据MS visual studio 2005,#IND00000000000 ...

...我认为这是(-inf)所以

我做了一个检查:


....

if(val< 0)

做点什么< - 从来没有到过这里......


我希望有人可以解决这个问题吗?

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

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

马丁之家J?rgensen - http://www.martinjoergensen.dk


Martin J?rgensen写道:

-snip-

理解NaN和Inf之间的位模式差异。 。




另外:你可以ge nerate -1。#IND000000 ... by:


#include math.h


....

var = sqrt(-10);

....


var = -1。#IND000000 ..,根据MS visual studio 2005 (以防万一

有人想尝试一下)。

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

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

Martin J?rgensen的主页 - http://www.martinjoergensen.dk


Hi,

I found the code below from
http://www.blitzbasic.com/Community/...hp?topic=55633

------
'' Check if number is finite.
Local u:Double = 2 '' also works for floats

For Local n = 1 To 12
Print isfinite(u) + " " + u
u :* u
Next

Print
Print "What about NaN?"
Print

u = u - u
Print isfinite(u) + " " + u
Function isfinite( x:Double ) '' assumes Intel byte order

Const EXP_BITS = %01111111111100000000000000000000

Local bits = ( Int Ptr Varptr x )[1] '' [0] for Mac?

Return ( bits & EXP_BITS ) <> EXP_BITS '' exponent is all 1s for
infinity or NaN

End Function
------

I need something like that - but the author writes that "This is intel
only. Anybody want to make it multiplatform? It shouldn''t be difficult,
but I don''t have a Mac".

The code above doesn''t look like "standard C" to me... But I would like
this function to work *preferable* under both linux, mac and windows
pc''s....

My guess is that the code should be changed to something like:

int(double testval)
{
Const EXP_BITS = %01111111111100000000000000000000

return (testval && EXP_BITS); /* returns either 0 or 1 ? */
}

But I guess EXP_BITS is completely wrong defined, so how to make it work
- and are there better existing ways to check for Inf or NaN???

It would be nice with 2 functions: One that tests for plus/minus Inf and
one that checks for NaN, if there''s any difference? I didn''t really
understood the bit-pattern-difference between NaN and Inf...
Best regards / Med venlig hilsen
Martin J?rgensen

--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk

解决方案


"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:lb************@news.tdc.dk...

Hi,

I found the code below from
http://www.blitzbasic.com/Community/...hp?topic=55633

------
'' Check if number is finite.
Local u:Double = 2 '' also works for floats

For Local n = 1 To 12
Print isfinite(u) + " " + u
u :* u
Next

Print
Print "What about NaN?"
Print

u = u - u
Print isfinite(u) + " " + u
Function isfinite( x:Double ) '' assumes Intel byte order

Const EXP_BITS = %01111111111100000000000000000000

Local bits = ( Int Ptr Varptr x )[1] '' [0] for Mac?

Return ( bits & EXP_BITS ) <> EXP_BITS '' exponent is all 1s for infinity
or NaN

End Function
------

I need something like that - but the author writes that "This is intel
only. Anybody want to make it multiplatform? It shouldn''t be difficult,
but I don''t have a Mac".

The code above doesn''t look like "standard C" to me... But I would like
this function to work *preferable* under both linux, mac and windows
pc''s....

My guess is that the code should be changed to something like:

int(double testval)
{
Const EXP_BITS = %01111111111100000000000000000000

return (testval && EXP_BITS); /* returns either 0 or 1 ? */
}

But I guess EXP_BITS is completely wrong defined, so how to make it work -
and are there better existing ways to check for Inf or NaN???

It would be nice with 2 functions: One that tests for plus/minus Inf and
one that checks for NaN, if there''s any difference? I didn''t really
understood the bit-pattern-difference between NaN and Inf...



The source snippet might look different to you than me, but it seems to be
missing about ten tokens in order to be C. joe


Martin J?rgensen wrote:
-snip-

It would be nice with 2 functions: One that tests for plus/minus Inf and
one that checks for NaN, if there''s any difference? I didn''t really
understood the bit-pattern-difference between NaN and Inf...



Basically I have a variable that contains -1.#IND00000000000...
according to MS visual studio 2005... I thought that this was (-inf) so
I made a check:

....
if( val < 0)
do something <- never got here....

I hope somebody can figure this out?
Best regards / Med venlig hilsen
Martin J?rgensen

--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk


Martin J?rgensen wrote:
-snip-

understood the bit-pattern-difference between NaN and Inf...



Addition: You can generate -1.#IND000000... by:

#include math.h

....
var = sqrt (-10);
....

var = -1.#IND000000.., according to MS visual studio 2005 (just in case
somebody wants to try it out).
Best regards / Med venlig hilsen
Martin J?rgensen

--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk


这篇关于为Inf或NaN检查双倍 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆