>>>>> Alex M writes:

    Alex> That is actually documented I believe.  It's not limited to
    Alex> jdk1.2.  I don't know why it can't be just 0.3000000000000
    Alex> but it is.  This is why the language spec says not to
    Alex> compare floats and doubles without casting, you will always
    Alex> get negative results even though you think you should have a
    Alex> positive one.

    Alex> So to compare 0.3d with 0.3f you would do:

    Alex> double d = 0.3;
    Alex> float f = 0.3f;

    Alex> if ((float)d == f) { do something };

Don't use '==' to compare floating point values, instead compare 
their difference against a small value:

    double epsilon = 0.0001;
    double a, b;
    ...
    if (a - b < epsilon) ...

    Alex> You will lose precicion on your double variable though, so
    Alex> make sure you don't need it before you do it.

    Alex> Back to why you can't store 0.3 as a double.  I'm guess this
    Alex> has more to do with the limitations of binary representation
    Alex> of floating point numbers.  It's been a long time since my
    Alex> intro to CS courses when I learned all this, anybody have
    Alex> anymore info on this?

See http://www.validgh.com/goldberg/paper.ps (What Every Computer
Scientist Should Know About Floating-Point Arithmetic')


        Juergen

-- 
Juergen Kreileder, Blackdown Java-Linux Porting Team
http://www.blackdown.org/java-linux.html


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to