• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Infinity

 
Adam Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhh. That makes much more sense. Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic