gpt4 book ai didi

math - 如何为浮点选择epsilon值?

转载 作者:行者123 更新时间:2023-12-04 13:22:29 26 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

5年前关闭。




Improve this question




既然我们知道 0.1 + 0.2 != 0.3由于有限的数字表示,我们需要改为检查帽子abs(0.1+0.2 - 0.3) < ε .问题是,对于不同的类型,我们一般应该选择什么ε值呢?是否可以根据位的数量以及可能执行的操作的数量和类型来估计它?

最佳答案

您可以使用以下算法估计机器 epsilon。您需要将此 epsilon 乘以整数值 1+(log(number)/log(2)) .在为方程中的所有数字确定此值后,您可以使用 error analysis估计特定计算的 epsilon 值。

epsilon=1.0

while (1.0 + (epsilon/2.0) > 1.0) {
epsilon = epsilon /2.0
}
//Calculate error using error analysis for a + b
epsilon_equation=Math.sqrt(2*epsilon*epsilon)

document.write('Epsilon: ' + epsilon_equation+'<br>')
document.write('Floating point error: ' + Math.abs(0.2 + 0.4 -0.6)+'<br>')
document.write('Comparison using epsilon: ')
document.write(Math.abs(0.2 + 0.4 -0.6)<epsilon_equation)

根据您的评论,我在 C# 中尝试了相同的方法,它似乎有效:
using System;

namespace ConsoleApplication
{

public class Program
{
public static void Main(string[] args)
{
double epsilon = 1.0;

while (1.0 + (epsilon/2.0) > 1.0)
{
epsilon = epsilon/2.0;
}

double epsilon_equation = Math.Sqrt(2*epsilon*epsilon);

Console.WriteLine(Math.Abs(1.0 + 2.0 - 3.0) < Math.Sqrt(3.0 * epsilon_equation * epsilon_equation));
}
}
}

关于math - 如何为浮点选择epsilon值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com