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

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

问题描述

需要能够在十进制数字上使用标准数学函数.准确性非常重要. double不是可接受的替代.如何在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#?

修改 我正在使用System.Decimal.我的问题是System.Math不能与System.Decimal一起使用.例如,以下功能不适用于System.Decimal:

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

推荐答案

好,浮点数学不是除非您要对3D图形进行三角测量或其他操作,否则您就不会这么做.

Well, Double uses floating point math which isn't what you're after unless you're doing trigonometry for 3D graphics or something.

如果需要进行除法之类的简单数学运算,则应使用 System.Decimal .

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

从MSDN:十进制关键字表示128位数据类型.与浮点类型相比,十进制类型具有更高的精度和更小的范围,使其适用于财务和货币计算.

更新:经过一番讨论,问题是您想使用Decimals,但是System.Math仅将Doubles用于几个关键功能.可悲的是,您正在使用高精度数字,并且由于Decimal是128位而Double只有64,因此转换会导致精度损失.

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.

显然,有一些可能的方案可以使大部分System.Math处理Decimal,但我们还没有.

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

我搜索了一些数学库,最后发现了一个现已删除的StackOverflow问题,其中TheVillageIdiot提到了以下库:

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 ,这是一个用数学语言编写的开源(MIT/X11,LGPL& GPL)库C#/.Net,旨在为符号代数和数值/科学计算提供一个自包含的干净框架.

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)Microsoft®的IMSL™C#数字库. NET应用程序(付费)

3) .NET的极端优化数学库

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

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