This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java subtraction issue...  RSS feed

 
travis woods
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does

System.err.println("test....= " + (.8 - 1.0));

return a -.1999999999996 instead of -.20?

and what should i be using to make it work correctly?
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Travis.

I am going to move this thread to a more appropriate forum.

This forum is for Servlets questions.

I am going to move this to the Java in General (Beginning)

Also the reason has to do with floating point arithmatic. Because you have decimal points it automatically uses either float or double.

This is a common problem that is not just Java related.

C++ has this issue too.

Mark
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Travis, Its called typecasting, try this:

System.err.println("test " + ((float)(.8 - 1.0)));

HTH

Gabe
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kinda shocking but digital computers are really pretty bad at arithmetic. Hard to believe? Find one where 1 / 3 * 3 == 1. You have to learn to know when to round or do fuzzy compares, like -.1999999999996 might be close enough to -.20 for all practical purposes.

COBOL defined decimal operations that handle money very intuitively and .8 - 1.0 would give you -.2 but only because it's really doing 80 - 100 = -20 under the covers. PowerBuilder made decimals that matched COBOL recognizing that mainframe compatibility might be important. Donno why Java didn't bother. Or does BigDecimal do the job?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!