使用BigDecimal打印Pi [英] Using BigDecimal to print Pi

查看:118
本文介绍了使用BigDecimal打印Pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图使用​​BigDecimal来打印Pi,但事实证明这是不正确的

I'm tring to use BigDecimal to print Pi, but it turns out it's not accurate

    BigDecimal d = new BigDecimal(Math.PI); 
    System.out.println(d); 

以上答案给我"3.141592653589793115997963468544185161590576171875" 但是"3.141592653589793"之后的数字不正确

The above answer gives me "3.141592653589793115997963468544185161590576171875" but the digits after "3.141592653589793" is incorrect

这是怎么回事?我可以使用bigdecimal打印PI吗?

how this is happenning ? and can i use bigdecimal to print PI ?

推荐答案

来自

PI

公共静态最终双PI

一个比pi更近的双精度值,即圆周长与其直径之比.

The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.

这大概是(当打印为十进制扩展名时):

This is roughly (when printed as decimal expansion):

3.141592653589793

这只是一个固定的64位序列,表示Pi的近似值,它是一个硬编码的常量,保存在标准库的JAR中.该64位序列 not 并不包含用于计算任意数量的Pi数字的算法.

It's just a fixed sequence of 64 bits that represents an approximation of Pi, it is a hard-coded constant that is saved in the JAR of the standard library. This 64-bit sequence does not contain an algorithm for computing an arbitrary number of Pi digits.

在第15位之后看到的数字或多或少是伪随机垃圾.将其包装为BigDecimal并不能使其更加精确.

The numbers that you see after the 15-th place are more or less pseudo-random garbage. Wrapping it into BigDecimal doesn't make it any more precise.

这篇关于使用BigDecimal打印Pi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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