'Please explain the purpose of the bitwise or in this javascript function
I am converting a javascript function to java, and don't understand the purpose of the bitwise ors in the code below:
(Math.tan(PHId)) ^ 2)- is this ensuring the number always ends in 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);
}
Solution 1:[1]
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) useMath.poweverywhere,E_N_to_Latis 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bergi |
