Matt Garrod
Matt Garrod

Reputation: 878

What does the ./ (dot slash) operator represent in 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?

Upvotes: 9

Views: 7875

Answers (4)

the wolf
the wolf

Reputation: 35552

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

Upvotes: 4

ie.
ie.

Reputation: 6101

1. represents floating point number. / represents divide.

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1123480

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.

Upvotes: 14

Matthew Adams
Matthew Adams

Reputation: 10156

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

Upvotes: 19

Related Questions