• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

compilation error

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, here is a code i am testing...

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?
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you tring to do this? 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.
 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Try it like this
double f7 = -2147483655D;
 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The range for int is -2,147,483,648 to 2,147,483,647.
You can check the o/p by using
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic