I have implemented two functions LatY and YLat that converts a given latitude value to a normalized Mercator y-coordinate value and vice versa. The LatY function uses the following formula:
double LatY(double lat)
{
var sin = Math.Sin(lat * Math.PI / 180);
var y = 0.5 - 0.25 * Math.Log((1 + sin) / (1 - sin)) / Math.PI;
return y;
}
And the YLat function uses the following formula:
double YLat(double y)
{
var exp = -(Math.PI / 0.25) * (y - 0.5);
var antiLog = Math.Pow(10, exp);
var sin = (antiLog - 1) / (antiLog + 1);
var lat2 = Math.Asin(sin) * (180 / Math.PI);
return lat2;
}
However, when I tested the functions by converting a latitude value to y using LatY and then back to latitude using YLat, the result was not the same as the original latitude value. For example:
var y = LatY(-112.484379);
var lat = YLat(y);
The expected value of lat should be -112.484379, but the calculated value is -87.22371494918505.
I suspect that the precision issue may be causing this problem, but I'm not sure how to fix it. Can anyone provide some insights on what might be causing this issue and how to address it?
I was expecting that the calculated latitude value would be the same as the original latitude value. However, the result was not as expected, and there was a significant difference between the expected and actual values.
Note: I have also tried it in JavaScript but facing the same problem.