简体   繁体   中英

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. How can math operations be implemented with decimal numbers in C#?

edit I am using the System.Decimal . My issue is that System.Math does not work with System.Decimal . For example, the following functions do not work with System.Decimal :

  • System.Math.Pow
  • System.Math.Log
  • 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.

If you need to do simple math operations like division, you should use System.Decimal .

From MSDN: The decimal keyword denotes a 128-bit data type. 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.

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.

Apparently there are some possible plans to make most of System.Math handle Decimal, but we aren't there yet.

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.

  2. Extreme Optimization Mathematics Library for .NET (paid)

  3. DecimalMath A relative newcomer, this one advertises itself as: Portable math support for Decimal that Microsoft forgot and more. Sounds promising.

DecimalMath contains all functions in System.Math class with decimal argument analogy

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. 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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