++ increment of float inacurate with no compiles warnings?

Hi,

I have a simple class that uses the ++ opperator to increment a floting point variable. However the result is not correct and there are no compiler warnings about loss of precision. The class is....

class Test

{

publicstaticvoid main(String[] args)

{

float x = 1.123f;

x++;

System.out.println(x);

}

}

However the result of X is 2.1230001. X will now fail any if statements such as

if (x = 2.123)

{

// Do Something

}

So my question is

1) Why does this happen

2) Is it possible to use the ++ operator on a floating point variable with accuracy?

I'm studying for the SCJP and attention to details like this is everything!!

Thanks!

Alan K.

[1162 byte] By [alankilbridea] at [2007-11-27 5:48:40]
# 1

It has nothing to do with ++.

[url=http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2]SOME THINGS YOU SHOULD KNOW ABOUT FLOATING-POINT ARITHMETIC[/url]

[url=http://docs.sun.com/source/806-3568/ncg_goldberg.html]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]

Another good (slightly simpler) FP explanation:

http://mindprod.com/jgloss/floatingpoint.html

jverda at 2007-7-12 15:34:23 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks!!Ps: I found the last link to be of most help!
alankilbridea at 2007-7-12 15:34:23 > top of Java-index,Java Essentials,Java Programming...