• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code Error

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Could anyone please point out the error in the code

for length =5
it gives checkbits= -6
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

You realise that the Math.pow() method returns a double, and you might suffer rounding errors from that cast. It is worthwhile putting in some print statements both to print out the power and also the values of j as you traverse the loop. That should help you work out what is going on.

I can't tell just by reading the code, since I don't know what your loop is supposed to do.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be Problem with STATIC methode calling..ie(Math.pow())Check it..
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are going backwards; j is going from 0 to -1 to -2 to ...
That means that Math.pow(2, j) will become smaller than 1 (but never smaller than 0) once j becomes smaller than 0, and the cast to int truncates it to 0. That means that the loop will break when length + 1 + j <= 0 which means when j == -6.

If you turn j-- into j++ the result is 4.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I read your post for a few times now, however I still can figure out what do you mean by an error.
Perhaps you should share with us a desired result?!

Arpit Panwar wrote:


You have an infinitive loop for positive length in here.

Arpit Panwar wrote:


Luckily for you, you have a condition which breaks the loop.

In case j is -6, above condition would look like:
  • length = 5 --> 5 + 1 - 6 = 0
  • pow(2, -6) = 0,015625 --> ((int) 0,015625) => 0

  • and since: 0 <= 0, the condition is fulfiled.

    And the number -6 is the first one in a row, which breaks the loop.

    I have to admit that this code snip looks very strange.
    I'm still not sure if this was done intentionally or it is perhaps a side effect of a bad day. ;)


    Regards,
    Rok
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic