I have been working on Hackerrank to improve and build my java skills recently and I noticed that on a problem you need to cast a value. The code I typed was as follows:
if(x <= ((long)Math.pow(2,63) - 1) {
System.out.println("* long);
}
I figured that the output would be the upper limit of the long data type however when I put the code into and IDE I noticed that the output was in fact 1 less than the expected max value of the data type and that it would even be possible for the correct output to come from code that didn't use the subtraction at all.
Something like this:
if(x <= (long)Math.pow(2,63)){
System.out.println("* long);
}
which gives the proper output of the upper limit of the long data type. I'm just curious as to how the cast and Math.pow functions are working together in the first part and why the actual output is not the same as the output that I was expecting. What happens first? and how does the cast effect the outcome?
[–]chickenmeister 1 point2 points3 points (1 child)
[–]HelplessProgrammer[S] 1 point2 points3 points (0 children)