java long变量表现得像int变量

时间:2015-12-10 13:49:37

标签: java

我认为“长”类型变量的行为类似于“int”类型变量。我觉得情况就是这样,因为随着变量越来越大,当它试图超过2147483647(这是“int”类型的最大值)时,它实际翻转到负数。显然,“长”类型的最大值更容易适应。

我想我应该在这里粘贴我的代码。请不要苛刻代码,它根本没有被重构。此外,您将看到许多蹩脚的尝试强制代码将我的变量视为“长”类型,但这些努力的总体并不成功。

注意,此代码处理“N选择X”或组合。输入是组合的数量和N.代码循环通过X的可能值,直到它找到匹配或直到它超过匹配的可能性(或直到计算的组合“变为负”。)

提前感谢您的帮助。

public class primativeLongPractice {

    private static long fctrl (long num) {
          long ans = 1L;
          for (long i=num; i>0; i--) ans = ans * i;
          return ans;
        }

    private static long nchoosex (long n, long x) {
          long y = n - x;
          if (y>x) {
            long temp = y;
            y=x;
            x=temp;
          }
          long ans = 1L;
          for (long i=n; i>x; i--) ans = ans * i;
          return ans/fctrl(y);
        }

    public static long checkchoose(long m, int n) {
          long N = (long)n;
          long combos = 0L;
          long x = 1L; // starting out at 1 and going up
            // compute "n choose x" call it combos
            combos = nchoosex(N,x);
            System.out.println(n + " choose " + x + " equals " + combos + "; m equals " + m);
            if (combos==m) return x;
            while ((combos>1)&&(combos<m)) {
              x = x + 1;
              combos = nchoosex(N,x);
              System.out.println(n + " choose " + x + " equals " + combos + "; m equals " + m);
              if (combos==m) return x;
            }
          System.out.println("Didn't find anything");
          return -1L;
        }

    public static void main(String[] args) {
        long p = 155117520L;
        int q = 30;
        long r = checkchoose(p,q);
        System.out.println("For inputs " + q + " and " + p + " the function returned " + r);
    }
}

2 个答案:

答案 0 :(得分:2)

我快速调试了它。

对于n = 30, x = 14,您的值ans有一个长溢出并产生值

ans = -5769043765476591616

fctrl(y) = 87178291200

这使得似乎就像结果因整数溢出而滚动。

答案 1 :(得分:0)

long确实比int更容易接受,但它仍然会溢出。

我在代码中添加了一些额外的打印来显示问题:

private static long fctrl(long num) {
    long ans = 1L;
    for (long i = num; i > 0; i--) {
        // Are we going tyo overflow?
        if (ans * i < 0) {
            System.out.println("fctrl overflow!!! " + ans + " at " + i);
        }
        ans = ans * i;
    }
    return ans;
}

private static long nchoosex(long n, long x) {
    long y = n - x;
    if (y > x) {
        long temp = y;
        y = x;
        x = temp;
    }
    long ans = 1L;
    for (long i = n; i > x; i--) {
        if (ans * i < 0) {
            System.out.println("nchoosex overflow!!! " + ans + " at " + i);
        }
        ans = ans * i;
    }
    return ans / fctrl(y);
}

public static long checkchoose(long m, int n) {
    long ln = (long) n;
    long combos = 0L;
    long x = 1L; // starting out at 1 and going up
    // compute "n choose x" call it combos
    combos = nchoosex(ln, x);
    System.out.println(n + " choose " + x + " equals " + combos + "; m equals " + m);
    if (combos == m) {
        return x;
    }
    while ((combos > 1) && (combos < m)) {
        x = x + 1;
        combos = nchoosex(ln, x);
        System.out.println(n + " choose " + x + " equals " + combos + "; m equals " + m);
        if (combos == m) {
            return x;
        }
    }
    System.out.println("Didn't find anything");
    return -1L;
}

public void test() {
    System.out.println("Hello");
    long p = 155117520L;
    int q = 30;
    long r = checkchoose(p, q);
    System.out.println("For inputs " + q + " and " + p + " the function returned " + r);
}

打印

  

nchoosex溢出!!! 7月745747076954880000