New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve numerical precision of inverse spherical mercator #1105
Conversation
Much better would be to replace
by
This is accurate everywhere, is demonstrably odd, and doesn't require branches or C99 functions. |
c40f61f
to
3e967d5
Compare
I knew you would have a better way to do this. Updated the code and it is indeed much simpler. Thanks for the suggestion! |
src/PJ_merc.c
Outdated
@@ -57,7 +57,7 @@ static LP e_inverse (XY xy, PJ *P) { /* Ellipsoidal, inverse */ | |||
|
|||
static LP s_inverse (XY xy, PJ *P) { /* Spheroidal, inverse */ | |||
LP lp = {0.0,0.0}; | |||
lp.phi = M_HALFPI - 2. * atan(exp(-xy.y / P->k0)); | |||
lp.phi = -atan(sinh(-xy.y / P->k0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lots of negation there. I guess atan(sinh(xy.y / P->k0)) would do ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the two negations cancel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You guessed right! Nice catch.
Perhaps there should be a new test point in gie tests to demonstrate the increased accuracy ? |
3e967d5
to
8dff160
Compare
Yeah. The original PR didn't have on and for some reason I can't come up with one that demonstrates that this approach is better. Perhaps it isn't more precise than the original code? |
The difference between both approaches only show up at extremely small absolute values
At 1e-10 and above, the relative error is below 1e-7 |
Yeah, and my tests has been using extremely small inputs. From the below I see a change, but not exactly an improvement. I would expect to the input to be repeated in the output in this example:
|
Disregard my conclusion from the previous post. With the changes in this PR the precision is in fact improved. I forgot to account for the |
8dff160
to
27b2deb
Compare
27b2deb
to
62b8164
Compare
Supersedes #931