#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# math accuracy

edited November 2014

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.

Tagged:

## Answers

• edited August 2014 Answer ✓

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...

• edited August 2014 Answer ✓

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` or `D`:

``````double d = 1875*16/67d;
println(d); // 447.76119402985074
exit();
``````

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.` or `StrictMath.`!

``````println(sqrt(2));      // 1.4142135
println(Math.sqrt(2)); // 1.4142135623730951
exit();
``````
• Thanks GoTo! That makes sense.

• Answer ✓

Easier reading than the semi-mandatory link KevinWorkman provided... :-) http://floating-point-gui.de/

• Very useful resource PhiLho! Thanks.

Sign In or Register to comment.