是否有一个包可以实现非常大的高精度数字?

发布于 2024-12-11 17:55:58 字数 408 浏览 0 评论 0原文

我发现我需要以高精度计算大量数字。例如:5422300.8452。存储这些数据以便简单的算术运算符可以对其进行操作的最佳方式是什么?我一直在尝试组合长整型和双精度型,但经过大量计算后它变得很复杂。 我确信有一个简单的解决方案,只是我还不太了解。

好的,基本上,我明白了: 我的问题是我想保留小数点后 4 位的精度,但输入较大的数字会开始截断数字。例如,我有这样的:

Double Remain = (double) Math.round((double quantity/12)*10000)/10000;

这适用于较小的数字,但 900000000 仍然会被截断为 7.5 左右。

所以我基本上需要将数字除以 然后取 2 的模并除以分母,并对结果进行舍入。 然后添加2。

它起作用了!

I'm finding that i'm needing to compute large numbers to high precision. For example: 5422300.8452. What is the best way to store this data so that simple arithmetic operators can act on it? I've been trying to do combinations of Longs and Doubles but it gets complicated after a significant number of computations.
I'm sure there is a simple solution, i'm just not so knowledgeable yet.

Ok Basically, I figured it out:
My problem was that I wanted to keep 4 decimal places of precision but entering a high number started truncating digits. For example, I had this:

Double Remain = (double) Math.round((double quantity/12)*10000)/10000;

This works for smaller numbers but 900000000 still gets truncated to 7.5 something.

So I basically need to divide the numbers as long
and then take the modulus of the 2 and divide it by the denominator, and round the result.
Then add the 2.

It works!

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

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

发布评论

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

评论(3

坠似风落 2024-12-18 17:55:58

BigInteger 和 BigDecimal 是您需要的类正在寻找。不过,您将无法使用简单的算术运算符,因为 Java 中不存在运算符重载。但这些类具有所有简单操作所需的方法。

BigInteger and BigDecimal are the classes you're looking for. You won't be able to use simple arithmetic operators, though, because operator overloading doesn't exist in Java. But those classes have the required methods for all the simple operations.

我不吻晚风 2024-12-18 17:55:58

我不确定我是否理解您的后续问题,但为了划分,代码可能如下所示:

BigDecimal x = new BigDecimal(45);
BigDecimal y = new BigDecimal(12);
BigDecimal result = x.divide(y);

这就是 JB Nizet 描述的缺点,不能使用运算符,因此

BigDecimal x = new BigDecimal(45);
BigDecimal y = new BigDecimal(12);
BigDecimal result = x/y;

不起作用。

I'm not sure if I understand your followup question, but in order to divide, the code could look as follows:

BigDecimal x = new BigDecimal(45);
BigDecimal y = new BigDecimal(12);
BigDecimal result = x.divide(y);

Thats the downside described by JB Nizet, no operators can be used, so

BigDecimal x = new BigDecimal(45);
BigDecimal y = new BigDecimal(12);
BigDecimal result = x/y;

won't work.

哭了丶谁疼 2024-12-18 17:55:58

好的,基本上,我明白了:
我的问题是我想保留小数点后 4 位的精度,但输入较大的数字会开始截断数字。例如,我有这样的:

Double Remain = (double) Math.round((double quantity/12)*10000)/10000;

这适用于较小的数字,但 900000000 仍然会被截断为 7.5 左右。

所以我基本上需要将数字除以
然后取 2 的模并除以。

如果解释我想做的事情的目的,也许会有帮助。我希望代码显示特定数量的有效数字。如果我使用 Double 作为变量来存储要除的用户输入,则许多数字可能会被截断(取决于结果小数位数)。我需要一种方法来标准化整数中变量的范围,同时保持数字精确到小数点后 4 位。

换句话说,无论商需要多少位小数,我都需要能够处理相同大小的数字。

为了做到这一点,我获取用户输入,并将其除以文字 (12)。
然后我以同样的方式取模,并将模除以 12。
然后我将两者拼凑在一起,如下所示:

Quotient.toString() + Remainder.toString().substring(1)

Ok Basically, I figured it out:
My problem was that I wanted to keep 4 decimal places of precision but entering a high number started truncating digits. For example, I had this:

Double Remain = (double) Math.round((double quantity/12)*10000)/10000;

This works for smaller numbers but 900000000 still gets truncated to 7.5 something.

So I basically need to divide the numbers as long
and then take the modulus of the 2 and divide.

Perhaps it would've helped if explained the purpose of what I was trying to do. I wanted the code to display a specific number of significant digits. If I use a Double as the variable to store user input to be divided, many digits might get truncated (depending on the number of resulting decimal places.) I needed a way to standardize the scope of the variable in the whole numbers while keeping the numbers precise to 4 decimal places.

In other words, I need to be able to handle the same size number regardless of how many decimal places the quotient takes.

In order to do this I take the user input, and divide it by the literal (12).
Then I take the modulus the same way and divide the modulus by 12.
I then piece the 2 together like so:

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