Count folding paper [duplicate] - python

Consider this division in Python 3:
>>> 2/2
1.0
Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast?
In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding down to 0? for the opposite, 2.x-specific problem.

Take a look at PEP-238: Changing the Division Operator
The // operator will be available to request floor division unambiguously.

Oops, immediately found 2//2. This will output an int rather than a float.

Behavior of the division operator in Python 2.7 and Python 3
In Python 2.7: By default, division operator will return integer output.
To get the result in double, multiply the dividend or divisor by 1.0.
100/35 => 2 # Expected is 2.857142857142857
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857
In Python 3
// => used for integer output
/ => used for double output
100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0 # Floating-point result if the divisor or dividend is real

The accepted answer already mentions PEP 238. I just want to add a quick look behind the scenes for those interested in what's going on without reading the whole PEP.
Python maps operators like +, -, * and / to special functions, such that e.g. a + b is equivalent to
a.__add__(b)
Regarding division in Python 2, there is by default only / which maps to __div__ and the result is dependent on the input types (e.g. int, float).
Python 2.2 introduced the __future__ feature division, which changed the division semantics the following way (TL;DR of PEP 238):
/ maps to __truediv__ which must "return a reasonable approximation of
the mathematical result of the division" (quote from PEP 238)
// maps to __floordiv__, which should return the floored result of /
With Python 3.0, the changes of PEP 238 became the default behaviour and there is no more special method __div__ in Python's object model.
If you want to use the same code in Python 2 and Python 3 use
from __future__ import division
and stick to the PEP 238 semantics of / and //.

Related

unsupported operand type(s) for <<: 'float' and 'int' [duplicate]

Consider this division in Python 3:
>>> 2/2
1.0
Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast?
In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding down to 0? for the opposite, 2.x-specific problem.
Take a look at PEP-238: Changing the Division Operator
The // operator will be available to request floor division unambiguously.
Oops, immediately found 2//2. This will output an int rather than a float.
Behavior of the division operator in Python 2.7 and Python 3
In Python 2.7: By default, division operator will return integer output.
To get the result in double, multiply the dividend or divisor by 1.0.
100/35 => 2 # Expected is 2.857142857142857
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857
In Python 3
// => used for integer output
/ => used for double output
100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0 # Floating-point result if the divisor or dividend is real
The accepted answer already mentions PEP 238. I just want to add a quick look behind the scenes for those interested in what's going on without reading the whole PEP.
Python maps operators like +, -, * and / to special functions, such that e.g. a + b is equivalent to
a.__add__(b)
Regarding division in Python 2, there is by default only / which maps to __div__ and the result is dependent on the input types (e.g. int, float).
Python 2.2 introduced the __future__ feature division, which changed the division semantics the following way (TL;DR of PEP 238):
/ maps to __truediv__ which must "return a reasonable approximation of
the mathematical result of the division" (quote from PEP 238)
// maps to __floordiv__, which should return the floored result of /
With Python 3.0, the changes of PEP 238 became the default behaviour and there is no more special method __div__ in Python's object model.
If you want to use the same code in Python 2 and Python 3 use
from __future__ import division
and stick to the PEP 238 semantics of / and //.

Reversing a number failed in python [duplicate]

Consider this division in Python 3:
>>> 2/2
1.0
Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast?
In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding down to 0? for the opposite, 2.x-specific problem.
Take a look at PEP-238: Changing the Division Operator
The // operator will be available to request floor division unambiguously.
Oops, immediately found 2//2. This will output an int rather than a float.
Behavior of the division operator in Python 2.7 and Python 3
In Python 2.7: By default, division operator will return integer output.
To get the result in double, multiply the dividend or divisor by 1.0.
100/35 => 2 # Expected is 2.857142857142857
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857
In Python 3
// => used for integer output
/ => used for double output
100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0 # Floating-point result if the divisor or dividend is real
The accepted answer already mentions PEP 238. I just want to add a quick look behind the scenes for those interested in what's going on without reading the whole PEP.
Python maps operators like +, -, * and / to special functions, such that e.g. a + b is equivalent to
a.__add__(b)
Regarding division in Python 2, there is by default only / which maps to __div__ and the result is dependent on the input types (e.g. int, float).
Python 2.2 introduced the __future__ feature division, which changed the division semantics the following way (TL;DR of PEP 238):
/ maps to __truediv__ which must "return a reasonable approximation of
the mathematical result of the division" (quote from PEP 238)
// maps to __floordiv__, which should return the floored result of /
With Python 3.0, the changes of PEP 238 became the default behaviour and there is no more special method __div__ in Python's object model.
If you want to use the same code in Python 2 and Python 3 use
from __future__ import division
and stick to the PEP 238 semantics of / and //.

Works in Python2 but not Python3; converting binary to decimal [duplicate]

Consider this division in Python 3:
>>> 2/2
1.0
Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast?
In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding down to 0? for the opposite, 2.x-specific problem.
Take a look at PEP-238: Changing the Division Operator
The // operator will be available to request floor division unambiguously.
Oops, immediately found 2//2. This will output an int rather than a float.
Behavior of the division operator in Python 2.7 and Python 3
In Python 2.7: By default, division operator will return integer output.
To get the result in double, multiply the dividend or divisor by 1.0.
100/35 => 2 # Expected is 2.857142857142857
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857
In Python 3
// => used for integer output
/ => used for double output
100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0 # Floating-point result if the divisor or dividend is real
The accepted answer already mentions PEP 238. I just want to add a quick look behind the scenes for those interested in what's going on without reading the whole PEP.
Python maps operators like +, -, * and / to special functions, such that e.g. a + b is equivalent to
a.__add__(b)
Regarding division in Python 2, there is by default only / which maps to __div__ and the result is dependent on the input types (e.g. int, float).
Python 2.2 introduced the __future__ feature division, which changed the division semantics the following way (TL;DR of PEP 238):
/ maps to __truediv__ which must "return a reasonable approximation of
the mathematical result of the division" (quote from PEP 238)
// maps to __floordiv__, which should return the floored result of /
With Python 3.0, the changes of PEP 238 became the default behaviour and there is no more special method __div__ in Python's object model.
If you want to use the same code in Python 2 and Python 3 use
from __future__ import division
and stick to the PEP 238 semantics of / and //.

Why python and python3 are giving different arithmetic results? [duplicate]

Consider this division in Python 3:
>>> 2/2
1.0
Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast?
In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding down to 0? for the opposite, 2.x-specific problem.
Take a look at PEP-238: Changing the Division Operator
The // operator will be available to request floor division unambiguously.
Oops, immediately found 2//2. This will output an int rather than a float.
Behavior of the division operator in Python 2.7 and Python 3
In Python 2.7: By default, division operator will return integer output.
To get the result in double, multiply the dividend or divisor by 1.0.
100/35 => 2 # Expected is 2.857142857142857
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857
In Python 3
// => used for integer output
/ => used for double output
100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0 # Floating-point result if the divisor or dividend is real
The accepted answer already mentions PEP 238. I just want to add a quick look behind the scenes for those interested in what's going on without reading the whole PEP.
Python maps operators like +, -, * and / to special functions, such that e.g. a + b is equivalent to
a.__add__(b)
Regarding division in Python 2, there is by default only / which maps to __div__ and the result is dependent on the input types (e.g. int, float).
Python 2.2 introduced the __future__ feature division, which changed the division semantics the following way (TL;DR of PEP 238):
/ maps to __truediv__ which must "return a reasonable approximation of
the mathematical result of the division" (quote from PEP 238)
// maps to __floordiv__, which should return the floored result of /
With Python 3.0, the changes of PEP 238 became the default behaviour and there is no more special method __div__ in Python's object model.
If you want to use the same code in Python 2 and Python 3 use
from __future__ import division
and stick to the PEP 238 semantics of / and //.

Get ZeroDivisionError: float division in python

In the code below: highly simplified. I get ZeroDivisionError: float division
Any value below one gives errors. Other times 5/365 gives the error.
How do I fix?
import math
def top( t):
return ((.3 / 2) * t) / (.3 * math.sqrt(t))
t = 365/365
top= top(t)
print (top)
The problem is here:
t = 365/365
You are dividing two integers, so python is using integer division. In integer division, the quotient is rounded down. For example, 364/365 would be equal to 0. (365/365 works because it is equal to 1, which is still 1 rounded down.)
Instead, use float division, like so.
t = 365.0/365.0
In addition to cheeken's answer, you can put the following at the top of your modules:
from __future__ import division
Doing so will make the division operator work the way you want it to i.e always perform a (close approximation of) true mathematical division. The default behaviour of the division operator (where it performs truncating integer division if the arguments happen to be bound to integers) was inherited from C, but it was eventually realised that it was not a great fit for a dynamically typed language like Python. In Python 3, this no longer happens.
In my Python 2 modules, I almost always import division from __future__, so that I can't get caught out by accidentally passing integers to a division operation I don't expect to truncate.
It's worth noting that from __future__ import ... statements have to be the very first thing in your module (I think you can have comments and a docstring before it, nothing else). It's not really a normal import statement, even though it looks like one; it actually changes the way the Python interpreter reads your code, so it can't wait until runtime to be exectuted like a normal import statement. Also remember that import __future__ does not have any of the magic effects of from __future__ import ....
Try this:
exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
A ZeroDivisionError is encountered when you try to divide by zero.

Categories

Resources