简体   繁体   中英

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?

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

1. represents floating point number. / represents divide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM