如何比较Perl中不仅仅是平等的浮点数 [英] How to compare floating points beyond just equality in perl

查看:70
本文介绍了如何比较Perl中不仅仅是平等的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较两个大于或等于和小于或等于的浮点数.以下代码仅允许我测量相等性(对或错).我需要测量> =和< =.我意识到比较浮点数在任何语言中都是一个问题.我试图查找有关此问题的先前问题.我所能找到的只是为了平等而进行的比较.有什么建议吗?

I am trying to compare two floats for greater than or equal to, and less than or equal to. The following code only allows me to measure equality (true or false). I need to measure >= and <=. I realize comparing floats is an issue in any language. I've tried to look up previous questions regarding this. All I could find was comparison for equality only. Any suggestions?

#!/usr/bin/perl 

my $a = 0.0;
my $b = 0.1;

if (abs($a - $b) < 0.0000001){
    print "True\n";
}
else{
    print "False\n";
}

推荐答案

您要查询的方案是您有两个数字$a$b,它们是您在尝试计算某些精确值时所计算的值数学值 a b .但是,由于任何数值算术在计算中都有错误,因此$a$b值包含(可能)使它们与 a b 不同的错误.然后,仅给定$a$b,您想确定 a b 之间的关系.

The scenario you are asking about is that you have two numbers, $a and $b, that are values you have computed while trying to calculate some exact mathematical values a and b. However, because any numerical arithmetic has errors in calculation, the values $a and $b contain errors that (probably) make them different from a and b. Then, given only $a and $b, you want to determine a relationship between a and b.

这里的规则是垃圾进,垃圾出.当您的数字包含错误时,可以使用有限的方法进行处理.您无法完美地确定这种关系.

The rule here is garbage in, garbage out. When you have numbers containing errors, there are limited things you can do with them. You cannot determine the relationship perfectly.

$a a 有多少不同? $b b 有多少不同?这个问题没有普遍的答案.这取决于您执行了哪些计算以获得这些值以及所涉及的数字.错误的范围可能从零到无穷大,或者可能会为您带来非数字(NaN)结果.确定错误的程度在很大程度上取决于应用程序.没有您正在执行的计算的详细信息,就无法给出答案.

How much can $a differ from a? How much can $b differ from b? There is no general answer to this question. It depends on what calculations you performed to get these values and what numbers were involved. The errors could range from zero to infinity or could give you non-numeric (NaN) results. Determining how much the error can be is hugely application-specific. No answer can be given without details about the computations you are performing.

计算出可能有多少错误之后,您将如何处理?错误的数量告诉您$a$b中可能有多少斜率".如果$a$b的差异远大于该斜率,那么您可以确定 a b 肯定不同.但是,如果$a$b在该倾斜距离之内,您能说什么?

After you have calculated how much error there can be, what do you do with it? The amount of error tells you how much "slop" there might be in $a and $b. If $a and $b differ by more than that amount of slop, terrific, you can be sure that a and b are definitely different. But, if $a and $b are within that slop distance, what can you say?

无法确定.您不知道 a b 是否相等, a 是否更大,或者 b 是否更大.那么在这种情况下您想要什么答案?

No determination is possible. You cannot know whether a and b are equal, whether a is greater, or whether b is greater. So what answer do you want in that case?

有一个神话,经常在Stack Overflow上重复出现,应该比较浮点数的相等性和容差.当计算值在公差范围内时,用这种神话给出的算法几乎总是将报告的数字显示为相等.但是没有理由应该是正确的答案.如果告知两个数字不相等,则某些应用程序将中断.

There is a myth, often repeated on Stack Overflow, that floating-point numbers ought to be compared for equality with a tolerance. The algorithm given with this myth almost always shows numbers reported as equal when the calculated values are within tolerance. But there is no reason that should be the right answer. Some applications will break if told two numbers are equal when they are not.

因此,在无法确定正确答案时应给出的答案取决于您的应用程序.

So the answer to be given when the correct answer cannot be determined depends on your application.

这篇关于如何比较Perl中不仅仅是平等的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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