什么是接受浮点不精确在C#中受益 [英] What's the benefit of accepting floating point inaccuracy in c#

查看:128
本文介绍了什么是接受浮点不精确在C#中受益的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的脑海这个问题最后天了,我挣扎短语我的问题。不过,我想我已经钉我想知道什么。



为什么C#接受使用浮点来存储数据不准确?这有什么用它优于其他方法的好处?



例如, Math.Pow(的Math.sqrt(2) 2)不是在c#苛求。有编程语言,可以精确地计算出它(例如,数学)。



我能想到的一个说法是,它的计算究竟是慢了许多,然后只是不准确应对,但数学和放大器; Matlab的用于计算巨大的科学问题,所以我觉得很难相信,这些语言确实显著比C#慢。



那么,为什么呢?



PS:我为你滥发带着这些疑问对不起,你一直非常有帮助


解决方案

为什么C#接受使用浮点来存储数据不准确?




C#不接受超过精度性能的权衡; 。用户做什么,或者不,接受



C#有三个浮点类型 - 浮动,双和十进制 - 因为这三种类型满足绝大多数的需求现实世界中的程序员。



float和double是良好的科学计算,其中一个答案是正确的三个或四个小数位总是非常接近,因为这是精确的原始测量进来时。假设你除以3 10.00,并得到3.333333333333。由于原始测量可能是精确到只有0.01,这一事实所计算的结果是关闭小于0.0000000000004是无关紧要的。在科学计算,你不是代表已知将要-确切数量。的不精确在第十五位小数是无关紧要的,如果原来的测量值只有精确到小数点后第二位



这当然是不正确的的财务计算。到财务计算操作数通常是精确到小数点后两位,并表示具体的数量即可。小数有利于财经计算,因为小数点运算结果的确切的规定,所有的输入和输出,可以准确地表示为小数(他们都在一个合理的范围内)。小数仍然有舍入误差,当然,不过这是精确的操作,正是那些你可能希望在做财务计算时是准确的。




和什么是使用它比其他方法的好处?




您应该说明你想比较有什么其他方法反对。有计算机上执行计算的许多不同的技术。




例如,Math.Pow(的Math.sqrt(2),2)不准确的在C#。有编程语言,可以精确地计算出它(例如,数学)。




让我们清楚这一点;数学不算根2完全相同;数目是不合理的,因此它不能被存储的任何有限数量精确计算。相反,数学是什么做的是它代表了数字作为描述数字是如何产生的对象。如果你说给我2的平方根,那么本质上数学分配一个对象,它的意思是平方根运营商的应用程序的确切人数2。如果再正方形,它有说:如果你多东西,是别的东西的平方根,还给原来的价值的专用逻辑。数学有代表不同的特别号码,以及像PI或E,以及规则这些号码的各种操作是如何结合在了一起庞大的身躯对象。



基本上,这是一个的符号的制度;它操纵数以同样的方式,当他们这样做的铅笔和纸的数学人呢。大多数计算机程序操作数字就像一个计算器:立即执行的计算和圆其关闭。如果这是不能接受的,那么你应该坚持一个象征性的系统。




我能想到的一个说法是,计算它到底是很多慢则只是不准确应对,但数学和放大器; Matlab的用于计算巨大的科学问题,所以我觉得很难相信,这些语言确实显著比C#慢。




这不是他们是慢,虽然浮点乘法真的是在现代硬件上快得令人难以置信。它是象征性的计算引擎的非常复杂的的。它编码基础数学的的的所有的规则,并且有很多的这些规则! C#是不是打算成为一个专业级的符号计算引擎,它的目标是一个通用编程语言。


I've had this problem on my mind the last few days, and I'm struggling to phrase my question. However, I think I've nailed what I want to know.

Why does c# accept the inaccuracy by using floating points to store data? And what's the benefit of using it over other methods?

For example, Math.Pow(Math.Sqrt(2),2) is not exact in c#. There are programming languages that can calculate it exactly (for example, Mathematica).

One argument I could think of is that calculating it exactly is a lot slower then just coping with the inaccuracy, but Mathematica & Matlab are used to calculate gigantic scientific problems, so I find it hard to believe those languages are really significantly slower than c#.

So why is it then?

PS: I'm sorry for spamming you with these questions, you've all been really helpful

解决方案

Why does c# accept the inaccuracy by using floating points to store data?

"C#" doesn't accept the tradeoff of performance over accuracy; users do, or do not, accept that.

C# has three floating point types - float, double and decimal - because those three types meet the vast majority of the needs of real-world programmers.

float and double are good for "scientific" calculations where an answer that is correct to three or four decimal places is always close enough, because that's the precision that the original measurement came in with. Suppose you divide 10.00 by 3 and get 3.333333333333. Since the original measurement was probably accurate to only 0.01, the fact that the computed result is off by less than 0.0000000000004 is irrelevant. In scientific calculations, you're not representing known-to-be-exact quantities. Imprecision in the fifteenth decimal place is irrelevant if the original measurement value was only precise to the second decimal place.

This is of course not true of financial calculations. The operands to a financial calculation are usually precise to two decimal places and represent exact quantities. Decimal is good for "financial" calculations because decimal operation results are exact provided that all of the inputs and outputs can be represented exactly as decimals (and they are all in a reasonable range). Decimals still have rounding errors, of course, but the operations which are exact are precisely those that you are likely to want to be exact when doing financial calculations.

And what's the benefit of using it over other methods?

You should state what other methods you'd like to compare against. There are a great many different techniques for performing calculations on computers.

For example, Math.Pow(Math.Sqrt(2),2) is not exact in c#. There are programming languages that can calculate it exactly (for example, Mathematica).

Let's be clear on this point; Mathematica does not "calculate" root 2 exactly; the number is irrational, so it cannot be calculated exactly in any finite amount of storage. Instead, what mathematica does is it represents numbers as objects that describe how the number was produced. If you say "give me the square root of two", then Mathematica essentially allocates an object that means "the application of the square root operator to the exact number 2". If you then square that, it has special purpose logic that says "if you square something that was the square root of something else, give back the original value". Mathematica has objects that represent various special numbers as well, like pi or e, and a huge body of rules for how various manipulations of those numbers combine together.

Basically, it is a symbolic system; it manipulates numbers the same way people do when they do pencil-and-paper math. Most computer programs manipulate numbers like a calculator: perform the calculation immediately and round it off. If that is not acceptable then you should stick to a symbolic system.

One argument I could think of is that calculating it exactly is a lot slower then just coping with the inaccuracy, but Mathematica & Matlab are used to calculate gigantic scientific problems, so I find it hard to believe those languages are really significantly slower than c#.

It's not that they're slower, though multiplication of floating points really is incredibly fast on modern hardware. It's that the symbolic calculation engine is immensely complex. It encodes all the rules of basic mathematics, and there are a lot of those rules! C# is not intended to be a professional-grade symbolic computation engine, it's intended to be a general-purpose programming language.

这篇关于什么是接受浮点不精确在C#中受益的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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