- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个单元测试,测试边界:
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CreateExtent_InvalidTop_ShouldThrowArgumentOutOfRangeException()
{
var invalidTop = 90.0 + Double.Epsilon;
new Extent(invalidTop, 0.0, 0.0, 0.0);
}
public static readonly double MAX_LAT = 90.0;
public Extent(double top, double right, double bottom, double left)
{
if (top > GeoConstants.MAX_LAT)
throw new ArgumentOutOfRangeException("top"); // not hit
}
我以为我可以通过向其添加最小可能的正 double 来使 90.0 超过边缘,但现在没有抛出异常,知道为什么吗?
在调试时,我看到 top 显示为 90,而它应该是 90.00000000...。
编辑:我应该更认真地考虑一下,90+Double.Epsilon
会失去分辨率。似乎最好的方法是进行一些位移。
解决方案:
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CreateExtent_InvalidTop_ShouldThrowArgumentOutOfRangeException()
{
var invalidTop = Utility.IncrementTiny(90); // 90.000000000000014
// var sameAsEpsilon = Utility.IncrementTiny(0);
new Extent(invalidTop, 0, 0, 0);
}
/// <summary>
/// Increment a double-precision number by the smallest amount possible
/// </summary>
/// <param name="number">double-precision number</param>
/// <returns>incremented number</returns>
public static double IncrementTiny(double number)
{
#region SANITY CHECKS
if (Double.IsNaN(number) || Double.IsInfinity(number))
throw new ArgumentOutOfRangeException("number");
#endregion
var bits = BitConverter.DoubleToInt64Bits(number);
// if negative then go opposite way
if (number > 0)
return BitConverter.Int64BitsToDouble(bits + 1);
else if (number < 0)
return BitConverter.Int64BitsToDouble(bits - 1);
else
return Double.Epsilon;
}
/// <summary>
/// Decrement a double-precision number by the smallest amount possible
/// </summary>
/// <param name="number">double-precision number</param>
/// <returns>decremented number</returns>
public static double DecrementTiny(double number)
{
#region SANITY CHECKS
if (Double.IsNaN(number) || Double.IsInfinity(number))
throw new ArgumentOutOfRangeException("number");
#endregion
var bits = BitConverter.DoubleToInt64Bits(number);
// if negative then go opposite way
if (number > 0)
return BitConverter.Int64BitsToDouble(bits - 1);
else if (number < 0)
return BitConverter.Int64BitsToDouble(bits + 1);
else
return 0 - Double.Epsilon;
}
这就完成了工作。
最佳答案
根据 the documentation of Double.Epsilon
:
The value of the Epsilon property reflects the smallest positive
Double
value that is significant in numeric operations or comparisons when the value of theDouble
instance is zero.
(强调我的。)
将它添加到 90.0 不会产生“90.0 之后的下一个最小值”,这只会再次产生 90.0。
关于c# - 为什么将 double.epsilon 添加到一个值会产生相同的值,完全相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506477/
据说它会选择概率为 1-epsilon 的经验平均值最高的臂,在第 no 页中为概率编写的方程中,epsilon/k 是如何添加到它的(以及用于随机概率选择的 epsilon/k) :6篇论文Algo
据我所知,epsilon 标志着探索和利用之间的权衡。一开始,你希望 epsilon 较高,这样你才能取得大的进步并学到东西。当您了解 future 的奖励时,epsilon 应该衰减,以便您可以利用
教科书教我们使用 epsilon (ε) 编写正则表达式符号,但是如何将该符号直接转换为代码而不必完全修改我的正则表达式? 例如,我将如何编写这个正则表达式来捕获以 a 开头或结尾的所有小写字符串。
我在想 ES6 中的相等是封闭的情况,就像这个基本的例子: x = 0.2; y = 0.3; z = 0.1; equal = (Math.abs(x - (y - z)) < Number.EPS
有一个最接近零的FLT_MIN常量。如何最接近某个数字值? 举个例子: float nearest_to_1000 = 1000.0f + epsilon; // epsilon must be th
这是一张图片:http://i.imgur.com/MRvz24u.gif 所以我可以说出问题是什么,我用于球坐标的 epsilon(或任何符号)重复出现。所以我计算点数的方式是: double th
我正在尝试以最小值分隔两个等值的 double 。上下文是一个事件模拟。我不希望事件同时发生,因此我将设置为发生新事件的时间增加了一个最小值。 (这很烦人,经常发生(自行实现的随机数生成器),所以我实
sys.float_info.epsilon 返回什么? 在我的系统上我得到: >>> sys.float_info.epsilon 2.220446049250313e-16 >>> sys.flo
我正在尝试计算 float 类型的 epsilon 值的近似值(我知道它已经在标准库中)。 这台机器上的 epsilon 值是(打印有一些近似值): FLT_EPSILON = 1.192093e-
为什么 Decimal 数据类型没有 Epsilon 字段? From the manual ,decimal 值的范围是 ±1.0 × 10e−28 到 ±7.9 × 10e28。 The desc
我编写了一个类,用 Java 中的两个 double 来测试相等、小于和大于。我的一般情况是比较可以精确到半美分的价格。 59.005 与 59.395 相比。我为这些情况选择的 epsilon 是否
什么是最小的浮点值 A 使得 (x = 0 && pOppositeToCurrentCave.hasLeftVertexTo(otherLeftX)) { otherLeftX = pOpp
为了简化问题,假设我想计算表达式 a / (b - c)在 float s。 为了确保结果有意义,我可以检查 b和 c相等: float EPS = std::numeric_limits::epsi
我正在进行一些几何计算,这需要我比较基于 double 的坐标。我通常通过添加一些人工 epsilon 来处理这种情况下的浮点不准确问题。这很常见,并且有很多关于该主题的信息。 http://floa
根据维基百科,不同精度数据类型的布局是 single precision :指数 (e):8 位,小数 (f):23 位 double precision :e:11位,f:52位 quadruple
我让球弹来弹去,每次它们碰撞时,它们的速度矢量都会因恢复系数而降低。 现在我的球 CoR 是 0.80。因此,在多次弹跳之后,我的球已经“停止”滚动,因为它们的速度已经变得非常小了。 在什么阶段检查速
我在编码方面的经验很少,并且正在学习 Python 类(class)。我被要求在某个所需的 epsilon 内近似无穷总和,以便该系列中两个连续项之间的差值小于 epsilon。 我搜索了一下,发现了
我试图了解 LR1 解析器的工作原理,但我想到了一个奇怪的问题:如果语法包含 Epsilons 怎么办?例如:如果我有语法: S -> A A -> a A | B B -> a 很清楚如何开始: S
我的团队正在使用财务软件,该软件在 C# float 加倍时公开货币值(value)。有时,我们需要比较这些值以查看它们是否等于零,或者是否低于特定限制。当我注意到此逻辑中的意外行为时,我很快了解到浮
在this question中,我编写了一个脚本,以查找将值存储为总和的一部分然后再次返回时可能出现的最大错误。 我发现最大错误与Number.EPSILON有关: maximumError / Nu
我是一名优秀的程序员,十分优秀!