Reputation: 1200
I have a range of negative values : -30 to -80 Let's say
array_values = [-30, -31, -32, -33, -34, -35, -36, -37, -38, -39, -40, -41, -42, -43, -44, -45, -46, -47, -48, -49, -50, -51, -52, -53, -54, -55, -56, -57, -58, -59, -60, -61, -62, -63, -64, -65, -66, -67, -68, -69, -70, -71, -72, -73, -74, -75, -76, -77, -78, -79, -80]
I'm evaluating this expression : |(-k).exp(array_values[idx]/k)|
for idx in array_values
k=35
The problem I'm having is that, there's no smooth exponential curve I'm getting after plotting this expression.
So I thought I'd check with just exp(array_values[idx]/k)
Here are a bunch of values I get from the calculator and python(both math.exp() and numpy.exp() respectively)
exp(-30/35): Calculator: 0.42437284567 Python:0.36787944117144233
exp(-31/35): 0.4124194827 0.36787944117144233
exp(-32/35): 0.40080281159 0.36787944117144233
exp(-33/35): 0.38951334871 0.36787944117144233
exp(-34/35): 0.38754187754 0.36787944117144233
Am I doing something wrong, or is there a bug in the function? My code line for this is
expos = [math.exp(-i/35) for i in array_values]
Upvotes: 0
Views: 3777
Reputation:
As you can check, 0.36787944117144233
is just e^(-1)
, and you can infer that you are passing the floor of the fractional arguments (because you are performing an integer division).
Nose-on-your-face bugs of such a kind in fundamental functions are highly unlikely.
Upvotes: 1
Reputation: 717
You can do:
expos = [math.exp(float(-i)/35) for i in array_values]
The integer division returns integer values for example:
>>> 1/2
0
but a float value:
>>> 1.0/2
0.5
Upvotes: 0
Reputation: 1021
Dividing int
with int
results in int
. So
array_values = [-30, -31, -32, -33, -34]
test = [-i/35 for i in array_values]
>>> [0, 0, 0, 0, 0]
expos = [math.exp(-i/35) for i in array_values]
>>> [1.0, 1.0, 1.0, 1.0, 1.0]
If you convert one of the numbers in the division to float
will produce the expected results.
test = [float(-i)/35 for i in array_values]
>>> [0.8571428571428571, 0.8857142857142857, 0.9142857142857143, 0.9428571428571428, 0.9714285714285714]
expos = [math.exp(-float(i)/35) for i in array_values]
#or expos = [math.exp(-i/35.0) for i in array_values]
>>> [2.3564184423836605, 2.4247157128777834, 2.494992477791509, 2.5673061098152665, 2.6417156445012626]
Upvotes: 2
Reputation: 59444
You are converting your values to positive by putting a minus in front of an already negative value. You should also divide it to 35.0 so that it returns a float
instead of an int
. This works:
import math
array_values = [-30, -31, -32, -33, -34, -35, -36]
expos = [math.exp(i/35.0) for i in array_values]
print expos
result:
[0.42437284567695, 0.4124194827001579, 0.40080281159210923, 0.3895133487108618, 0.37854187754140095, 0.36787944117144233, 0.35751733497916927]
Upvotes: 1