So, from what I understand, processing functions aren’t built to handle double floating points. Are there any external libraries I can download that can overload functions so they can handle doubles? Or is there no real way to that without reinventing the way the system does math?
Suffix all floating literals w/ d
or D
as these examples:
.125d, 2.302585092994046d, 1e-3d, etc.
Replace Processing’s math functions w/ Java’s Math 1s:
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html
Math.cos(), Math.max(), Math.random(), etc.
I don’t really understand. Could you please show what this looks like in code form? Like, for example, how would I write a function that receives a double as an input and println()s e to the power of that double?
How do I download that library?
We just use it like this: println(Math.exp(100)); // 2.6881171418161356E43
Okay, I can see what you’re talking about. For some reason, I just assumed Math was in a separate library. But, I have another question: How do you convert a double into a string? Because, I need to output doubles not only to the println console, but also as text to the drawing console.
String ns = "" + Math.exp(100);
And what about doubleLists? Is there any way in Processing to make a list with doubles as components?
Processing.org/reference/ArrayList.html
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Double.html
import java.util.List;
final List<Double> doubles = new ArrayList<Double>();
You probably know that Processing was built on top of Java.
Now Java has two floating point data types float
and double
and in all fp maths Java uses the double
data type, for instance the class java java.lang.Math
provides most of the mathematical functions you are ever likely to want. Processing provides the same functions but they accept float parameters and return floats.
So to calculate e^100
either of these would work
float f = exp(100);
double d = Math.exp(100);
So if you are serious about using doubles in Processing then you must explicitly code numbers as doubles for instance if we run this code
double f = 22.0 / 7.0;
double d = 22.0d / 7.0d;
println(f + " " + d);
gives the following output
3.142857074737549 3.142857142857143
notice they are different because the division in the first line is dividing 2 floats but in the second it’s dividing 2 doubles.
try this code
double f = exp(100);
double d = Math.exp(100);
println(f + " " + d);
produces
Infinity 2.6881171418161356E43
because floats can’t hold the same range of numbers as doubles.
So what I am saying is that if you want to use doubles then you need to avoid Processing’s data structures and use Java directly as shown in @GotoLoop’s post.
Welp, I’m afraid I kinda need to do both. What I’m doing requires that I graph equations, and might even require domain coloring (assigning a color to a set of coordinates (x,y) corresponding to a function f(x+y*i), which, even in processing without doubles, is very CPU intensive).
You can always perform the calculations as doubles but then cast them to floats when needed e.g.
double radius = 123.98735d;
double area = Math.PI * radius * radius;
float area_f = (float) area;
BTW you might look at the graphica library if you are interested in plotting graphs
Thanks! That solved my issue as well. One remains: the nf() function does not accept nf(double, int, int). It can be done by casting such as :
double x= 3.14159565358979d;
String strX= nf( (float) x, 1,9);
but that defeats the precision of double. Is there a work-around?
I guess you may try out String.format():
- Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)
- Formatter (Java SE 11 & JDK 11 )
final String DOUBLE_FORMATTER = "%.5f"; // 5 decimal digits
println("PI:", String.format(DOUBLE_FORMATTER, Math.PI)); // 3.14159
println("E: ", String.format(DOUBLE_FORMATTER, Math.E)); // 2.71828
exit();
That is a consequence of Processing treating all floating point numbers as floats
Yes. That works perfectly. THANKS!!!
Wanting to preserve the way nf() works, extending it to double, here is my
nf (double, int, int ) function. The normal nf() remains working for types other than double.
void setup(){
println( nf(Math.PI,1,16) );
}
//--------- nf(double, int, int ) -------------------------------
String nf(double x, int i, int j ){
String DOUBLE_FORMATTER = "%"+i+"."+j+"f";
String XX= String.format(DOUBLE_FORMATTER, x);
return XX;
}
I ended up being able to cast a string to a double with new Double(). There are also 2 other methods for doing this, including Double.valueOf(string), and Double.parseDouble(string).
Great help! “My quiver is full of Double arrows now” I can fully manipulate Double class. The java link to all the Math. functions was a big help.