【问题标题】:Calculating the mass of the earth using a BigInteger使用 BigInteger 计算地球的质量
【发布时间】:2014-10-31 12:36:44
【问题描述】:

我正在尝试使用 BigInteger 计算地球的质量,但是从字面上看,有些东西并没有加起来。

我用来计算地球质量的公式是:M = ar2/G = 5.98 × 1024 kg。 a 是重力加速度(9.8 m/s 的平方),r 是地球的半径 ((6.4) *(10^6)),Big-G ((6.673) * (10^-11))引力常数。

我应该得到的值是:5.97219 × 10^24 kg = 5,972,190,000,000,000,000,000,000,但是我当前输出的值是:-9353。显然是一个巨大的差异,所以任何帮助将不胜感激。

这是我目前所拥有的......

double bigG = (6.673) * (10^-11);
double radiusSquared = 6371^2;
double acceleration = (10^2); (obviously rounded here from 9.8)
double mass = (acceleration*radiusSquared)/bigG;

BigInteger massBig = new BigDecimal(mass).toBigInteger();
System.out.println("mass of earth: "+massBig);'

我不确定我是否只是输入错误,或者我无法识别 Java 的某些底层过程,因为我对 Java 和物理都是新手。

谢谢


更新

好的,我按照你说的做了,似乎已经解决了部分问题...... 这是更新的代码:

public static void getPlanetMass(){
    double bigG = (6.673) * (Math.pow(10, -11));
    double radiusSquared =  (Math.pow(6371, 2));
    double acceleration =  (Math.pow(9.8,2)); //(obviously rounded here from 9.8)
    double mass = (acceleration*radiusSquared)/bigG;
    BigInteger massBig = new BigDecimal(mass).toBigInteger();
    System.out.println("mass of earth: "+massBig);
}

当前输出:58,417,939,781,807,300,608 正确输出:5,973,600,000,000,000,000,000,000

既然我在这里大约有一百万,我仍然缺少几个数量级,任何精通数学/物理的人都会在这里提供帮助。

【问题讨论】:

  • (1) 半径为6371公里,即6371000米。你少了几个零。 (2) 加速度不要平方;只有半径得到平方。 (1) 和 (2) 是错误。这是一个轻微的改进。 (3) 写 bigG = 6.673e-11(无需调用 Math.pow 来计算 10 的幂)。
  • 啊,我知道我忽略了某处与 10 的幂有关的事情,或者在本例中为 1000。谢谢
  • 哇!好像你找到了空心地球假说的证据!! ;) en.wikipedia.org/wiki/Hollow_Earth

标签: java math physics biginteger


【解决方案1】:

^不是幂,而是二进制XOR

如果在一个操作数中设置了二进制 XOR 运算符,则复制该位,但没有 两者都有。

你想要的

Math.pow(x, y);

【讨论】:

  • x 是整数,y 是指数,我认为是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
相关资源
最近更新 更多