Unexpected Negative Numbers in Java

谁都会走 提交于 2021-01-27 12:11:48

问题


    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:


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.

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.

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;

And you'll need to use the add method.

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



回答2:


From the Java Language Specification section on integer operations:

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. 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

Use a biginteger datatype to declare your array.



来源:https://stackoverflow.com/questions/30064609/unexpected-negative-numbers-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!