问题:Python部门

我试图将一组从-100到0的数字归一化到10-100的范围,并且遇到了问题,只是注意到即使根本没有任何变量,这也无法评估我期望的方式:

>>> (20-10) / (100-10)
0

浮动划分也不起作用:

>>> float((20-10) / (100-10))
0.0

如果除法的任一侧都转换为浮点数,它将起作用:

>>> (20-10) / float((100-10))
0.1111111111111111

第一个示例中的每一边都被评估为一个int,这意味着最终答案将被强制转换为int。由于0.111小于.5,因此将其舍入为0。在我看来,这不是透明的,但我想是这样的。

有什么解释?

I was trying to normalize a set of numbers from -100 to 0 to a range of 10-100 and was having problems only to notice that even with no variables at all, this does not evaluate the way I would expect it to:

>>> (20-10) / (100-10)
0

Float division doesn’t work either:

>>> float((20-10) / (100-10))
0.0

If either side of the division is cast to a float it will work:

>>> (20-10) / float((100-10))
0.1111111111111111

Each side in the first example is evaluating as an int which means the final answer will be cast to an int. Since 0.111 is less than .5, it rounds to 0. It is not transparent in my opinion, but I guess that’s the way it is.

What is the explanation?


回答 0

您使用的是Python 2.x,其中整数除法将被截断而不是变成浮点数。

>>> 1 / 2
0

您应该将其中一个设为float

>>> float(10 - 20) / (100 - 10)
-0.1111111111111111

from __future__ import division,强制/采用总是返回float的Python 3.x行为。

>>> from __future__ import division
>>> (10 - 20) / (100 - 10)
-0.1111111111111111

You’re using Python 2.x, where integer divisions will truncate instead of becoming a floating point number.

>>> 1 / 2
0

You should make one of them a float:

>>> float(10 - 20) / (100 - 10)
-0.1111111111111111

or from __future__ import division, which the forces / to adopt Python 3.x’s behavior that always returns a float.

>>> from __future__ import division
>>> (10 - 20) / (100 - 10)
-0.1111111111111111

回答 1

将Integers放入其中,因此Python给了您一个整数

>>> 10 / 90
0

如果此后将其强制转换为浮点数,则四舍五入将已经完成,换句话说,0整数将始终变为0浮点数。

如果您在除法的任一侧使用浮点数,那么Python将为您提供您所期望的答案。

>>> 10 / 90.0
0.1111111111111111

因此,在您的情况下:

>>> float(20-10) / (100-10)
0.1111111111111111
>>> (20-10) / float(100-10)
0.1111111111111111

You’re putting Integers in so Python is giving you an integer back:

>>> 10 / 90
0

If if you cast this to a float afterwards the rounding will have already been done, in other words, 0 integer will always become 0 float.

If you use floats on either side of the division then Python will give you the answer you expect.

>>> 10 / 90.0
0.1111111111111111

So in your case:

>>> float(20-10) / (100-10)
0.1111111111111111
>>> (20-10) / float(100-10)
0.1111111111111111

回答 2

在进行除法之前,需要将其更改为浮点数。那是:

float(20 - 10) / (100 - 10)

You need to change it to a float BEFORE you do the division. That is:

float(20 - 10) / (100 - 10)

回答 3

在Python 2.7中,/如果输入为整数,则运算符为整数除法:

>>>20/15
1

>>>20.0/15.0
1.33333333333

>>>20.0/15
1.33333333333

在Python 3.3中,/即使输入是整数,运算符也是浮点除法。

>>> 20/15
1.33333333333

>>>20.0/15
1.33333333333

对于Python 3中的整数除法,我们将使用//运算符。

//运算符在Python 2.7和Python 3.3中都是整数除法运算符。

在Python 2.7和Python 3.3中:

>>>20//15
1

现在,看比较

>>>a = 7.0/4.0
>>>b = 7/4
>>>print a == b

对于上述程序,输出在Python 2.7中为False,在Python 3.3中为True。

在Python 2.7中a = 1.75和b = 1。

在Python 3.3中,a = 1.75和b = 1.75,仅因为/是一个浮点除法。

In Python 2.7, the / operator is an integer division if inputs are integers:

>>>20/15
1

>>>20.0/15.0
1.33333333333

>>>20.0/15
1.33333333333

In Python 3.3, the / operator is a float division even if the inputs are integer.

>>> 20/15
1.33333333333

>>>20.0/15
1.33333333333

For integer division in Python 3, we will use the // operator.

The // operator is an integer division operator in both Python 2.7 and Python 3.3.

In Python 2.7 and Python 3.3:

>>>20//15
1

Now, see the comparison

>>>a = 7.0/4.0
>>>b = 7/4
>>>print a == b

For the above program, the output will be False in Python 2.7 and True in Python 3.3.

In Python 2.7 a = 1.75 and b = 1.

In Python 3.3 a = 1.75 and b = 1.75, just because / is a float division.


回答 4

它与您使用的python版本有关。基本上,它采用C行为:如果将两个整数相除,结果将四舍五入为一个整数。还请记住,Python从左到右执行操作,这在您键入时发挥作用。

示例:由于这是我进行算术运算时总是会浮现的一个问题(我应该转换为浮点数和哪个数字),因此提供了一个来自该方面的示例:

>>> a = 1/2/3/4/5/4/3
>>> a
0

当我们将整数相除时,毫不奇怪的是,它会降低四舍五入。

>>> a = 1/2/3/4/5/4/float(3)
>>> a
0.0

如果我们强制转换要浮点数的最后一个整数,我们仍然会得到零,因为到我们的数字除以浮点数时,由于整数除法,该数字已经变为0。

>>> a = 1/2/3/float(4)/5/4/3
>>> a
0.0

与上述相同,但将float类型转换向左侧稍微移近。

>>> a = float(1)/2/3/4/5/4/3
>>> a
0.0006944444444444445

最后,当我们将第一个整数转换为浮点数时,结果是所需的整数,因为从第一个除法(即最左边的整数)开始,我们使用浮点数。

额外1:如果您尝试回答该问题以改善算术评估,则应检查此内容

附加2:请注意以下情况:

>>> a = float(1/2/3/4/5/4/3)
>>> a
0.0

It has to do with the version of python that you use. Basically it adopts the C behavior: if you divide two integers, the results will be rounded down to an integer. Also keep in mind that Python does the operations from left to right, which plays a role when you typecast.

Example: Since this is a question that always pops in my head when I am doing arithmetic operations (should I convert to float and which number), an example from that aspect is presented:

>>> a = 1/2/3/4/5/4/3
>>> a
0

When we divide integers, not surprisingly it gets lower rounded.

>>> a = 1/2/3/4/5/4/float(3)
>>> a
0.0

If we typecast the last integer to float, we will still get zero, since by the time our number gets divided by the float has already become 0 because of the integer division.

>>> a = 1/2/3/float(4)/5/4/3
>>> a
0.0

Same scenario as above but shifting the float typecast a little closer to the left side.

>>> a = float(1)/2/3/4/5/4/3
>>> a
0.0006944444444444445

Finally, when we typecast the first integer to float, the result is the desired one, since beginning from the first division, i.e. the leftmost one, we use floats.

Extra 1: If you are trying to answer that to improve arithmetic evaluation, you should check this

Extra 2: Please be careful of the following scenario:

>>> a = float(1/2/3/4/5/4/3)
>>> a
0.0

回答 5

通过放置“。”来指定浮点数 该数字之后也会导致其默认浮动。

>>> 1 / 2
0

>>> 1. / 2.
0.5

Specifying a float by placing a ‘.’ after the number will also cause it to default to float.

>>> 1 / 2
0

>>> 1. / 2.
0.5

回答 6

使其中至少一个浮点,然后将是浮点除法,而不是整数:

>>> (20.0-10) / (100-10)
0.1111111111111111

将结果强制转换为浮点型为时已晚。

Make at least one of them float, then it will be float division, not integer:

>>> (20.0-10) / (100-10)
0.1111111111111111

Casting the result to float is too late.


回答 7

在python中cv2未更新除法计算。因此,您必须将其包括from __future__ import division 在程序的第一行中。

In python cv2 not updated the division calculation. so, you must include from __future__ import division in first line of the program.


回答 8

无论哪种方式,它都是整数除法。10/90 =0。在第二种情况下,您只是将0强制转换为浮点数。

尝试将“ /”的操作数之一强制转换为浮点数:

float(20-10) / (100-10)

Either way, it’s integer division. 10/90 = 0. In the second case, you’re merely casting 0 to a float.

Try casting one of the operands of “/” to be a float:

float(20-10) / (100-10)

回答 9

在第二个示例中,除法已经发生,您正在转换为浮动。试试这个:

float(20-10) / float(100-10)

You’re casting to float after the division has already happened in your second example. Try this:

float(20-10) / float(100-10)

回答 10

令我惊讶的是,没有人提到原始海报可能喜欢有理数。如果您对此感兴趣,基于Python的程序Sage会为您服务。(尽管3.x正在开发中,但目前仍基于Python2.x。)

sage: (20-10) / (100-10)
1/9

这不是每个人的解决方案,因为它会做一些准备工作,所以这些数字不是intSage Integer类元素。尽管如此,值得一提的是Python生态系统的一部分。

I’m somewhat surprised that no one has mentioned that the original poster might have liked rational numbers to result. Should you be interested in this, the Python-based program Sage has your back. (Currently still based on Python 2.x, though 3.x is under way.)

sage: (20-10) / (100-10)
1/9

This isn’t a solution for everyone, because it does do some preparsing so these numbers aren’t ints, but Sage Integer class elements. Still, worth mentioning as a part of the Python ecosystem.


回答 11

我个人更喜欢1. *在开始时插入一个。所以表达式变成了这样的东西:

1. * (20-10) / (100-10)

因为我总是对某些公式进行除法,例如:

accuracy = 1. * (len(y_val) - sum(y_val)) / len(y_val)

因此不可能简单地添加一个.0like 20.0。就我而言,用a换行float()可能会失去一点可读性。

Personally I preferred to insert a 1. * at the very beginning. So the expression become something like this:

1. * (20-10) / (100-10)

As I always do a division for some formula like:

accuracy = 1. * (len(y_val) - sum(y_val)) / len(y_val)

so it is impossible to simply add a .0 like 20.0. And in my case, wrapping with a float() may lose a little bit readability.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。