Win a copy of High Performance Python for Data Analytics this week in the Python 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Ininifty as the answer

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made this program to divide by N for each iteration of the while loop, and in doing so it would take the sum of every iteration and print it to the line, the only problem is that the answer I receive is this.

Sum is: Infinity





If anyone knows why I'd love to know. Also, if anyone has any input on how to do this better too, I would love to know.

Thanks.
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have initialise z=0


Hope this helps ...
 
Logan Knecht
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahahahah oh wow, what a stupid mistake.
 
Sheriff
Posts: 22058
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest turning x and N into ints. Check out this about some problems with floating point numbers.
 
Marshal
Posts: 71774
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
I'd suggest turning x and N into ints.

But then make sure you are never dividing by 0!
 
Rob Spoor
Sheriff
Posts: 22058
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only division here is 1.0 by z, and z is already an int.

Or do you mean this as a warning for future references that any division with no floats or doubles will throw a ArithmeticException?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie
But then make sure you are never dividing by 0!



Yes that will lead to an exception.
But that brings up an question, why separate behavior for int and double?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Googled and found the answer here
[ August 13, 2008: Message edited by: Amit Ghorpade ]
 
Campbell Ritchie
Marshal
Posts: 71774
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
The only division here is 1.0 by z, and z is already an int.

Or do you mean this as a warning for future references that any division with no floats or doubles will throw a ArithmeticException?

Sorry for delay: yes, I meant a warning against the ArithmeticException.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic