Some floating point numbers (float/double) cannot be represented with infinite precision. This is very common in all languages. That's why one has to be sure when to use floats or doubles instead of int. Try to execute the following code: <PRE> double d = 1/3; if(d*3 == 1.0) System.out.println("Equal"); else System.out.println("Not Equal"); </PRE>
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
You can also use the BigDecimal class... Rounding or using the DecimalFormat class may be able to get you what you want, but is't really recommended in many situations. The fact of the matter is that floating point numbers are really meant for scientific calculations and not for monetary (or similar) calculations... and this exact same behavior occurs on ANY development platform that uses floating point numbers, including C/C++. BigDecimal is Sun's answer to this problem... although rolling your own class using longs or integers isn't terribly difficult. BigDecimal is also being enhanced/replaced by IBM's more fully functional class as well, although I don't know all the details... check the JCP.
<B>Rich Wardwell</B><BR> <A HREF="mailto:firstname.lastname@example.org" rel="nofollow">email@example.com</A> <BR>Sun Certified Programmer for the Java 2 Platform
This is from one of the cattle drive assignments. The purpose of this particular assignment is to learn the boundaries of double. In a previous assignment the student learned the boundaries of int and long. The important thing is that as you add all these imprecisions up, they turn into bigger inprecisions. Finally, if you have a value that is approximately 100.0, you cannot test for equality. Instead, you will need to test if the value you have is in the range of, say, 99.9999 and 100.0001. Several classes and methods within the java core library will help you with the rounding you may need to do.