测试浮点值是否相等:是否有“精度”的标准名称?不变? [英] Testing for floating-point value equality: Is there a standard name for the "precision" constant?

查看:177
本文介绍了测试浮点值是否相等:是否有“精度”的标准名称?不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了给出了这个不错的答案关于如何比较浮点值是否相等。建议使用下面的代码(稍微修改一下),而不是直接比较0:

  const double epsilon = 1e-5 ; 

double d = ...;如果(Math.Abs​​(d)ε
{
// d被认为等于0.
}

我的问题是关于变量名称 epsilon 1)< /强> epsilon是指定浮点数精度的普遍认可的名称吗?(是哪一个被认为是不同数字的两个值之间的最小区分差异)? >

由于上述代码简单易懂,因此看起来可能不相关。但是我常常偶然发现这个命名问题,并希望一劳永逸地知道如何命名这个精度常数,并且使得阅读我的代码的其他人知道它的意图。

1)我知道epsilon(简称 )通常用于指定偏差或错误,例如在统计学(回归分析)。

解决方案

如果你是一个数学家,epsilon是一个好名字 - 无足轻重SmallMalue是如果你是一个啰嗦的普通人,那就更好了。 :)

I just read this nice answer given on how to compare floating-point values for equality. The following (slightly modified by me) is suggested instead of straight-forward comparison to 0:

const double epsilon = 1e-5;

double d = ...;
if (Math.Abs(d) < epsilon)
{
    // d is considered equal to 0.
}

My question is about the name of the variable epsilon1). Is "epsilon" the generally agreed-upon name for specifying the precision of floating-point numbers? (…which is the smallest discriminating difference between any two values which are considered different numbers)?

Since the above code is always easy to understand due to its simplicity, this may seem irrelevant. But I've often stumbled about this naming issue and would like to know, once and for all time, how to name that precision constant, favourably in a way that will let others who read my code know what's intended.

1) I'm aware that epsilon (ε for short, or e) is often used to designate deviations or errors, e.g. in statistics (regression analysis).

解决方案

epsilon is a fine name if you're a mathematician - insignificantlySmallValue is better if you're a long-winded normal person. :)

这篇关于测试浮点值是否相等:是否有“精度”的标准名称?不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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