파이썬 부서
나는 -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으로 반올림됩니다. 제 생각에는 투명하지 않지만 그 방법이 맞는 것 같습니다.
설명은 무엇입니까?
부동 소수점 숫자가 아닌 정수 나누기가 잘리는 Python 2.x를 사용하고 있습니다.
>>> 1 / 2
0
당신은 그들 중 하나를 만들어야합니다 float
:
>>> float(10 - 20) / (100 - 10)
-0.1111111111111111
또는 . 항상 float를 반환하는 Python 3.x의 동작을 from __future__ import division
강제 /
로 적용합니다.
>>> from __future__ import division
>>> (10 - 20) / (100 - 10)
-0.1111111111111111
당신이있어 파이썬은 당신에게 정수 등을 제공되도록에서 정수를 넣어 :
>>> 10 / 90
0
나중에 플로트에 캐스트하면 반올림이 이미 완료된 것입니다. 즉, 0 정수는 항상 0 플로트가됩니다.
디비전의 양쪽에 float를 사용하면 Python이 예상 답변을 제공합니다.
>>> 10 / 90.0
0.1111111111111111
따라서 귀하의 경우 :
>>> float(20-10) / (100-10)
0.1111111111111111
>>> (20-10) / float(100-10)
0.1111111111111111
나누기 전에 플로트로 변경해야합니다. 그건:
float(20 - 10) / (100 - 10)
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
파이썬 3의 정수 나누기에는 //
연산자 를 사용합니다 .
//
운영자 모두 파이썬 2.7 파이썬 3.3의 정수 나눗셈 연산자이다.
Python 2.7 및 Python 3.3에서 :
>>>20//15
1
이제 비교를보십시오
>>>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.
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
Specifying a float by placing a '.' after the number will also cause it to default to float.
>>> 1 / 2
0
>>> 1. / 2.
0.5
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.
In python cv2
not updated the division calculation. so, you must include from __future__ import division
in first line of the program.
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)
You're casting to float after the division has already happened in your second example. Try this:
float(20-10) / float(100-10)
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 int
s, but Sage Integer
class elements. Still, worth mentioning as a part of the Python ecosystem.
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.
참고URL : https://stackoverflow.com/questions/2958684/python-division
'Programing' 카테고리의 다른 글
Objective-C에서 배열 결합 (0) | 2020.07.03 |
---|---|
Express에서 사용자 정의 파비콘을 설정하는 방법은 무엇입니까? (0) | 2020.07.03 |
자바 스크립트의 자식 클래스에서 부모 메서드를 호출하는 방법은 무엇입니까? (0) | 2020.07.03 |
문자열이 직렬화되어 있는지 확인 하시겠습니까? (0) | 2020.07.03 |
하스켈에서의 암기? (0) | 2020.07.03 |