• Post Reply Bookmark Topic Watch Topic
  • New Topic

The way Java deals with numeric calculations  RSS feed

 
Momen Travolta
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can I know how Java deals with 2nd line and 4th line?

I expected this result as something similar is mentioned in the book; but I would like to know -if could be- (1) due to what the answer is this, and (2) why doesn't the same result appear in C programs.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case 2 is actually not java specific, but an issue of the IEEE floating point standard itself. Certain values that seem that they should represent very well, just don't.

And what is wrong with case 4?

Henry
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at FAQ no 20.
 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Momen Travolta wrote:why doesn't the same result appear in C programs.


They do, unless they're hidden by rounding.

This problem is not programming language specific, it's fundamental to number systems: Using a finite representation, not every number in one number system can be exactly represented in another number system. The conversion errors you've noted are a result of that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!