# Is there a library that allows for the convenient use of doubles?

**URL:** <https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532>\
**Category:** Beginners\
**Created:** [April 8, 2020, 7:49am UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532 "2020-04-08T07:49:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)\
**Post date:** [April 8, 2020, 7:49am UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/1 "2020-04-08T07:49:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 8, 2020, 12:02pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/2 "2020-04-08T12:02:49Z")

</div>

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](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html)  
**Math.cos()**, **Math.max()**, **Math.random()**, etc.

---

<div class="post-metadata">

**Author:** ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)\
**Post date:** [April 8, 2020, 2:25pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/3 "2020-04-08T14:25:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 8, 2020, 2:29pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/4 "2020-04-08T14:29:12Z")

</div>

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#exp(double)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#exp(double))

---

<div class="post-metadata">

**Author:** ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)\
**Post date:** [April 8, 2020, 2:36pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/5 "2020-04-08T14:36:12Z")

</div>

How do I download that library?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 8, 2020, 3:21pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/6 "2020-04-08T15:21:56Z")

</div>

We just use it like this: `println(Math.exp(100)); // 2.6881171418161356E43 `

---

<div class="post-metadata">

**Author:** ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)\
**Post date:** [April 8, 2020, 3:37pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/7 "2020-04-08T15:37:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 8, 2020, 3:45pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/8 "2020-04-08T15:45:54Z")

</div>

`String ns = "" + Math.exp(100);`

---

<div class="post-metadata">

**Author:** ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)\
**Post date:** [April 8, 2020, 4:13pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/9 "2020-04-08T16:13:46Z")

</div>

And what about doubleLists? Is there any way in Processing to make a list with doubles as components?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 8, 2020, 4:24pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/10 "2020-04-08T16:24:03Z")

</div>

[Processing.org/reference/ArrayList.html](http://Processing.org/reference/ArrayList.html)  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Double.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Double.html)

```auto
import java.util.List;
final List<Double> doubles = new ArrayList<Double>();

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 8, 2020, 4:50pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/11 "2020-04-08T16:50:22Z")

</div>

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

```auto
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

```auto
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

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

---

<div class="post-metadata">

**Author:** ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)\
**Post date:** [April 8, 2020, 5:40pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/12 "2020-04-08T17:40:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 8, 2020, 5:58pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/13 "2020-04-08T17:58:44Z")

</div>

You can always perform the calculations as doubles but then cast them to floats when needed e.g.

```auto
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 😄

---

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [April 9, 2020, 3:47pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/14 "2020-04-09T15:47:01Z")

</div>

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 :

```auto
double x= 3.14159565358979d;
String strX= nf( (float) x, 1,9);

```

but that defeats the precision of double. Is there a work-around?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 9, 2020, 4:36pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/15 "2020-04-09T16:36:17Z")

</div>

> [@rhole](#):
>
> 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...)](http://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 )](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#dndec)

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 9, 2020, 5:21pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/16 "2020-04-09T17:21:16Z")

</div>

> [@rhole](#):
>
> One remains: the nf() function does not accept nf(double, int, int).

That is a consequence of Processing treating all floating point numbers as floats

---

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [April 9, 2020, 5:47pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/17 "2020-04-09T17:47:56Z")

</div>

Yes. That works perfectly. THANKS!!!

---

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [April 9, 2020, 6:03pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/18 "2020-04-09T18:03:27Z")

</div>

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.

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)\
**Post date:** [April 12, 2020, 3:06am UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/19 "2020-04-12T03:06:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [April 12, 2020, 3:43pm UTC](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532/20 "2020-04-12T15:43:46Z")

</div>

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.

[Next page](https://discourse.processing.org/t/is-there-a-library-that-allows-for-the-convenient-use-of-doubles/19532.md?page=2)
