在C#中使用System.Decimal进行数学运算? [英] Math operations using System.Decimal in C#?

查看:106
本文介绍了在C#中使用System.Decimal进行数学运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要可以使用十进制数字上的标准数学函数。准确度非常重要 double 不是可接受的替代品。有没有任何图书馆允许这个?



编辑
我正在使用 System.Decimal 。我的问题是 System.Math 不适用于 System.Decimal

  System.Math.Pow 
System.Math.Log
System.Math.Sqrt
/ pre>

所有功能不适用于 System.Decimal

解决方案

嗯, Double 使用浮点数学不是除非你对三维图形或其他东西进行三角测量,否则你要做什么。



如果你需要像分区一样做简单的数学运算,你应该使用 System.Decimal



从MSDN:十进制关键字表示128位数据类型。与浮点类型相比,十进制类型具有更高的精度和更小的范围,这使得它适合金融和货币计算。



更新:经过一些讨论,问题在于您想使用小数点,但是System.Math只需要双重数位关键功能。可悲的是,您正在使用高精度数字,并且由于十进制是128位,Double只有64位,所以转换会导致精度损失。



显然有一些可能的计划让大部分System.Math处理十进制,但是我们还没有。



我在数学库中搜索了一下,我终于找到了一个现在,删除StackOverflow问题,其中TheVillageIdiot提到以下库:



1) Mathdotnet ,一个用C#/。Net编写的数学开源(MIT / X11,LGPL& GPL)库,旨在为符号代数和数值/科学计算提供一个自包含的清洁框架。



2) IMSL ™C#Microsoft®.NET应用程序数值库(付费)



3) .NET极端优化数学库


I need to be able to use the standard math functions on decimal numbers. Accuracy is very important. double is not an acceptable substitution. Are there any libraries that allow for this?

edit I am using the System.Decimal. My issue is that System.Math does not work with System.Decimal.

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

Are all functions that do not work with System.Decimal

解决方案

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.

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. 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 I finally found a now-deleted StackOverflow question where TheVillageIdiot mentioned the following libraries:

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) IMSL™ C# Numerical Library for Microsoft® .NET Applications (Paid)

3) Extreme Optimization Mathematics Library for .NET

这篇关于在C#中使用System.Decimal进行数学运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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