gpt4 book ai didi

java - 为什么我的代码对于大数字给出不正确的结果?

转载 作者:行者123 更新时间:2023-12-02 05:23:04 24 4
gpt4 key购买 nike

我正在尝试解决这个任务:

In this exercise you will write a program that estimates the value of π. Pretend that we are looking at a dartboard in the picture above. If the square is 2units wide and 2 units high, the radius of the circle within the square is 1 unit. Then the area of the circle is π*r2 = π*12 = π. The area of the square will be 2*2 = 4.

The estimation of π will work by randomly “throwing darts” at the dartboard. We will assume that every dart will hit the board but the location of that strike will be random so some darts will land inside the circle and some will land outside the circle. The calculation of π is then the ratio of the number of darts that land inside the circle to the total number of darts, multiplied by 4. Note that as we increase the number of darts used in the simulation, the accuracy improves.

Follow the steps to complete the program:

  1. Declare variables that represent x, y coordinates of a dart, the distance of the dart from the origin, the number of darts that land inside the circle, and the total number of darts.

  2. Read in the total number of darts (N) from the user and create an instance of the Random class.

  3. Create a loop that runs N times. Inside the loop, you will use the nextFloat() method of the Random class to create x, y coordinates randomly, and then compute the distance of the dart from the origin. Determine whether the dart lands inside or outside of the circle.

  4. Compute π using the estimation algorithm.

  5. Run the program with increasing numbers from 100 to 100,000,000 and observe the accuracy of π.

这是我编写的程序。它在某种程度上有效,但对于像 100000 这样的输入,结果似乎不正确。任何人都可以解释我的代码有什么问题吗?

import java.util.Scanner;
import java.util.Random;

public class calculatePI {
public static void main(String[] args) {
double x;
double y;
double distance;
int i = 0;
float inside = 0;
float outside = 0;
float totalDarts;

Scanner scan = new Scanner(System.in);
Random random = new Random();

System.out.println("Enter the number of darts: ");
totalDarts = scan.nextFloat();

for (i = 1; i < totalDarts; i++) {
x = Math.abs(random.nextFloat() % 2);
y = Math.abs(random.nextFloat() % 2);

distance = (float) Math.sqrt(Math.pow(x - 1, 2)
+ Math.pow(y - 1, 2));

if (distance <= 1)
inside++;
else
outside++;
}

System.out.println("Total number of arrows inside circle:" + inside);
System.out.println("Estimated value of Pie: "
+ (((inside * 4) / (double) totalDarts)));

}

}

最佳答案

要解决您的问题,请更改类型

float inside = 0;
float outside = 0;
float totalDarts;

intlong

float 的问题是并非所有整数都可以用这种类型精确表示。 float 使用 24 位来存储数字(其余位用于存储符号和精度),这意味着 float 可以在不损失精度的情况下存储 int 值仅在 -1677721616777216 范围内。

您可以使用

观察这一点
System.out.println(16777216f);
System.out.println(16777217f);
System.out.println(16777218f);

打印

1.6777216E7
1.6777216E7
1.6777218E7

现在您看到16777217无法精确表示,将用16777216表示。这意味着 16777216f+1 仍将返回 16777216F,这会阻止您的 totalDarts 数量大于 16777216,这会导致更大的数字出现问题。

关于java - 为什么我的代码对于大数字给出不正确的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com