C# 中的 Java BigDecimal 类相当于什么?

发布于 2024-09-01 22:31:45 字数 78 浏览 5 评论 0原文

BigDecimal 是 java.math 包中的一个类,它对于处理一定规模的大数有很多好处。 C# 中是否有具有此功能的等效类或数据类型。

BigDecimal is a class in the java.math package that has a lot of benefits for handling big numbers of a certain scale. Is there an equivalent class or data type in c# with this feature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

瘫痪情歌 2024-09-08 22:31:46

您还可以使用 Math.Gmp.Native NuGet 包我写。其源代码可在 GitHub 上获取,文档可在 此处。它向 .NET 公开 GMP 库的所有功能,该库被称为高度优化的任意精度算术图书馆。

任意精度浮点数由 mpf_t 类型。对这些浮点数的运算均以 mpf_ 前缀开头。例如, mpf_addmpf_cmp。每个操作都给出了源代码示例。

You can also use the Math.Gmp.Native NuGet package that I wrote. Its source code is available on GitHub, and documentation is available here. It exposes to .NET all of the functionality of the GMP library which is known as a highly-optimized arbitrary-precision arithmetic library.

Arbitrary-precision floating-point numbers are represented by the mpf_t type. Operations on these floating-point numbers all begin with the mpf_ prefix. For examples, mpf_add or mpf_cmp. Source code examples are given for each operation.

西瓜 2024-09-08 22:31:45

就在最近,我还需要 C# 中的任意精度小数,并发现了这里发布的想法: https://stackoverflow.com/a/4524254 /804614

然后我完成了草案,以支持所有基本算术和比较运算符,以及所有典型数值类型和一些指数方法之间的转换,这是我当时需要的。

它当然不全面,但非常实用,而且几乎随时可用。由于这是一晚编码的结果,我不能保证这个东西没有错误或完全准确,但它对我来说非常有用。无论如何,我想在这里发布它,因为我没有找到任何其他方法可以在 C# 中使用任意精度小数,而无需包含大量库(大多数甚至不是 .net,而是 C++ 的包装器),这些库附带了各种不必要的功能东西。

基本思想是使用 .NET 4.0 的 BigInteger 类型和以 10 为底的指数 (Int32) 构建具有任意大尾数的自定义浮点类型。

如果您发现错误/不准确之处,有建议或任何有建设性的内容,请随时直接编辑我的帖子或发表评论,以便我可以改进答案。

我不完全确定这是否是放置这个东西的最佳位置,但这是关于这个主题的首要问题之一,我真的很想分享我的解决方案。 ;)

编辑:我将实现移至 GitHubGist: https://gist.github.com/JcBernack/0b4eef59ca97ee931a2f45542b9ff06d< /a>

Just recently I also needed an arbitrary precision decimal in C# and came across the idea posted here: https://stackoverflow.com/a/4524254/804614

I then completed the draft to support all basic arithmetic and comparison operators, as well as conversions to and from all typical numerical types and a few exponential methods, which I needed at that time.

It certainly is not comprehensive, but very functional and almost ready-to-use. As this is the result of one night coding, I can not assure that this thing is bug free or entirely exact, but it worked great for me. Anyway, I want to publish it here because I did not find any other way to use arbitrary precision decimals in C# without the need to include massive librarys (mostly not even .net, but wrappers to c++), which come with all kinds of unnecessary stuff.

The basic idea is to build a custom floating-point type with an arbitrary large mantissa using the BigInteger type of .NET 4.0 and a base 10 exponent (Int32).

If you find bugs/inaccuracies, have suggestions or anything constructive, please feel free to directly edit my post or leave a comment so I may improve the answer.

I'm not entirely sure if this is the best spot to place this thing, but this is one of the top questions on SO about this topic and I really want to share my solution. ;)

EDIT: I moved the implementation to GitHubGist: https://gist.github.com/JcBernack/0b4eef59ca97ee931a2f45542b9ff06d

゛清羽墨安 2024-09-08 22:31:45

C# 仅使用 BigInteger 构建了它(在 .NET Framework 4 中)。

decimal 的精度足以满足您的任务吗?它是一个 128 位数字,可以保存 ±1.0 × 10−28 到 ±7.9 × 1028 范围内的值。

C# only has BigInteger built it (in .NET framework 4).

Is decimal enough precision for your task? It's a 128-bit number that can hold values in the range ±1.0 × 10−28 to ±7.9 × 1028.

故事和酒 2024-09-08 22:31:45

那么,除了使用支持 BigDecimal 的第三方库(如果存在)之外,没有简单的解决方法。就我而言,最简单的方法是采用十进制实现(例如来自 mono)并使用 BigInteger 类型重写它。在内部,在 mono 的实现中,十进制类型由三个整数组成。所以我认为这不会很难实施。但我不确定效率。然而,您应该首先考虑使用标准十进制类型,如 codeka 提到的。

Well, apart from using third-party libraries with support of the BigDecimal (if they exist), there are no easy workarounds. The most easy way, as far as i am concerned is to take a decimal implementation( from mono for example) and to rewrite it using the BigInteger type. Internally, in mono's implementation, decimal type is composed from three integers. So i don't think that would be hard to implement. I am not sure about efficiency though. You should first however consider using standard decimal type as codeka mentioned.

聚集的泪 2024-09-08 22:31:45

有一个名为 BigNum 的 C# 库可以满足您的需求,并且在某些情况下还具有其他功能。

例如,它有一个平方根函数,而 BigDecimal 没有:

PrecisionSpec precision = new PrecisionSpec(1024, PrecisionSpec.BaseType.BIN);
BigFloat bf = new BigFloat(13, precision);
bf.Sqrt();
Console.WriteLine(bf.ToString());

维基百科在 http://en.wikipedia.org/wiki/Arbitrary- precision_arithmetic#Libraries

来源:

There's a C# library called BigNum that does what you're looking for, and in some cases has additional functionality.

For example, it has a square root function, which BigDecimal doesn't have:

PrecisionSpec precision = new PrecisionSpec(1024, PrecisionSpec.BaseType.BIN);
BigFloat bf = new BigFloat(13, precision);
bf.Sqrt();
Console.WriteLine(bf.ToString());

Wikipedia has a list of other such libraries at http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Libraries

Sources:

甜中书 2024-09-08 22:31:45

Deveel Math

GitHub:

https://github.com/deveel/deveel-math

作者 GitHub:

< a href="https://github.com/tsutomi" rel="nofollow">Antonello Provenzano

支持:

  • BigComplex
  • BigDecimal
  • BigMath
  • Rational
  • ...

如何安装

从 NuGet 包管理控制台,选择其中的项目将安装该库并键入以下命令

PM> Install-Package dmath

Deveel Math

GitHub:

https://github.com/deveel/deveel-math

Author GitHub:

Antonello Provenzano

Support:

  • BigComplex
  • BigDecimal
  • BigMath
  • Rational
  • ...

How to Install It

From the NuGet Package Management console, select the project where the library will be installed and type the following command

PM> Install-Package dmath
风尘浪孓 2024-09-08 22:31:45

当问题最初发布时,这可能不是一个选项,但在 C# 代码中使用 BigDecimal 的一种非常简单的方法是安装 IKVM.NET 通过 NuGet 进行打包:

PM> Install-Package IKVM

然后按照在 Java 中的方式进行操作:

using System;
using java.math;

namespace BigDecimalDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(args[0]);
            Console.WriteLine(Factorial(n));
        }

        static BigDecimal Factorial(int n)
        {
            return n == 1
                ? BigDecimal.ONE
                : Factorial(n - 1).multiply(new BigDecimal(n));
        }
    }
}

根据您使用 IKVM 的程度,可能会偶尔出现互操作问题偶然发现,但根据我的经验,它通常对于像这样的简单事情非常有效。

This may not have been an option when the question was originally posted, but one really easy way to use a BigDecimal in your C# code is to install the IKVM.NET package via NuGet:

PM> Install-Package IKVM

Then do exactly as you would in Java:

using System;
using java.math;

namespace BigDecimalDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(args[0]);
            Console.WriteLine(Factorial(n));
        }

        static BigDecimal Factorial(int n)
        {
            return n == 1
                ? BigDecimal.ONE
                : Factorial(n - 1).multiply(new BigDecimal(n));
        }
    }
}

Depending on how far you go with IKVM there can be the occasional interop issue to stumble through but in my experience it usually works great for simple stuff like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文