This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Ininifty as the answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ininifty as the answer" Watch "Ininifty as the answer" New topic
Author

Ininifty as the answer

Logan Knecht
Greenhorn

Joined: Jun 05, 2008
Posts: 16
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.
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
You have initialise z=0


Hope this helps ...
Logan Knecht
Greenhorn

Joined: Jun 05, 2008
Posts: 16
Ahahahah oh wow, what a stupid mistake.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

I'd suggest turning x and N into ints. Check out this about some problems with floating point numbers.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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?


SCJP, SCWCD.
|Asking Good Questions|
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Googled and found the answer here
[ August 13, 2008: Message edited by: Amit Ghorpade ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ininifty as the answer
 
Similar Threads
Apply function names as parameters in other functions
Plot update error - JMathPlot
get confusion in constructor execution
Scanner class help
+ - operation