# Problem sin() cos() Java to Processing 4.0

**URL:** <https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567>\
**Category:** Beginners\
**Created:** [January 10, 2025, 1:42am UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567 "2025-01-10T01:42:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![RadioG](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@RadioG](https://discourse.processing.org/u/RadioG)\
**Post date:** [January 10, 2025, 1:42am UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/1 "2025-01-10T01:42:28Z")

</div>

Hi,

Under Processing4.0 this formula does not give me the expected result of: 18.6063731708993. Do you have a solution with a better syntax?

```auto
(Math.asin(sin(12.34*PI/180) * Math.cos(1000/6371)+cos(12.34*PI/180)*Math.sin(1000/6371) * Math.cos(45*PI/180)) )*180/PI);

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 10, 2025, 2:11am UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/2 "2025-01-10T02:11:05Z")

</div>

I tried this:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/f/4f193e306bdb8ba3128bd2632d5dfd21a9dec68f.png)

```auto
println(Double.toString((Math.asin(Math.sin(12.34d*Math.PI/180)*Math.cos(1000.0d/6371)+Math.cos(12.34d*Math.PI/180)*Math.sin(1000.0d/6371)*Math.cos(45d*Math.PI/180))*180d/Math.PI)));
// output:
// 18.606373170899314

// Google calculates:
// asin(sin(12.34*PI/180)*cos(1000.0/6371) + cos(12.34*PI/180)*sin(1000.0/6371)*cos(45*PI/180) )*180/PI =
18.6063732 rad

```

`:)`

---

<div class="post-metadata">

**Author:** ![RadioG](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@RadioG](https://discourse.processing.org/u/RadioG)\
**Post date:** [January 10, 2025, 1:48pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/3 "2025-01-10T13:48:19Z")

</div>

Merci beaucoup de ton rapide retour. J’ai testé le résultat de ta formule avec Print, la valeur est parfaite!  
Cependant, pour continuer mes lignes de code, je dois calculer la valeur des coordonnées et j’ai testé en supprimant la fonction " Double.toString() " et la valeur est fausse. Voici ma formule, le résultat attendu est 43.951391048603, si tu peux y jeter un œil.

```auto
lat= Math.asin(Math.sin(43.9513810d*DEG_TO_RAD)
*Math.cos(1.13d/6371000)+Math.cos(43.9513810d*DEG_TO_RAD )
*Math.sin(1.13d/6371000)
*Math.cos(351.4d*DEG_TO_RAD ));

```

---

<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:** [January 10, 2025, 4:07pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/4 "2025-01-10T16:07:19Z")

</div>

If you require high `double` precision, besides replacing Processing’s math functions w/ corresponding Java’s Math, avoid Processing’s constants like DEG\_TO\_RAD as well; b/c they’re `float` type, not `double`:

> <https://github.com/processing/processing4/blob/processing-1295-4.3.2/core/src/processing/core/PConstants.java#L210-L211>

```auto
final double DEG_TO_RAD_D = Math.PI / 180, RAD_TO_DEG_D = 180 / Math.PI;

final double phi = 43.95_13_81d, dist = 1.13d / 6_371_000, theta = 351.4d;

final double lat = Math.asin(
  Math.sin(phi * DEG_TO_RAD_D) * Math.cos(dist) +
  Math.cos(phi * DEG_TO_RAD_D) * Math.sin(dist) * Math.cos(theta * DEG_TO_RAD_D)
);

println(lat, lat * RAD_TO_DEG_D);

```

For further reading about `double` type in Processing/Java, go to this thread below:

> [@Issues when applying a formula](https://discourse.processing.org/t/issues-when-applying-a-formula/45520/2):
>
> Processing uses the 32-bit float primitive datatype as its default floating-point precision: For 64-bit floating-point precision we use double instead: However, it’s not enough to simply declare variables w/ primitive datatype double! We also must suffix any floating-point literal w/ a d, so it uses a 64-bit storage: 6356911.94613d Without it, 6356911.94613 would be coerced to 6356912.0 due to 32-bit storage constraint! Plus all Processing’s math functions need to be replaced w/ their co…

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 11, 2025, 3:57am UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/5 "2025-01-11T03:57:03Z")

</div>

> [@RadioG](#):
>
> Voici ma formule, le résultat attendu est 43.951391048603, si tu peux y jeter un œil.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/4/e460d0580bc4fca26baf08303d6c6ebee99421c6.png)

`:)`

---

<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:** [January 11, 2025, 8:55am UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/6 "2025-01-11T08:55:44Z")

</div>

There are 2π radians in a full circle so the first line should be

`double DEG_TO_RAD = Math.PI / 180;`

also I would include a second constant-

`double RAD_TO_DEG = 180 / Math.PI;`

the the last line would be

`println(lat * RAD_TO_DEG);`

which makes it clear the output is in degrees.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 11, 2025, 12:56pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/7 "2025-01-11T12:56:27Z")

</div>

@quark

> [@quark](#):
>
> There are 2π radians in a full circle so the first line should be
> 
> `double DEG_TO_RAD = Math.PI / 180;`

Agree!

I was trying to see if _Math.TAU_ worked when I tried this and _it did not_ and I did not change the 360 back to 180.

@RadioG  
It does yield the expected result so I suggest that you revisit this, scrutinize your formula and code and what the expected result is.

What is the mathematical formula used for the expected result?  
And what is calculating this expected result?

Reference:  
[TAU / Reference / Processing.org](https://processing.org/reference/TAU.html) \< This is a float !  
[TWO\_PI / Reference / Processing.org](https://processing.org/reference/TWO_PI.html) \< This is a float !

`:)`

---

<div class="post-metadata">

**Author:** ![RadioG](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@RadioG](https://discourse.processing.org/u/RadioG)\
**Post date:** [January 11, 2025, 6:37pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/8 "2025-01-11T18:37:03Z")

</div>

Effectivement la conversion passe par “Math.PI/180” et non “…/360”.  
En synthèse :

- la valeur numérique de la latitude issue d’une fichier.csv devient `double φ1 =(Double.parseDouble(table.getString(iLignecalcullatlonBearing, "LAT")));`
- et le calcul de lat: `final double lat=(Math.asin(Math.sin(φ1* Math.PI/ 180.0d)*Math.cos(distance_D /6_371_000d) +Math.cos(φ1* Math.PI / 180.0d)*Math.sin(distance_D /6_371_000d)*Math.cos(θ* Math.PI / 180.0d) )) *180.0d/Math.PI ;`
- le résultat semble correct maintenant. A suivre …

---

<div class="post-metadata">

**Author:** ![RadioG](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@RadioG](https://discourse.processing.org/u/RadioG)\
**Post date:** [January 12, 2025, 11:53am UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/9 "2025-01-12T11:53:14Z")

</div>

Encore une fois merci, la précision est maintenant _millimétrique_ !  
A bientôt sur ce précieux forum. 😀

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [January 13, 2025, 3:36pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/10 "2025-01-13T15:36:42Z")

</div>

You are deviding a double by an integer.

While this works, you should add a capital D after all numbers.

Avoid any Processing method or constant.

Make sure everything is `double`.

Does Math refer to `java.lang.Math` or some Processing class?

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [January 13, 2025, 3:45pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/11 "2025-01-13T15:45:01Z")

</div>

```auto
private static double doFormula (){
  return java.lang.Math.asin(java.lang.Math.sin(12.34D*java.lang.Math.PI/180D) * java.lang.Math.cos(1000D/6371D)+java.lang.Math.cos(12.34D*java.lang.Math.PI/180D)*java.lang.Math.sin(1000D/6371D) * java.lang.Math.cos(45D*java.lang.Math.PI/180D)) )*180D/java.lang.Math.PI;
}

```

```auto
java.lang.Math.asin(java.lang.Math.sin(12.34D*java.lang.Math.PI/180D) * java.lang.Math.cos(1000D/6371D)+java.lang.Math.cos(12.34D*java.lang.Math.PI/180D)*java.lang.Math.sin(1000D/6371D) * java.lang.Math.cos(45d*java.lang.Math.PI/180D)) )*180D/java.lang.Math.PI;

```

```auto
private final static double formulaResult = java.lang.Math.asin(java.lang.Math.sin(12.34D*java.lang.Math.PI/180D) * java.lang.Math.cos(1000D/6371D)+java.lang.Math.cos(12.34D*java.lang.Math.PI/180D)*java.lang.Math.sin(1000D/6371D) * java.lang.Math.cos(45D*java.lang.Math.PI/180D)) )*180D/java.lang.Math.PI;

```

If you want it to print correctly, that’s a different story.

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [January 13, 2025, 3:46pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/12 "2025-01-13T15:46:09Z")

</div>

Google might not be totally perfect either.

I would probably trust Java over Google.

In general, avoid Processing’s math-related functions.

Only use them for testing purposes.

Java’s Math methods are faster, more precise, and are generally superior.

---

<div class="post-metadata">

**Author:** ![RadioG](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@RadioG](https://discourse.processing.org/u/RadioG)\
**Post date:** [January 13, 2025, 10:36pm UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/13 "2025-01-13T22:36:04Z")

</div>

Hi,  
Indeed the use of “Double” with Processing4.0 is essential for a precision of 8 decimal places or more.  
Concerning the choice of Processing, it was initially motivated to create an HMI and Java which is its language. On the other hand Processing was developed at the time of 32 bits and is running out of steam a little. My prototype is now functional, I will move on to a Rasberry pi4 and Java. Do you think it is a good choice for these calculations?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 14, 2025, 1:03am UTC](https://discourse.processing.org/t/problem-sin-cos-java-to-processing-4-0/45567/14 "2025-01-14T01:03:34Z")

</div>

> [@RadioG](#):
>
> Indeed the use of “Double” with Processing4.0 is essential for a precision of 8 decimal places or more.

> <https://stackoverflow.com/questions/20437003/what-is-the-difference-between-double-and-double-in-java>

There is also a difference between adding D or d as a suffix!  
I will let you look this up.

> [@RadioG](#):
>
> My prototype is now functional, I will move on to a Rasberry pi4 and Java. Do you think it is a good choice for these calculations?

Processing is Java under the hood!

The Processing helper functions can be found here:  
_[processing4/core/src/processing/core/PApplet.java at main · processing/processing4 · GitHub](https://github.com/processing/processing4/blob/main/core/src/processing/core/PApplet.java)_

if you search for `sin(` you will find this:

> <https://github.com/processing/processing4/blob/main/core/src/processing/core/PApplet.java#L4332>

Notice that it is cast to a float!

Tip:  
I added the bracket after sin to narrow down search!  
Try it without bracket and see what happens!

`:)`
