# About Processing automatically converting double literals to floats

**URL:** <https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886>\
**Category:** Beginners\
**Created:** [March 26, 2021, 7:46pm UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886 "2021-03-26T19:46:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 26, 2021, 7:46pm UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/1 "2021-03-26T19:46:45Z")

</div>

Hi everyone,

In regular Java and other programming languages, this is well known that you can write :

```processing
System.out.println(0.1 + 0.2 == 0.3);

```

which gives `false`!!

This is due to floating point precision and how computers store float values : [https://0.30000000000000004.com/](https://0.30000000000000004.com/)

Now typing the same in Processing gives this :

```processing
System.out.println(0.1 + 0.2); // -> 0.3

```

So at first I was very intrigued! 🤔

But looking at the exported application, I saw the packaged code :

```java
import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class precision extends PApplet {
  public void setup() {
System.out.println(0.1f + 0.2f);
    noLoop();
  }

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "precision" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

```

Which shows that Processing is **adding the letter `f` to each double literal value automatically**.

Actually in the [Java section](https://0.30000000000000004.com/#java) it shows that using floats solve this issue.

Thought, this is nice for us :

> <https://stackoverflow.com/questions/19444081/java-float-is-more-precise-than-double>

Is the fact that float values avoid some precision errors the only reason behind Processing changing things behind the scene?

Also how much does the Processing IDE hide code pre processing for us? (I know that it wraps the whole code inside the `PApplet` class 😉 )

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 26, 2021, 7:49pm UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/2 "2021-03-26T19:49:22Z")

</div>

Float is far less accurate than double - they’re not removing precision errors, they’re adding them.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 26, 2021, 7:55pm UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/3 "2021-03-26T19:55:33Z")

</div>

@neilcsmith Yes you are totally right, the fact that float values have more rounding error (and got it right with the addition above) is confusing because you can think that they are more precise that `double` ! 😉

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 27, 2021, 6:59pm UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/4 "2021-03-27T18:59:28Z")

</div>

So is this the reason why sometimes this happens?

```auto
float a = 123;
int b = 2;
float c = a + b;
println(c); // prints -> 125.00000000003

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 27, 2021, 7:05pm UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/5 "2021-03-27T19:05:25Z")

</div>

Yes, see the website linked above - [https://0.30000000000000004.com/](https://0.30000000000000004.com/) (I’d missed @josephh has posted this link until Discourse warned me I was posting the same thing! 😄 )

The funny thing is that at times float might look more accurate, because the error is so small it doesn’t fit in a float. It looks correct because it’s more imprecise.

Processing’s decision to use floats instead of doubles is one of my pet hates - it makes little sense in most cases, and the preprocessor needed to transpile is one of the reasons Processing is so far behind Java in its syntax. PraxisLIVE actually uses double throughout its Processing wrapper API for this reason.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 28, 2021, 12:15am UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/6 "2021-03-28T00:15:43Z")

</div>

> [@neilcsmith](#):
>
> the preprocessor needed to transpile is one of the reasons Processing is so far behind Java in its syntax.

@neilcsmith Do you know what part in the the source code is responsible for the transpiling part? I couldn’t find it 🤔

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 28, 2021, 8:30am UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/7 "2021-03-28T08:30:15Z")

</div>

@josephh see here and other code in that package [processing/PdePreprocessor.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/java/src/processing/mode/java/preproc/PdePreprocessor.java)

---

<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:** [March 28, 2021, 1:34pm UTC](https://discourse.processing.org/t/about-processing-automatically-converting-double-literals-to-floats/28886/8 "2021-03-28T13:34:46Z")

</div>

If you really need your floating literals to be `double` instead of `float` inside a “.pde” file just suffix them w/ the letter `d` or `D`: 💡  
`static final double PI_DOUBLE_PRECISION = 3.141592653589793d;`
