简体   繁体   English

在 C# 中使用 System.Decimal 的数学运算

[英]Math operations using System.Decimal in C#

I need to be able to use the standard math functions on decimal numbers.需要能够对十进制数使用标准数学函数。 Accuracy is very important .准确性非常重要 double is not an acceptable substitution. double不是可接受的替代品。 How can math operations be implemented with decimal numbers in C#? C#中的十进制数如何实现数学运算?

edit I am using the System.Decimal .编辑我正在使用System.Decimal My issue is that System.Math does not work with System.Decimal .我的问题是System.Math不适用于System.Decimal For example, the following functions do not work with System.Decimal :例如,以下函数不适用于System.Decimal

  • System.Math.Pow System.Math.Pow
  • System.Math.Log System.Math.Log
  • System.Math.Sqrt System.Math.Sqrt

Well, Double uses floating point math which isn't what you're after unless you're doing trigonometry for 3D graphics or something.好吧, Double使用浮点数学,这不是你所追求的,除非你正在为 3D 图形或其他东西做三角函数。

If you need to do simple math operations like division, you should use System.Decimal .如果你需要做简单的数学运算,比如除法,你应该使用System.Decimal

From MSDN: The decimal keyword denotes a 128-bit data type.来自 MSDN: decimal 关键字表示 128 位数据类型。 Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations.与浮点类型相比,十进制类型的精度更高,范围更小,适用于金融和货币计算。

Update: After some discussion, the problem is that you want to work with Decimals, but System.Math only takes Doubles for several key pieces of functionality.更新:经过一番讨论,问题是您想使用小数,但 System.Math 仅将 Doubles 用于几个关键功能。 Sadly, you are working with high precision numbers, and since Decimal is 128 bit and Double is only 64, the conversion results in a loss of precision.遗憾的是,您使用的是高精度数字,并且由于 Decimal 是 128 位,而 Double 仅为 64,因此转换会导致精度损失。

Apparently there are some possible plans to make most of System.Math handle Decimal, but we aren't there yet.显然有一些可能的计划让大部分 System.Math 处理小数,但我们还没有。

I googled around a bit for math libraries and compiled this list:我搜索了一下数学库并编译了这个列表:

  1. Mathdotnet , A mathematical open source (MIT/X11, LGPL & GPL) library written in C#/.Net, aiming to provide a self contained clean framework for symbolic algebraic and numerical / scientific computations. Mathdotnet ,一个用 C#/.Net 编写的数学开源(MIT/X11、LGPL 和 GPL)库,旨在为符号代数和数值/科学计算提供一个自包含的干净框架。

  2. Extreme Optimization Mathematics Library for .NET (paid) .NET 的极限优化数学库(付费)

  3. DecimalMath A relative newcomer, this one advertises itself as: Portable math support for Decimal that Microsoft forgot and more. DecimalMath一个相对较新的人,这个广告自称: Microsoft 忘记的对 Decimal 的便携式数学支持等等。 Sounds promising.听起来很有希望。

DecimalMath contains all functions in System.Math class with decimal argument analogy DecimalMath包含System.Math class 中的所有函数,带有decimal参数类比

Note: it is my library and also contains some examples in it注意:这是我的库,其中还包含一些示例

You haven't given us nearly enough information to answer the question.你没有给我们足够的信息来回答这个问题。

decimal and double are both inaccurate.十进制和双精度都是不准确的。 The representation error of decimals is zero when the quantity being represented is exactly equal to a fraction of the form (x/10 n ) for suitable choices of x and n.对于适当的x和n选择,当表示的数量完全等于形式(x/10 n )的一部分时,小数的表示误差为零 The representation error of doubles is zero when the quantity is exactly equal to a fraction of the form (x/2 n ) again for suitable choices of x and n.当数量再次等于x和n的合适选择的形式(x/2 n )的一小部分时,双打的表示误差为零。

If the quantities you are dealing with are not fractions of that form then you will get some representation error, period.如果您正在处理的数量不是该表格的一部分,那么您将得到一些表示错误,句号。 In particular, you mention taking square roots.特别是,您提到取平方根。 Many square roots are irrational numbers;许多平方根是无理数; they have no fractional form, so any representation format that uses fractions is going to give small errors.它们没有分数形式,因此任何使用分数的表示格式都会产生小错误。

Can you explain what you are doing in hugely more detail?你能更详细地解释你在做什么吗?

Use the decimal data type, which has 128 bits of precision:使用具有 128 位精度的十进制数据类型:

http://msdn.microsoft.com/en-us/library/364x0z75(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/364x0z75(v=vs.80).aspx

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

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