用Java计算小数超过16位的答案 [英] Calculating an Answer in Java with more than 16 decimal places

查看:473
本文介绍了用Java计算小数超过16位的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小程序来计算Java中的Pi。

I've written a small program to calculate Pi in Java.

我的摘要目前已输入 double

My summations are currently typed double.

我有两个输出,一个是Pi到16位小数(原始输出)。
第二个输出使用 NumberFormat / DecimalFormat 舍入到3-5个小数位。

I have two outputs, one which is Pi to 16 decimal places (the raw output). The second output is rounded using NumberFormat/DecimalFormat to 3-5 decimal places.

我花了一段时间谷歌搜索和浏览Stackoverflow但作为一个初学者我不确定任何关键字可能需要找到我正在寻找的正确方法,所以请原谅我的天真...

I've spent a little while googling and browsing Stackoverflow but being a beginner I'm unsure of any keywords that may be needed to find the correct method I'm looking for, so please excuse my naivety...

是否有可能计算出答案(即Pi)超过16位小数地方呢?说,32?
如果是这样,我该如何处理?

Is it possible to calculate an answer (i.e. Pi) to more than 16 decimal places? Say, 32? If so, how can I approach this?

我看过 BigDecimal ,但我不认为这是我所追求的全部我见过的例子首先预先定义了带有大量小数位的数字,而不是通过计算,但我可能错了。

I've looked at BigDecimal, however I don't think this is what I'm after as all examples I've seen pre-define the number with a large amount of decimal places first, not via a calculation, but I may be wrong.

我理解 double 是64位类型,因此不会超过16位小数。
我也理解使用 long 因为类型会失去精确度...

I understand double's are a 64-bit type so won't achieve more than 16 decimal places. I also understand using long as a type will lose precision...

任何建议或指导将不胜感激。

Any advice or guidance will be appreciated.

谢谢。

更新

不幸的是,我仍然没有使用BigDecimals获得超过16位小数...
我是朝着正确的方向前进吗?

Unfortunately, I'm still not getting any more than 16 decimal places using BigDecimals... Am I heading in the right direction?

再次更新

我刚刚意识到我的问题是 规模 在我的部门期间。我将它改为100,现在我得到Pi到100位小数......我认为 scale 参数允许更高的精度等。它需要除以(BigDecimal divisor,int scale,int roundingMode)方法。请参阅Java Doc >> here<<

I've just realised I think my problem is with scale during my division. I changed it to 100 and now I get Pi to 100 decimal places... I think the scale parameter allows more precision etc. It requires the divide(BigDecimal divisor, int scale, int roundingMode) method. See Java Doc >> here <<

根据Madhava-Leibniz系列中的500,000个术语,我现在可以计算Pi到10,000个小数位,其中有6个正确有效数字。其余的仍在收敛(显然需要1000多个术语才能更准确地表示Pi)。但是为了我们大多数人需要Pi的目的,这很好。

With 500,000 terms from the Madhava–Leibniz series, I can now calculate Pi to 10,000 decimal places with 6 'correct' significant figures. The rest are still converging (and obviously need 1000's more terms to get a more accurate representation of Pi). But for the purposes of what most of us need Pi for, this is fine.

谢谢大家的帮助。

推荐答案

首先,double的宽度与它可以容纳的小数位数没有直接关系。 double值是尾数和指数(以及其他内容),当然,内部是base 2,因此它比double可以容纳X小数位更复杂。

First of all, the width of a double isn't directly related to the number of decimal places it can hold. A double value is a mantissa and an exponent (among other things), and, of course, is base 2 internally, so it's more complex than "a double can hold X decimal places".

在任何情况下,您都应该能够使用 BigDecimal 进行所有计算,只需使用 BigDecimal 而不是内置的算术运算符。

In any case you should be able to use a BigDecimal for all of your calculations, just perform all of your math using the methods of BigDecimal instead of built-in arithmetic operators.

这篇关于用Java计算小数超过16位的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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