Search code examples
pythonmathnumpyexponential

python exponential function - incorrect exponential values


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]

Solution

  • 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]