For a problem in HackerRank Java Datatypes (to find in which primitive datatype the given number can be fitted in) when I submit the following code :
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x>=(long)-128 && x<=(long)127)System.out.println("* byte");
if(x>=(long)Short.MIN_VALUE && x<=(long)Short.MAX_VALUE)System.out.println("* short");
if(x>=(long)Integer.MIN_VALUE && x<=(long)Integer.MAX_VALUE)System.out.println("* int");
if(x>=(long)Long.MIN_VALUE && x<=(long)Long.MAX_VALUE)System.out.println("* long");
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}
}
it passes all the test cases. But when i submit the following code :
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x>=(long)-128 && x<=(long)127)System.out.println("* byte");
if(x>=-1*(long)Math.pow(2,15) && x<=(long)Math.pow(2,15)-1)System.out.println("* short");
if(x>=-1*(long)Math.pow(2,31) && x<=(long)Math.pow(2,31)-1)System.out.println("* int");
if(x>=-1*(long)Math.pow(2,63) && x<=(long)Math.pow(2,63)-1)System.out.println("* long");
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}
}
it fails some test cases.
This is one of the testcases that do not pass (downloaded from HackerRank)
17 9223372036854775808 9223372036854775807 -9223372036854775808 -9223372036854775807 4294967296 4294967295 -4294967296 -4294967295 65536 65535 -65536 -65535 256 255 -256 -255 12222222222222222222222222222222222222222221
Why so? Has it anything to do with the return type of Math.pow()? Any help is appreciated. :)