File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Infinity" Watch "Infinity" New topic


Adam Smith

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?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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

Joined: Nov 15, 2003
Posts: 3
Ahhhh. That makes much more sense. Thanks for your help.
I agree. Here's the link:
subject: Infinity
It's not a secret anymore!