0

I am converting a javascript function to java, and don't understand the purpose of the bitwise ors in the code below:

  1. (Math.tan(PHId)) ^ 2) - is this ensuring the number always ends in 2?

  2. (Et ^ 6)

The code is part of a library to convert Irish Grid References to/from Latitude and Longitude: http://www.nearby.org.uk/tests/geotools2.js

GT_Math.E_N_to_Lat = function(East, North, a, b, e0, n0, f0, PHI0, LAM0)
{
    //Un-project Transverse Mercator eastings and northings back to latitude.
    //Input: - _
    //eastings (East) and northings (North) in meters; _
    //ellipsoid axis dimensions (a & b) in meters; _
    //eastings (e0) and northings (n0) of false origin in meters; _
    //central meridian scale factor (f0) and _
    //latitude (PHI0) and longitude (LAM0) of false origin in decimal degrees.

    //'REQUIRES THE "Marc" AND "InitialLat" FUNCTIONS

    //Convert angle measures to radians
    var Pi = 3.14159265358979;
    var RadPHI0 = PHI0 * (Pi / 180);
    var RadLAM0 = LAM0 * (Pi / 180);

    //Compute af0, bf0, e squared (e2), n and Et
    var af0 = a * f0;
    var bf0 = b * f0;
    var e2 = (Math.pow(af0,2) - Math.pow(bf0,2)) / Math.pow(af0,2);
    var n = (af0 - bf0) / (af0 + bf0);
    var Et = East - e0;

    //Compute initial value for latitude (PHI) in radians
    var PHId = GT_Math.InitialLat(North, n0, af0, RadPHI0, n, bf0);
    
    //Compute nu, rho and eta2 using value for PHId
    var nu = af0 / (Math.sqrt(1 - (e2 * ( Math.pow(Math.sin(PHId),2)))));
    var rho = (nu * (1 - e2)) / (1 - (e2 * Math.pow(Math.sin(PHId),2)));
    var eta2 = (nu / rho) - 1;
    
    //Compute Latitude
    var VII = (Math.tan(PHId)) / (2 * rho * nu);
    var VIII = ((Math.tan(PHId)) / (24 * rho * Math.pow(nu,3))) * (5 + (3 * (Math.pow(Math.tan(PHId),2))) + eta2 - (9 * eta2 * (Math.pow(Math.tan(PHId),2))));
    var IX = ((Math.tan(PHId)) / (720 * rho * Math.pow(nu,5))) * (61 + (90 * ((Math.tan(PHId)) ^ 2)) + (45 * (Math.pow(Math.tan(PHId),4))));
    
    var E_N_to_Lat = (180 / Pi) * (PHId - (Math.pow(Et,2) * VII) + (Math.pow(Et,4) * VIII) - ((Et ^ 6) * IX));
    
    return (E_N_to_Lat);
}
5
  • Can you please explain a bit further what you don't understand exactly? ^ is bitwise XOR. Commented May 6, 2022 at 21:15
  • I'm not sure what the type will be of the result of (Math.tan(PHId)) in javascript? I would imagine this is some sort of arithmetic shortcut, but I can't see how that would work if the result is a double/float. Thanks, Mark. Commented May 6, 2022 at 21:25
  • OK I found this: Before a bitwise operation is performed, JavaScript converts numbers to 32 bits signed integers. After the bitwise operation is performed, the result is converted back to 64 bits JavaScript numbers. Commented May 6, 2022 at 21:32
  • Do you have a description of this algorithm? Commented May 6, 2022 at 21:33
  • No sorry, only the comment in the javascript function. Commented May 6, 2022 at 22:08

1 Answer 1

4

I recommend to ask the author of the script.

However, I am reasonably certain that this is simply a mistake, and what was meant is Math.tan(PHId) ** 2 / Math.pow(Math.tan(PHId), 2) and Et ** 6/ Math.pow(Et, 6), i.e. exponentiation instead of bitwise OR. I believe this because

  • bitwise OR just doesn't make sense in numeric code
  • this looks very much like a series expansion - the preceeding terms also use exponentiation, and the mistake likely wasn't noticed because it introduces only a small error
  • All the other methods in the script (E_N_to_Long, Lat_Long_to_East, Lat_Long_to_North) use Math.pow everywhere, E_N_to_Lat is the only one to use ^

I am converting a javascript function to java

Notice the comment at the top of the script:

 * Credits

 * The algorithm used by the script for WGS84-OSGB36 conversions is derived 
 * from an OSGB spreadsheet (www.gps.gov.uk) with permission. This has been
 * adapted into PHP by Ian Harris, and Irish added by Barry Hunter

I would advise to start from these primary sources, instead of translating the JavaScript translation of a PHP translation of a spreadsheet formula translation of mathematics into Java.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the response. I have asked the author, he is going to check against the original PHP code. The error introduced by the ^2 is 2x10-8 so as you rightly concluded very small.
I contacted the author and it is indeed a conversion error from the original PHP code, the XOR should be raise to power. Thanks for all the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.