Problems with fractions

I f you want to do “proper” rational arithmetic, I think ApacheMath supports it for java. Some languages like ruby have support out of the box:-

half = Rational(1, 2)
puts half.to_s
calculated = Rational(2 - 1, 2)
puts calculated.to_s
quarter = Rational(1, 4)
puts (quarter + half).to_s

Output

1/2
1/2
3/4

The problem with float arithmetic there are always errors, which are worse for small numbers…

In your case you probably want


float x = 1 / 2.0 // that forces float answer from processing (or double answer if using regular java)

And accept the errors for now.