Hi, I am trying to make a program where i need to calculate a fraction (3-2)/2 or just 1/2 which is 0,5 ofcourse, however processing says it is 0. I don’t know if it’s a bug or what. I would just like to know how i can fix the problem .
For exampel if i write something like:
float x=1/2
println(x)
it just prints 0
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.