• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logical error in my Program  RSS feed

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written this code for a method that take an integer number and return the sum of a series.
this is the formula of the series
m(i)=4(1- 1/3 + 1/5 -1/7 + 1/9 - 1/11 + 1/13 - ................. - 1/(2i-1) + 1/(2i+1))
and this is the code

The problem is that the program take too many time to run and doesn't give true result.
i couldn't trace the error, and i hope helping me.
thanks in advance.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've defined the upper limit of your loop in terms of "i", but of course "i" changes with every loop iteration. I think you meant to use "n" there, yes?
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see two things which don't look right to me.

1. You pass an int value ("n") to the computeSeries method, but you don't use it anywhere in that method.

2. You have a for-loop which I don't think is ever going to terminate. Look at your termination condition:

For what value of i do you expect that to be false?
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your for loop will go on forever because for all positive i, i is always going to be less than 2i + 1.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot My friends Ernest Friedman-Hill, Paul Clapham, and Luigi Plinge
you really help me, and i will take more attention to avoid Falling through such errors.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely it will terminate when i equals Integer.MIN_VALUE / 2
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a double so that isn't going to work.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:It's a double so that isn't going to work.
Aaaargh! I didn't notice that. Even Double.MIN_VALUE / 2 won't work because you reach a value where the precision is < 2, so d + 2 == d to the JVM, and the value will remain the same for ever.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That ought to remind us always to use integer arithmetic for the counters in loop.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!