It is not a good idea to use the == operator for doubles. If you really
need to do something like that you should use a tolerance, more or less
like this:

double tol = 0.000001;
if (Math.Abs(a - b - 0.1) > tol) {
         // etc.
}
At 03:15 PM 2/22/2000 +0800, you wrote:
>Sorry for posting this off-topic question here, but since people in this list
>have most probably coded extensively with Java, I'm hoping somebody can
>provide
>some insights.  In my java program,  an  expression like the following
>evaluates
>to false:
>     (0.7 - 0.6 == 0.1)
>
>The following code snipplet:
>      double a = 0.7;
>      double b = 0.6;
>      double c = a - b;
>      System.out.println("a=" + a + " b=" + b + " c=" + c);
>gives the result:
>                a=0.7 b=0.6 c=0.09999999999999998
>
>Why is the result of the above operation not exact?  If this is the expected
>behaviour, what would be a good way to handle such numeric operations and
>comparisons, e.g.  when we're doing data validation?
>
>___________________________________________________________________________
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to