简体   繁体   English

./(点斜杠)运算符在Python中代表什么?

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

I am trying to port a piece of code from Python to PHP. 我正在尝试将一段代码从Python移植到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 . 因此,将浮点值1.0 (省略零)除以latcos()

It makes the 1 a float value. 它使1为浮点值。 Equivalent to float(1) 相当于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) 恕我直言,如果使用这种方式编写(在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. 由于math.cos()返回浮点数,因此可以在顶部使用整数。

If you want Python to have a ' true division ' similar to Perl / PHP, you do this way: 如果您希望Python具有类似于Perl / PHP的“ 真除法 ”,则可以这样做:

>>> from __future__ import division
>>> 1/2
0.5

1. represents floating point number. 1.表示浮点数。 / represents divide. /代表分度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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