为什么不能C#计算数学函数的精确值 [英] Why can't c# calculate exact values of mathematical functions

查看:104
本文介绍了为什么不能C#计算数学函数的精确值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能C#做任何详细的操作。

Why can't c# do any exact operations.

Math.Pow(Math.Sqrt(2.0),2) == 2.0000000000000004

我知道怎么加倍工作,我知道那里的舍入误差是的,我知道这是的几乎的正确值,我知道你不能无限的数字存储在一个有限的两倍。但为什么没有一种方式,C#可以精确计算的话,而我的计算器可以做到这一点。

I know how doubles work, I know where the rounding error is from, I know that it's almost the correct value, and I know that you can't store infinite numbers in a finite double. But why isn't there a way that c# can calculate it exactly, while my calculator can do it.

修改

这不是我的计算器,我只是举个例子:

It's not about my calculator, I was just giving an example:

http://www.wolframalpha.com/input/?i=Sqrt%282.000000000000000000000000000000000000000000000000000000000000000000000000000000001%29%5E2

干杯

推荐答案

机会是你的计算器的不能的做到这一点正是 - 但它可能是存储更多信息,比它的显示,所以现蕾后错误的显示内容范围之外结束。如果不是这样,或者它的错误发生,取消了在这种情况下 - 但是这并不等同于得到它完全正确一字一板地

Chances are your calculator can't do it exactly - but it's probably storing more information than it's displaying, so the error after squaring ends up outside the bounds of what's displayed. Either that, or its errors happen to cancel out in this case - but that's not the same as getting it exactly right in a deliberate way.

另一种选择是,计算器是要记住,导致以前的结果的操作以及应用代数抵消操作......这似乎不可思议的事,但。 .NET的确实的不会尝试这样做 - 这将计算中间值(二根),然后将其平方

Another option is that the calculator is remembering the operations that resulted in the previous results, and applying algebra to cancel out the operations... that seems pretty unlikely though. .NET certainly won't try to do that - it will calculate the intermediate value (the root of two) and then square it.

如果你觉得的的可以做的更好,我建议你尝试写出来的2的平方根来(说)50位小数,然后准确地将其平方。看看你是否拿出确切2 ...

If you think you can do any better, I suggest you try writing out the square root of two to (say) 50 decimal places, and then square it exactly. See whether you come out with exactly 2...

这篇关于为什么不能C#计算数学函数的精确值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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