0

This is a continuation of the question asked here: Exponential of large negative numbers. The reason I want to use decimal is that np.exp(-large_numbers) return 0.0. Using Eric's answer to this question: https://stackoverflow.com/a/7771210/6943476, I tried converting my np.array to decimal but I still get the data-type
numpy.ndarray.

Coding ability: Beginner

My final goal is to have a function return a damped sinusoid:

def waveform(mass_sm, amplitude, F, Q, phi, Time):
    w = F/mass_sm

    print type(Time)
    Ntime = np.array(Time,dtype=np.dtype(decimal.Decimal))
    priny type(Ntime)

    return mass_sm * amplitude * np.sin(w * Ntime + phi) * np.exp(-w * Ntime/ (2 * Q))

It tried to debug the code by breaking down the return function into the sine, and then the exponential:

def waveform(mass_sm, amplitude, F, Q, phi, Time):
    w = F/mass_sm

    print type(Time)
    Ntime = np.array(Time,dtype=np.dtype(decimal.Decimal))
    priny type(Ntime)

    return mass_sm * amplitude * np.sin(w * Ntime + phi)

Output:

<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
AttributeError: 'float' object has no attribute 'sin'

For the exponential part, I get the (expected) error that:

AttributeError: 'float' object has no attribute 'exp'

I have also tried to convert the elements of my array to decimal by doing this:

    Ntime =  [decimal.Decimal(i) for i in Time]

This time, getting this error for sine:

TypeError: can't multiply sequence by non-int of type 'float'

and this error for exp:

TypeError: bad operand type for unary -: 'list'

Community
  • 1
  • 1
Daniel George
  • 23
  • 1
  • 9
  • 1
    Time is a reserved keyword. So maybe the erractic behavior is coming from that. Try to rename the variables and see if it works. – Mikael Mar 29 '17 at 18:22
  • I still get an error while using the Decimal module. print Decimal(np.exp(1))**-28000.2 leads to TypeError: unsupported operand type(s) for ** or pow(): 'Decimal' and 'float'. It seems decimals cannot handle floats? – Daniel George Mar 29 '17 at 22:00

0 Answers0