Re: Comparing floating point values in Java



Philipp wrote:
float a = 1;
float b = 1;

Side note: consider explicitly setting float variables to float constants rather than integer constants:

float a = 1.f;

This prevents conversion from being an issue.

Generally you can count on constants comparing equal if they represent the same value. Generally you cannot count on calculated values equaling differently-calculated values even were they mathematically equivalent given infinite precision.

It makes a difference what values you wish to represent. Not all values are precisely representable in limited-precision floating point.

Read
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>
available as a PDF from
<http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf>
among other places.

- Lew
.



Relevant Pages

  • Re: Power C
    ... Anssi Saari wrote: ... Maybe you should show an exact example of something that didn't work? ... the C64 anyway does not like integer constants> 65535 such as: ... float a,b; ...
    (comp.sys.cbm)
  • Re: what is wrong with the function" nanosleep"
    ... > float random; ... > int baseGap = 20000; ... I would use float constants instead of integer constants in the expression, ...
    (comp.unix.programmer)
  • Re: float problem
    ... > I have a problem with float variables's decimal side. ... As Doug already pointed out 4 and 5 both are integer constants, ... Bye, Jojo ...
    (comp.sys.tandem)
  • Re: Power C
    ... the C64 anyway does not like integer constants> 65535 such as: ... won't compile ... it won't handle integer constants any larger ...
    (comp.sys.cbm)