将浮点变量的作用域影响他们的价值观? [英] Will the scope of floating point variables affect their values?

查看:122
本文介绍了将浮点变量的作用域影响他们的价值观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们在一个控制台应用程序执行下面的C#代码,我们会得到一个消息,如的总和不等于

If we execute the following C# code on a console application, we will get a message as The sums are Not equal.

如果我们取消注释行之后执行它的System.Console.WriteLine(),我们将得到一个消息作为的和相等

If we execute it after uncommenting the line System.Console.WriteLine(), we will get a message as The sums are equal.

    static void Main(string[] args)
    {
        float f = Sum(0.1f, 0.2f);
        float g = Sum(0.1f, 0.2f);

        //System.Console.WriteLine("f = " + f + " and g = " + g);

        if (f == g)
        {
            System.Console.WriteLine("The sums are equal");
        }
        else
        {
            System.Console.WriteLine("The sums are Not equal");
        }
    }

    static float Sum(float a, float b)
    {
        System.Console.WriteLine(a + b);
        return a + b;
    }



,这是什么行为,实际的原因是什么?

What is the actual reason for this behavior?

推荐答案

这不涉及到的范围。它的栈动力学和浮点处理的结合。编译器有一定的了解,将有助于使这种有悖常理的行为清楚。

It's not related to scope. It's the combination of the stack dynamics and floating point handling. Some knowledge of compilers will help make this counterintuitive behavior clear.

在该 Console.WriteLine 被注释,值˚F先按g 是计算堆栈上,并呆在那里,直到你在你的主要方法通过平等的测试后, 。

When the Console.WriteLine is commented, the values f and g are on the evaluation stack and stay there until after you've passed the equality test in your Main method.

Console.Writeline 没有评论,值˚F先按g 从计算堆栈的调用堆栈移动在调用的瞬间,恢复到计算堆栈时控制台。的WriteLine 的回报。和你比较如果(F == G)是事后进行。值这个存储的调用堆栈时可能会出现某些圆整,并可能会丢失一些信息。

When Console.Writeline is not commented, the values f and g are moved from the evaluation stack to the call stack at the moment of the invocation, to be restored to the evaluation stack when Console.WriteLine returns. And your comparison if (f == g) is done afterwards. Some rounding can occur during this storing of values to the call stack and some information can be lost.

当你调用场景 Console.WriteLine ˚F 先按g 在对比测试是不一样的价值观。他们一直在复制并恢复到具有精度和舍入的规则不同的格式,由虚拟机。

In the scenario where you do invoke Console.WriteLine, the f and the g in the comparison test are not the same values. They've been copied and restored to a format that has different rules on precision and rounding, by the virtual machine.

在您的特定代码,当 Console.WriteLine 被注释掉的调用,计算堆栈永远不会存储在调用栈,并没有发生四舍五入。因为它是允许平台的实现提供计算堆栈上提高精度,这种差异可能会出现。

In your particular code, when the invocation of Console.WriteLine is commented, the evaluation stack is never stored to the call stack and no rounding occurs. Because it is permitted for implementations of the platform to provide improved precision on the evaluation stack, this discrepancy can arise.

修改的我们什么在这种情况下,打由CLI规范允许的。在第I.12.1.3记载:

EDIT What we're hitting in this case is allowed by the CLI specification. In section I.12.1.3 it reads:

有关浮点数(静态,数组元素,
存储位置类字段)是固定大小。支持的存储大小
是FLOAT32和float64。其他地方(计算堆栈上,如
参数,作为返回类型和局部变量)浮点
号使用的是内部浮点类型表示。在每
这样的实例中,在名义类型的变量或表达的是
任float32or float64,但其值可以在内部表示
相附加范围和/或精度。内部
浮点表示的大小依赖于实现的,可以改变,
和应有的精度至少一样大所代表的变量或
表达的。

Storage locations for floating-point numbers (statics, array elements, and fields of classes) are of fixed size. The supported storage sizes are float32 and float64. Everywhere else (on the evaluation stack, as arguments, as return types, and as local variables) floating-point numbers are represented using an internal floating-point type. In each such instance, the nominal type of the variable or expression is either float32or float64, but its value can be represented internally with additional range and/or precision. The size of the internal floating-point representation is implementation-dependent, can vary, and shall have precision at least as great as that of the variable or expression being represented.

这是这句话的关键词是依赖于实现和可能会有所不同。在OP的情况下,我们看到他的执行确实如此不同而不同。

The keywords from this quote are "implementation-dependent" and "can vary". In the OP's case, we see his implementation does indeed vary.

在Java平台非strictfp浮点运算也有一个相关的问题,更多信息检查也是我的回答的将浮在JVM点运算给所有平台上相同的结果?

Non-strictfp floating point arithmetic in the Java platform also has a related issue, for more info check also my answer to Will floating point operations on the JVM give the same results on all platforms?

这篇关于将浮点变量的作用域影响他们的价值观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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