Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Logical error in my Program

 
Ranch Hand
Posts: 82
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 26966
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely it will terminate when i equals Integer.MIN_VALUE / 2
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a double so that isn't going to work.
 
Campbell Ritchie
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That ought to remind us always to use integer arithmetic for the counters in loop.
 
reply
    Bookmark Topic Watch Topic
  • New Topic