简体   繁体   English

试图在Java中找到长数据类型的范围

[英]trying to find range of long data type in java

trying to verify range of long in java.. why is this program not working? 试图在Java中验证long的范围。为什么此程序无法正常工作? it gives infinite loop (probably). 它给出了无限循环(可能)。 i'm not getting output at the command line and the cursor keeps blinking. 我没有在命令行上得到输出,并且光标一直闪烁。

class range
{
public static void main(String [] args)
{
         long i;
     for(i=0;;i++)
    {
         long c= (long)(i+1);
         if(i>c)    //upper_limit+1=lower_limit
            break;  // hence, at i=upper_limit c(=i+1) < i

    }
    System.out.println("upper ="+i);
    for(i=-1;;i--)
    {
        long c=(long)(i-1);         
        if(i<c)             //lowerlimit-1=upperlimit
            break;          //hence at i=lowerlimit c(=i-1)>i
    }
    System.out.println("lower ="+i);
}
}

Your program will work eventually - but it's got to perform 2 63 iterations (in each direction) first. 您的程序最终将可以工作-但必须先执行2 63次迭代(在每个方向上)。 That's going to take a very long time. 这将花费很长时间。 Long.MAX_VALUE and Long.MIN_VALUE are rather simpler ways of finding this out. Long.MAX_VALUELong.MIN_VALUE是找出问题的简单方法。 Or just look at JLS 4.2.1 : 或者只是看一下JLS 4.2.1

The values of the integral types are integers in the following ranges: 整数类型的值是以下范围内的整数:

  • ... ...

  • For long , from -9223372036854775808 to 9223372036854775807, inclusive long ,从-9223372036854775808到9223372036854775807(含)

If you want to see your program finding the right values, just start it closer to the final result: 如果您想让程序找到正确的值,只需将其启动到更接近最终结果的位置即可:

for(i=Long.MAX_VALUE - 10;;i++)
...
for(i=Long.MIN_VALUE + 10;;i--)

That quickly ends with output of: 这样很快就结束了:

upper =9223372036854775807
lower =-9223372036854775808

Why do you think your program is not correct? 您为什么认为您的程序不正确? It simply will take a long while - until long overflows(about 2^63 iterations). 这将花费很长时间-直到长时间溢出(大约2 ^ 63次迭代)。 I suggest that you use Long.MIN_VALUE and Long.MIN_VALUE or if you want to avoid this solution use binary search for when overflow happens. 我建议您使用Long.MIN_VALUELong.MIN_VALUE或者如果要避免此解决方案,请在发生溢出时使用二进制搜索

Also you may consider using the same program for short to verify it is correct, simply very time consuming. 另外,您可能会考虑使用相同的程序short地验证其正确性,这非常耗时。

You can use Long.MAX_VALUE and Long.MIN_VALUE 您可以使用Long.MAX_VALUE和Long.MIN_VALUE

It will take lots of time to enumerate all 2^64 possible long values 枚举所有2 ^ 64个可能的长值将花费大量时间

There is no reason to start at 0 or -1. 没有理由从0或-1开始。

If you want to verify MIN/Max values, try it like this: 如果要验证MIN / Max值,请尝试如下操作:

class range
{
public static void main(String [] args)
{
         long i;
     for(i=Long.MAX_VALUE;;i++)
    {
         long c= (long)(i+1);
         if(i>c)    //upper_limit+1=lower_limit
            break;  // hence, at i=upper_limit c(=i+1) < i

    }
    System.out.println("upper ="+i);
    for(i=Long.MIN_VALUE;;i--)
    {
        long c=(long)(i-1);         
        if(i<c)             //lowerlimit-1=upperlimit
            break;          //hence at i=lowerlimit c(=i-1)>i
    }
    System.out.println("lower ="+i);
}
}

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

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