简体   繁体   English

a == b,指数表示法。 八度怪异?

[英]a == b, exponential notation. Octave weirdness?

If I type this into octave: 如果我将其键入八度:

a = 8*10^-5;
b = 8.0e-005;
a==b

I get the answer: 我得到答案:

ans = 1

But if I type this into octave: 但是,如果我将其键入八度:

a = 3*10^-5;
b = 3.0e-005;
a==b

I get this answer 我得到这个答案

ans = 0

Am I missing something? 我想念什么吗? Is this not the right way to test for equality? 这不是测试平等的正确方法吗?

This is a result of loss of precision during computations involving floating point numbers, for example in Java 3 * Math.pow(10, -5) would give you '2.9999999999999997E-5' which no longer matches b in your second example. 这是在涉及浮点数的计算过程中精度下降的结果,例如在Java 3 * Math.pow(10, -5)将为您提供“ 2.9999999999999997E-5”,在第二个示例中不再与b匹配。 This is linked to how floating point values are represented and evolve during the evaluation of an expression. 这与在评估表达式期间浮点值如何表示和演化有关。 For more elaborate perspectives, you can google 'loss of precision' suffixing that with the context / environment you are using to evaluate these expressions. 对于更详尽的观点,您可以在“精度损失”后缀Google上,并在后缀中加上用于评估这些表达式的上下文/环境。 Hope this helps. 希望这可以帮助。

This is a common problem with the way floating point numbers are represented in computer systems. 这是计算机系统中浮点数表示方式的常见问题。

Many numbers which are 'round' in decimal become repeating fractions in the binary representation used by the computer, and they have to be truncated at some point, for a very slight loss of precision. 在计算机使用的二进制表示形式中,许多十进制“舍入”的数字成为重复的分数,并且必须在某些时候将其截断,以免造成非常小的精度损失。 If two floating point numbers are arrived at differently, you have to compare them with a little bit of slop, eg abs(ab) < EPSILON where EPSILON is an appropriate constant like 1e-12. 如果两个浮点数的取值不同,则必须将它们与一点斜率进行比较,例如abs(ab) < EPSILON ,其中EPSILON是合适的常数,例如1e-12。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM