Pages

Saturday, May 8, 2010

What is 2.0-1.1? Any Idea

This is one funny behavior of the java compiler that I came across.
Create a class file with the following source code

import java.math.BigDecimal;

public class test {
    public static void main (String args[])    {
        System.out.println(2.0-1.1);
        System.out.println(new BigDecimal(2.0).subtract(new BigDecimal(1.1)));
        System.out.println(new BigDecimal("2.0").subtract(new BigDecimal("1.1")));
    }
}


compile the same and run it, and there you go, the funny results that I saw,

0.8999999999999999
0.899999999999999911182158029987476766109466552734375
0.9



I came across this in a session handled by Mr.Venkat Subramaniam in the Great Indian Developer Summit which was held on Banglore 20th to 28th April... (not so precise about the dates... :)

No comments:

Post a Comment