简体   繁体   English

Java中出现意外的负数

[英]Unexpected Negative Numbers in 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. 但是,出于某种原因,它在我的输出中间给出了负数。

I could use 我可以用

    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,-1036647147,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,-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

The Fibonacci numbers will grow large quite fast. Fibonacci数字将大幅增长。 At the 46th number, you start getting negative numbers, eg -1323752223 . 在第46个数字处,您开始得到负数,例如-1323752223 This is because the numbers have grown so large that it overflows the int datatype. 这是因为数字已经变得如此之大以至于它溢出了int数据类型。

You can use long[] arrays, but that will only postpone the problem. 你可以使用long[]数组,但这只会推迟问题。 You'll starting getting negative numbers at the 92nd number, eg -6246583658587674878 , because it will overflow the long datatype. 您将开始在第92个数字处获得负数,例如-6246583658587674878 ,因为它将溢出long数据类型。

Using double won't have the precision needed at this magnitude. 使用double将不具有如此大的精度。 You can use BigInteger s, which have arbitrary precision and magnitude. 您可以使用具有任意精度和幅度的BigInteger

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

And you'll need to use the add method. 你需要使用add方法。

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

From the Java Language Specification section on integer operations: 从整数运算的Java语言规范部分

The built-in integer operators do not indicate overflow or underflow in any way. 内置整数运算符不以任何方式指示溢出或下溢。 The results are specified by the language and independent of the JVM version: Integer.MAX_VALUE + 1 == Integer.MIN_VALUE and Integer.MIN_VALUE - 1 == Integer.MAX_VALUE. 结果由语言指定且独立于JVM版本:Integer.MAX_VALUE + 1 == Integer.MIN_VALUE和Integer.MIN_VALUE - 1 == Integer.MAX_VALUE。 The same goes for the other integer types. 其他整数类型也是如此。

If you do something like this: 如果您这样做:

int x = 2147483647;
x++;

If you now print out x, it will be the value -2147483648 如果现在打印出x,则值为-2147483648

Use a biginteger datatype to declare your array. 使用biginteger数据类型声明您的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM