Link Details

Link 651053 thumbnail
User 772423 avatar

By shekhargulati
via whyjava.wordpress.com
Published: Aug 14 2011 / 10:47

Couple of days back I wrote a piece of code which was behaving in an unexpected manner. I was confused what was happening. Take a look at the sample code below and predict its behavior
  • 7
  • 10
  • 3768
  • 5

Comments

Add your comment
User 902267 avatar

javakata replied ago:

1 votes Vote down Vote up Reply


System.out.println(0.9999999999999f==1.0f);//true: float has bad precision
System.out.println(0.999999999999==1.0);//false: by default is double, better precision
System.out.println(new BigDecimal("0.9999999999999").equals(new BigDecimal("1.0")));//false: even better precision than double


This is stated right from the start in the java docs and at the very beginning of the Java Technical Docs.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Debugging JavaScript
Written by: Ashutosh Sharma
Featured Refcardz: Top Refcardz:
  1. Design Patterns
  2. OO JS
  3. Cont. Delivery
  4. Java EE7
  5. HTML5 Mobile
  1. Java EE7
  2. Spring Annotations
  3. Git
  4. Java
  5. REST