Re: What is the behaviour of double / float primitive == comparison?
Carfield Yim <[email protected]>
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
> > The == comparison will return true if the actual bits are the same. This > will depend on how the calculations pan out: how will the numbers you're > going to divide be represented in binary, and how will the result of the > division between these two numbers be represented? > > Btw, half values happen to be perfect for binary to represent, since > decimal .5 is "0.1" in floating binary. Decimal 0.1 is, however, > impossible to represent in floating binary. > Thanks a lot :-) > Use BigDecimal. Or multiply away the fraction part and do your > calculations using integers. Exact real values (floating point!) are a > lie anyway! (Your desk isn't 2 meters across..) > I will... if this is my choice... however I am the one maintaining existing code and just like to understand if some critical function is using "==" comparison on double... > PS: don't use float in any case, use double. > > PPS: Even more hilarious is the effect of JIT on floating point > calculations: at the compilation point, you suddenly get the precision > of the native (math co-)processor instead of the interpreter. This might > be higher than what the IEEE standard defines (and the interpreter > gives), apparently in particular regarding the exponent. The 'strictfp' > keyword can fix this particular issue, so that you get just as bad > accuracy when it is compiled - this to ensure perfect portability across > platforms. > This is very useful, in fact I have read the equals() method of java.lang.Double but I don't know if double primitive do this samething. =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com