如何在数字上测试'浮点'或'双'零? [英] How to test a 'float' or 'double' zero numerically?

查看:73
本文介绍了如何在数字上测试'浮点'或'双'零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




假设T为''float''或''double''。


T x;


x< 10 * std :: numeric_limits< T> :: epsilon();


我可以使用上面的比较来测试''x'是否在数字上为零。但是

我想知道什么应该是一个很好的乘法常数才

epsilon?


谢谢,

Peng

Hi,

Suppose T is ''float'' or ''double''.

T x;

x < 10 * std::numeric_limits<T>::epsilon();

I can use the above comparison to test if ''x'' is numerically zero. But
I''m wondering what should be a good multiplicative constant before
epsilon?

Thanks,
Peng

推荐答案

我可以使用上面的比较来测试''x'是否在数字上为零。


不,因为x也可以是负数。
I can use the above comparison to test if ''x'' is numerically zero.

No, as x can also be negative.

但是我想知道在
$之前应该是一个好的乘法常数b $ b epsilon?
But I''m wondering what should be a good multiplicative constant before
epsilon?



Epsilon是1.0 + epsilon!= 1.0的最小值。你需要用比较的数字来缩放它。比较

零总是很难。你可能最好使用abs(x)<

your_own_epsilon。将your_own_epsilon设置为你想要的,例如

0.00000001也许。<​​br />

问候,

Anders Dalvander

Epsilon is the smallest value such such that 1.0 + epsilon != 1.0. You
need to scale it with the numbers to compare with. Comparing against
zero is always hard. You are probably best of with using abs(x) <
your_own_epsilon. Set your_own_epsilon to what ever you want, such as
0.00000001 perhaps.

Regards,
Anders Dalvander


9月13日上午11点10分,Anders Dalvander< goo ... @ dalvander.comwrote:
On Sep 13, 11:10 am, Anders Dalvander <goo...@dalvander.comwrote:

我可以使用上面的比较来测试''x'是否在数字上为零。
I can use the above comparison to test if ''x'' is numerically zero.



否,因为x也可以是负数。


No, as x can also be negative.



对,我的意思是std :: abs(x)。

Right, I meant std::abs(x).


但是我想知道在

epsilon之前应该是一个很好的乘法常数?
But I''m wondering what should be a good multiplicative constant before
epsilon?



Epsilon是1.0 + epsilon!= 1.0的最小值。你需要用比较的数字来缩放它。比较

零总是很难。你可能最好使用abs(x)<

your_own_epsilon。将your_own_epsilon设置为您想要的,例如

0.00000001。


Epsilon is the smallest value such such that 1.0 + epsilon != 1.0. You
need to scale it with the numbers to compare with. Comparing against
zero is always hard. You are probably best of with using abs(x) <
your_own_epsilon. Set your_own_epsilon to what ever you want, such as
0.00000001 perhaps.



因此,没有普遍接受这样的epsilon?


谢谢,


Therefore, there is no general accept such epsilon?

Thanks,
Peng


Peng Yu< Pe ******* @ gmail.comkirjutas:
Peng Yu <Pe*******@gmail.comkirjutas:




假设T是''浮动''或''双倍''。


T x;


x< 10 * std :: numeric_limits< T> :: epsilon();


我可以使用上面的比较来测试''x'是否在数字上为零。但是
Hi,

Suppose T is ''float'' or ''double''.

T x;

x < 10 * std::numeric_limits<T>::epsilon();

I can use the above comparison to test if ''x'' is numerically zero. But



真的吗?如果x是-10000怎么办?如果它等于std :: numeric_limits

< T> :: epsilon()怎么办?

Really? What if x is -10000? What if it is equal to std::numeric_limits
<T>::epsilon()?


我想知道是什么

epsilon之前应该是一个很好的乘法常数?
I''m wondering what should be a good multiplicative constant before
epsilon?



要按字面意思回答你的问题,那么比较为零是很容易的,只需

使用if(x == 0)。但是,如果x是某个计算的结果,这通常不会给你带来太大的帮助。使用这个表达式,你几乎可以只用

来检查x是否被赋值为零预先。


如果你想比较一些数值算法中出现的值那么

这在很大程度上取决于算法。你的实际问题是什么

你试图解决?


Paavo


To answer your question literally, then comparing to zero is easy, just
use if(x==0). However, this usually does not give you much if x is a
result of some computation, with this expression you can pretty much just
check whether x has been assigned literal zero beforehand.

If you want to compare values appearing in some numeric algorithm then
this all very much depends on the algorithm. What is your actual problem
you are trying to solve?

Paavo



这篇关于如何在数字上测试'浮点'或'双'零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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