AngelRF
September 2, 2019, 12:01pm
1
I’m doing a Numeric Differentiation calculating program, and the second and third derivative get waay too noisy, according to wikipedia, this is caused by floating-point arithmetic rounding. https://en.wikipedia.org/wiki/Numerical_differentiation
Wikipedia says that noise can be reduced by increasing floating-point datatype precision.
I’m already using double, but in Processing reference there isn’t a preciser datatype than double.
Is there any possible way to get more precise datatype?
Google java BigDecimal, it’s a class and a pain to work with (compared to how we would do it normally), but it should do the job. There is also BigInteger.
Keep in mind they are both slow.
1 Like
kll
September 2, 2019, 12:38pm
3
i play here, but was just too slow for @clankill3r
import java.math.BigDecimal;
import java.math.RoundingMode;
// https://docs.oracle.com/javase/8/docs/api/java/math/class-use/BigDecimal.html#java.math
// https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
float af =2;
float bf =3;
float resultf;
double ad=2d;
double bd=3d;
double resultd;
BigDecimal abd = BigDecimal.valueOf(ad);
BigDecimal bbd = BigDecimal.valueOf(bd);
BigDecimal resultb= new BigDecimal("0");
resultf = af/bf;
resultd = ad/bd;
resultb = abd.divide(bbd,20, RoundingMode.HALF_EVEN);
println("resultf "+resultf);
println("resultd "+resultd);
println("resultb "+resultb);
Make sure to use constants & methods from Math class only and all non-integer numerical literals are suffixed w/ d
:
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html
TAU
→ 2 * Math.PI
cos(PI)
→ Math.cos(Math.PI)
-5.4
→ -5.4d
1e3
→ 1e3d
4 Likes
AngelRF
September 2, 2019, 2:40pm
5
So much thanks to you guys , but may be I will have to do serious optimization