如何检查double是否接近int? [英] How to check if a double is near an int?

查看:105
本文介绍了如何检查double是否接近int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个程序中,如果一个double在距离int的0.0001之内,那么我将它作为int处理为
。例如,10.00005将被视为int,而10.001

不是。


我正在寻找实现此检查的算法。任何人

给出了一个想法?


谢谢。

解决方案

4月23,4:56 pm,xz< zhang.xi ... @ gmail.comwrote:


在一个程序中,如果一个double在距离int的0.0001之内,然后我把它作为int来对待它。例如,10.00005将被视为int,而10.001

不是。


我正在寻找实现此检查的算法。任何人

给出了一个想法?


谢谢。



取决于你的意思将其视为


当然数学很简单,可以抓到小数部分

加倍并将其与.0001或您想要的任何可变数额进行比较。

在除以1之后得到商和余数。

我怀疑我甚至称它为算法...


4月23日下午5:05,Christopher< cp ... @ austin .rr.comwrote:


4月23日下午4:56,xz< zhang.xi ... @ gmail.comwrote:


在一个程序中,如果一个double在一个int的0.0001之内,那么我将它视为int。例如,10.00005将被视为int,而10.001

则不是。


我正在寻找一种算法来实现这种检查。任何人

给出了一个想法?


谢谢。



取决于你的意思将其视为


当然数学很简单,可以抓到小数部分

加倍并将其与.0001或您想要的任何可变数额进行比较。

在除以1之后得到商和余数。

我怀疑我甚至称它为算法...



通过将其视为我的意思是我会为双打做不同的事情

靠近int和双打不靠近int。


你的伎俩(如果你不想要的话)称之为算法)并没有真正起作用我的案例因为我希望9.99999是近似于但它的小数部分是
小数部分是0.99999。



4月23日下午5:15,xz< zhang.xi ... @ gmail.comwrote:


4月23日下午5:05,Christopher< cp ... @ austin.rr.comwrote:


4月23日下午4:56,xz< zhang.xi ... @ gmail.comwrote:


在一个程序中,如果一个double在距离int的0.0001之内,那么我将它视为int。例如,10.00005将被视为int,而10.001

则不是。


我正在寻找一种算法来实现这种检查。任何人

给出了一个想法?


谢谢。


取决于您的意思将其视为


当然数学很简单,可以抓住

double的小数部分并将其与.0001或任何你想要的可变金额。

在除以1之后得到商和余数。想想。

我怀疑我甚至称它为算法...



将其视为我的意思是我将为双打附近做不同的事情

靠近int和双精度不接近int。



嗯,这肯定会对事情有所了解。 将其视为意味着

做不同的事情....嗯

当你不想描述你想要的东西时很难帮助你

do。


你的伎俩(如果你不想称之为算法)并不真正起作用

对于我的情况,因为我想9.99999是接近int但它的小数部分是
,是0.99999。



仍然是同样的原则:


if(余额 - 容差<0.0)

{

//如果你愿意,可以把你的意思当作int并向下舍入

}

else if(remaining + tolerance 1.0)

{

//如果你愿意,你可以做任何你的意思,如果你愿意的话,你可以这样做。

}


In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.

I am looking for an algorithm to implement this checking. Anybody
gives an idea?

Thanks.

解决方案

On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:

In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.

I am looking for an algorithm to implement this checking. Anybody
gives an idea?

Thanks.

Depends what you mean by "treat it as"

Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I''d even call it an algorithm...


On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:

On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:

In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.

I am looking for an algorithm to implement this checking. Anybody
gives an idea?

Thanks.


Depends what you mean by "treat it as"

Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I''d even call it an algorithm...

By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.

Your trick (if you don''t wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.



On Apr 23, 5:15 pm, xz <zhang.xi...@gmail.comwrote:

On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:

On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:

In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.

I am looking for an algorithm to implement this checking. Anybody
gives an idea?

Thanks.

Depends what you mean by "treat it as"

Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I''d even call it an algorithm...


By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.

Well, that certainly sheds some light on things. "treat it as" means
"do different things"....hmmm
Pretty hard to help you when you won''t describe what you are trying to
do.

Your trick (if you don''t wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.

Still the same principle:

if( remainder - tolerance < 0.0 )
{
// Do whatever you mean by treat as int and round down if you wish
}
else if( remainder + tolerance 1.0 )
{
// Do whatever you mean by treat as int and round up if you wish
}


这篇关于如何检查double是否接近int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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