wood burning stoves 2.0*
The moose likes Java in General and the fly likes Infinity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Infinity" Watch "Infinity" New topic
Author

Infinity

Adam Smith
Greenhorn

Joined: Nov 15, 2003
Posts: 3
The statement System.out.println(100.0 / 0) seems to result in infinity being printed. I am perfectly happy why, but would be interested in any technical explanation...
I would not expect the result of a primitive arithmetic operation to result in a String... Can anyone explain?
Cheers..adam
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Adam,
Welcome to JavaRanch!
The IEEE floating-point standard, which Java follows, allows for a couple of special values: NaN (Not A Number), +Inf and -Inf. They're not strings -- they're just specific bit-patterns within the double and float types. There's no way to represent them as a series of digits, because they're not valid numbers, either. The String representation (NaN or Inf or whatever) is created by whatever method does the printing -- i.e., the println() method calls String.valueOf(double) to produce something that it can render to the screen, and it's this method's job to decide how to render the value; it comes up with the strings "NaN" and "+Inf", etc. If you were to write your own method to format doubles, it could do something else for NaN, whatever you wanted it to do.


[Jess in Action][AskingGoodQuestions]
Adam Smith
Greenhorn

Joined: Nov 15, 2003
Posts: 3
Ahhhh. That makes much more sense. Thanks for your help.
 
Consider Paul's rocket mass heater.
 
subject: Infinity