Hi,
I don't know exactly what's the problem but I have noticed that Processing is very bad making math operations!
For example,
double var = (1875*16)/67.0;
For Processing, var = 447.7611999511719, but the result is 447,76119402985075
I don't understand anything...
r.
Answers
That's because of floating point arithmetic.
Basically, these numbers are represented by binary numbers, which can't represent decimals exactly.
This is explained in the Processing API: http://www.processing.org/reference/float.html
As well as this article, which is required reading for every programmer: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Do you really need that amount of precision? If so, either try using doubles instead of floats, or look into the BigDecimal class: http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html
Thanks Kevin! I'll take a look...
Take notice that Processing's pre-processor suffixes all floating-point literals w/ an
f
!In order to counter that we gotta suffix them by ourselves w/ a
d
orD
:Also, Processing wraps up most Math & StrictMath utility methods to return a
float
.If you care about using
double
everywhere, prefix all of them w/Math.
orStrictMath.
!Thanks GoTo! That makes sense.
Easier reading than the semi-mandatory link KevinWorkman provided... :-) http://floating-point-gui.de/
Very useful resource PhiLho! Thanks.