Problem sin() cos() Java to Processing 4.0

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?

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

I tried this:

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

:)

3 Likes

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.

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 ));
1 Like

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:

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:

4 Likes

:)

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.

1 Like

@quark

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 < This is a float !
TWO_PI / Reference / Processing.org < This is a float !

:)

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 …

Encore une fois merci, la précision est maintenant millimétrique !
A bientôt sur ce précieux forum. :grinning:

2 Likes

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?

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;
}
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;
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.

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.

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?

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

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

if you search for sin( you will find this:

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!

:)

1 Like