I am trying to port a piece of code from Python to PHP. I've come across a line that I don't understand the notation for.
secLat = 1./cos(lat)
What does the ./ operator do in this context?
They are just using a decimal followed by a divide sign to make sure the result is a float instead of an int. This avoids problems like the following:
>>> 1/3
0
>>> 1./3
0.3333333333333333
You are reading that wrong I'm afraid; it's:
(1.)/cos(lat)
so, divide floating point value 1.0
(with the zero omitted) by the cos()
of lat
.
It makes the 1 a float value. Equivalent to float(1)
With two integers, the /
is a floor function:
>>> 12/5
2
With one argument a float, /
acts as you expect:
>>> 12.0/5
2.4
>>> 12/5.0
2.4
IMHO, the code you posted is less ambiguous if written this way (in Python)
secLat = 1.0/cos(lat)
Or
secLat = float(1)/cos(lat)
Or
secLat = 1/cos(lat)
Since math.cos() returns a float, you can use an integer on top.
If you want Python to have a 'true division' similar to Perl / PHP, you do this way:
>>> from __future__ import division
>>> 1/2
0.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With