>From any programming language (with the exception perhaps of BASIC) this or something
>similar will occur. To re-write your test
( (0.7 - 0.6) - 0.1 ) < 1E-4 (an appropriately small number)
should do the trick.
Decimal numbers in most programming languages are represented imprecisely. Some
languages have the facility to represent decimal numbers precisely using a special
base 10 data type. This may be useful if you want to do calculations involving money.
Otherwise the additional overhead (processor and memory) to do this is not normally
warranted.
kev
> ----------
> From: Tsao Su Jen
> Reply To: A mailing list for discussion about Sun Microsystem's Java Servlet API
>Technology.
> Sent: Tuesday, 22 February 2000 6:15 PM
> To: [EMAIL PROTECTED]
> Subject: Basic arithmetic operation in Java (0.7 - 0.6 != 0.1 ???)
>
> 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
>
>
>
EOM
NOTICE - This message contains information intended only for the use of the addressee
named above. It may also be confidential and/or privileged. If you are not the
intended recipient of this message you are hereby notified that you must not
disseminate, copy or take any action in reliance on it. If you have received this
message in error please notify [EMAIL PROTECTED]
___________________________________________________________________________
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