translation help?

0 views
Skip to first unread message

Bell, Kevin

unread,
Jun 23, 2008, 12:20:51 PM6/23/08
to utahp...@googlegroups.com
I’m translating a function from the mystery language below to python, but I don’t know what I’m doing…  should this get me the same results?  I don’t understand the do/while stuff in the first snippet.
       
 
MYSTERY CODE:
 
        // Substitute the estimate into the iterative calculation that
        // converges on the correct Latitude value.
        var part1 = (1 - (Ec * Math.sin(Lat0))) / (1 + (Ec * Math.sin(Lat0)));
        var LatR = Pi2 - (2 * Math.atan(t * Math.pow(part1,(Ec/2))));
        do {
               Lat0 = LatR;
               part1 = (1 - (Ec * Math.sin(Lat0))) / (1 + (Ec * Math.sin(Lat0)));
               //LatR = Pi2 - (2 * Math.atan2((t * (part1**(Ec/2))),1));
               LatR = Pi2 - (2 * Math.atan(t * Math.pow(part1,(Ec/2))));
        } while (Math.abs(LatR - Lat0) > 0.000000002);
 
 
MY PYTHON ATTEMPT:
      part1 = (1 - (Ec * math.sin(Lat0))) / (1 + (Ec * math.sin(Lat0)))
        LatR = Pi2 - (2 * math.atan(t * math.pow(part1,(Ec/2))))
        while (abs(LatR - Lat0) > 0.000000002):
               Lat0 = LatR
               part1 = (1 - (Ec * math.sin(Lat0))) / (1 + (Ec * math.sin(Lat0)))
               LatR = Pi2 - (2 * math.atan(t * math.pow(part1,(Ec/2))))

 

 

Kevin Bell

GIS Specialist

Salt Lake City Transportation Division

349 South 200 East, Suite 450

P.O. Box 145502

Salt Lake City, UT 84114-5502

801-535-7131

 

Jason Edwards

unread,
Jun 23, 2008, 12:24:26 PM6/23/08
to utahp...@googlegroups.com
Your mystery language looks like JavaScript to me...

Dennis Muhlestein

unread,
Jun 23, 2008, 12:26:56 PM6/23/08
to Utah Python User Group


On Jun 23, 10:20 am, "Bell, Kevin" <kevin.b...@slcgov.com> wrote:
> I'm translating a function from the mystery language below to python,
> but I don't know what I'm doing... should this get me the same results?
> I don't understand the do/while stuff in the first snippet.
>
> MYSTERY CODE:
>
> // Substitute the estimate into the iterative calculation that
> // converges on the correct Latitude value.
> var part1 = (1 - (Ec * Math.sin(Lat0))) / (1 + (Ec *
> Math.sin(Lat0)));
> var LatR = Pi2 - (2 * Math.atan(t * Math.pow(part1,(Ec/2))));
> do {
> Lat0 = LatR;
> part1 = (1 - (Ec * Math.sin(Lat0))) / (1 + (Ec *
> Math.sin(Lat0)));
> //LatR = Pi2 - (2 * Math.atan2((t * (part1**(Ec/2))),1));
> LatR = Pi2 - (2 * Math.atan(t * Math.pow(part1,(Ec/2))));
> } while (Math.abs(LatR - Lat0) > 0.000000002);
>
> MY PYTHON ATTEMPT:
> part1 = (1 - (Ec * math.sin(Lat0))) / (1 + (Ec * math.sin(Lat0)))
> LatR = Pi2 - (2 * math.atan(t * math.pow(part1,(Ec/2))))
> while (abs(LatR - Lat0) > 0.000000002):
> Lat0 = LatR
> part1 = (1 - (Ec * math.sin(Lat0))) / (1 + (Ec *
> math.sin(Lat0)))
> LatR = Pi2 - (2 * math.atan(t * math.pow(part1,(Ec/2))))
>

In Python, there is no do..while construct. The difference is that a
do while loop will always execute at least once since the while
condition is evaluated after the 1st execution. In a while loop, if
the condition is satisfied, the loop may never execute.

While there is probably a better solution, you can simulate the do/
while behavior like this:

while True:
# statements
if condition:
break

-Dennis
Reply all
Reply to author
Forward
0 new messages