Java中意外的负数 [英] Unexpected Negative Numbers in Java

查看:101
本文介绍了Java中意外的负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    import java.util.*;
    public class Prac9FibonacciNumbers {

public static void main(String[] args) {

    int[] x = new int[100];
    x[0] = 1;
    x[1] = 1;

    for (int a = 2; a < 100; a++) {

        x[a] = x[a - 1] + x[a - 2];

    }

    for (int a = 0; a < 100; a++) {

        if(a < 99){

            System.out.print(x[a] + ",");

        }
                else{

                System.out.print(x[a]);

                }

            }

        }

    }

该程序用于创建斐波那契数字的列表.但是,由于某种原因,它在输出中间给了我负数.

This program is meant to create a list of Fibonacci numbers. However, for some reason, it is giving me negative numbers right in the middle of my output.

我可以使用

    Math.abs()

但是我想知道为什么它给了我负数.输出在下面.请帮助我理解这个问题.

but I want to know why it is giving me negative numbers. The output is down below. Please help me with understanding this problem.

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170,1836311903,-1323752223,512559680,-811192543,-298632863,-1109825406,-1408458269,1776683621,368225352,2144908973,-1781832971,363076002,-1418756969,-1055680967,1820529360,764848393,-1709589543,-944741150,1640636603,695895453,-1958435240,-1262539787,1073992269,-188547518,885444751,696897233,1582341984-2015728079,-433386095,1845853122,1412467027,-103664714​​7,375819880,-660827267,-285007387,-945834654,-1230842041,2118290601,887448560,-1289228135,-401779575,-1691007710,-2092787285,511172301,-1581614984,-1070442683,1642909629,572466946,-2079590721,-1507123775,708252800,-798870975,-90618175,-889489150,-980107325

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170,1836311903,-1323752223,512559680,-811192543,-298632863,-1109825406,-1408458269,1776683621,368225352,2144908973,-1781832971,363076002,-1418756969,-1055680967,1820529360,764848393,-1709589543,-944741150,1640636603,695895453,-1958435240,-1262539787,1073992269,-188547518,885444751,696897233,1582341984,-2015728079,-433386095,1845853122,1412467027,-1036647147,375819880,-660827267,-285007387,-945834654,-1230842041,2118290601,887448560,-1289228135,-401779575,-1691007710,-2092787285,511172301,-1581614984,-1070442683,1642909629,572466946,-2079590721,-1507123775,708252800,-798870975,-90618175,-889489150,-980107325

推荐答案

斐波纳契数将快速增长.在第46个数字时,您开始得到负数,例如 -1323752223 .这是因为数字已经变得很大,以至于溢出了 int 数据类型.

The Fibonacci numbers will grow large quite fast. At the 46th number, you start getting negative numbers, e.g. -1323752223. This is because the numbers have grown so large that it overflows the int datatype.

您可以使用 long [] 数组,但这只会推迟问题.您将在第92个数字处开始获得负数,例如 -6246583658587674878 ,因为它将溢出 long 数据类型.

You can use long[] arrays, but that will only postpone the problem. You'll starting getting negative numbers at the 92nd number, e.g. -6246583658587674878, because it will overflow the long datatype.

使用 double 不会达到此级别所需的精度.您可以使用具有任意精度和大小的 BigInteger .

Using double won't have the precision needed at this magnitude. You can use BigIntegers, which have arbitrary precision and magnitude.

BigInteger[] x = new BigInteger[100];
x[0] = BigInteger.ONE;
x[1] = BigInteger.ONE;

您将需要使用 add 方法.

x[a] = x[a - 1].add(x[a - 2]);

这篇关于Java中意外的负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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