【问题标题】:Loss of precision using arrays and long data types使用数组和长数据类型的精度损失
【发布时间】:2014-02-15 05:14:22
【问题描述】:

这是一个旨在使用 Buffons 针模拟估计 pi ​​值的程序。我在这里遇到的问题是,如果用户输入了一个非常大的数字,程序就会崩溃。我假设这是因为我没有使用长数据类型而不是 int。当我尝试将试验方法中的试验次数更改为长数据类型时,它一直给我一个错误,说数组的长度缺乏精度。我不知道如何解决这个问题.. 帮助?

import java.util.Scanner;
public class Darts
{
    public static long trials() //User Inputs number of trials
    {
        long input;
        Scanner in = new Scanner(System.in);

        System.out.print("How many darts/trials? ");
        input = in.nextLong();

        return input;
    }

    public static double [] randomX(long trial) // Randomly generates numbers for the x coordinate on the amounts of trials

    {
        double [] randNumArrayX = new double[trial];
        for(int i = 0; i < trial; i++)
        {
            randNumArrayX[i] = Math.random();
        }
        return randNumArrayX;
    }

    public static double [] randomY(long trial) // Randomly generates numbers forthe y coordinate on the amounts of trials
    {
        double [] randNumArrayY = new double[trial];
        for(int i = 0; i < trial; i++)
        {
            randNumArrayY[i] = Math.random();
        }
        return randNumArrayY;
    }

    public static int [] dartBoard(double [] randx, double [] randy) // determines whether the dark hit the board or not
    {
        int [] hitMiss = new int[randx.length];
        int [] trials = new int[randx.length];

        for(int i = 0; i < randx.length; i++)
        {
            if( Math.pow(randx[i] , 2) + Math.pow(randy[i] , 2) <= 1)
            {
                hitMiss[i] = 1;
            }
            else
            {
                hitMiss[i] = 0;
            }
        }
        return hitMiss;
    }

    public static double [] calcPi(int [] h) // Calculates pi using randomly generated numbers
    {
        int hitCounter = 0;
        double [] pi = new double[h.length];

        for(int i = 0; i < h.length; i++)
        {
            if(h[i] == 1)
            {
                hitCounter++;
            }
            pi[i] = 4*(hitCounter/(i + 1));
        }
        return pi;
    }

    public static void print(double [] pi) // prints results
    {
        for(int i = 0; i < 10; i++)
        {
            System.out.printf("%-7s%2d%-8s%-10.6f%n", "Trial [", i, "]: pi = ", pi[i]);
        }
        System.out.println("\t   .....");
        System.out.printf("%-17s%-10.6f%n", "Estimate of pi = ", pi[pi.length -1]);
    }

    public static void main(String [] args) // main method
    {
        long t = trials();
        double [] x = new double[t];
        double [] y = new double[t];
        int [] h = new int[t];
        double [] p = new double[t];
        x = randomX(t);
        y = randomY(t);
        h = dartBoard( x , y );
        p = calcPi( h );
        print(p);
    }
}

【问题讨论】:

  • 在 Java 中不能创建大于 20 亿的数组,因为数组的大小必须使用 int,而您只是将其更改为 long。
  • @Keppil 把这个交给javac(new int[2])[1L] = 1; 错误是“可能损失精度”。
  • 你不能将 long 值作为数组长度传递
  • @Marko: Eclipse 给了我另一个错误:Type mismatch: cannot convert from long to int.
  • @Keppil 您是否注意到 Eclipse 的错误消息与 javac 完全不同?而且更有用——这是一个很好的例子。

标签: java java.util.scanner long-integer type-conversion


【解决方案1】:

数组的大小必须是int、char或产生int或char结果的表达式

long t = trials();
    double [] x = new double[t];

这里你正在使用 long。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多