i am getting compilation error.. javac test.java test.java:8: cannot assign a value to final variable MIN_VALUE Integer.MIN_VALUE = -2147483648 ; ^ test.java:9: cannot assign a value to final variable MAX_VALUE Integer.MAX_VALUE = 2147483647 ; ^ 2 errors how can i remove compilation error? what changes i have to do?
I guess you are new to Java. final variables are those which cannot be re-assigned any value once a value has been assigned to it. It is better you use another variable. You have to use a new variable for that. Shekar.
Integer.MIN_VALUE already has this value assigned to it - defined by Java. You should be able to just remove those two lines entirely, and it should be fine.
yes .... t removed those lines and it worked. my output: Round f4 is -2147483648 Round f5 is 2147483647 Round f7 is -2147483648 why Round f7 is -2147483648 ?? given float f7 = -2147483655f, so the answer should be -2147483655 like others (bcoz nothing to round off...its whole number). but why the last output is -2147483648 ?
If you look at the method signatures for Math.round(), the one that accepts a float as an argument returns an int value (the nearest int), hence -2147483648
hi, its not clear to me , how you are calculating this output ? float f7 = -2147483655f; see , the last two digit of f7 ..its 55..ok.The output i have got having last two digit 48....clearly these are not nearest integer. so, i think your explanation is not correct. one more thing to note,float f7 = -2147483655f; ....it has no decimal point at all...so how can i think of rounding off? anyway, i am not clear about the third output...will you be explicit more? can you tell me how are you calculating the third output in a simple way. thanks
The problem with float is that it is stored in 32 bits, a mantissa of about 24 bits and an exponent of 8 bits. They are both signed but because they have been normalized (another big topic) you get one more bit accuracy in your mantissa. However, that is the binary world. In the equivalent decimal world I think 25 bits equates to round about 32 million, so your number's magnitude is at best accurate to about 7 digits. If you want more accuracy you need to work in doubles and longs.
The nice thing about Standards is that there are so many to choose from!
so your number's magnitude is at best accurate to about 7 digits.
do you mean the output i am getting is a kind of garbage ? if it is not a garbage then can you tell me the mathematics , how i am getting that? i simply want to know the math...can you show me how that calculation is going on to produce that output? is it too difficult?
As pointed out, the Math.round() method returns an int. Since the smallest integer value possible is -2147483648, this is the output it gives for any number that is smaller than this. In other words, an int variable cannot store the number -2147483655f. If you want to print out this value, don't use Math.round().
hi, its not clear to me , how you are calculating this output ? float f7 = -2147483655f; see , the last two digit of f7 ..its 55..ok.The output i have got having last two digit 48....clearly these are not nearest integer. so, i think your explanation is not correct.
My explanation is correct, perhaps not complete. Here are the relevant parts from the API (from the Math class) static long round(double a) Returns the closest long to the argument. static int round(float a) Returns the closest int to the argument. The difference is the method that accepts a double returns a long, so, the number ending in 55 fits into a long, whereas it doesn't fit into an int when the same number is 'typed' as a float.
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking