Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ./ (dot slash) operator represent in Python?

Tags:

python

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?

like image 373
Matt Garrod Avatar asked Sep 11 '12 06:09

Matt Garrod


3 Answers

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
like image 132
Matthew Adams Avatar answered Sep 29 '22 04:09

Matthew Adams


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.

like image 31
Martijn Pieters Avatar answered Sep 29 '22 05:09

Martijn Pieters


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
like image 44
the wolf Avatar answered Sep 29 '22 05:09

the wolf