0

I have a couple of Python lists (lets say 'x' and 'y'), in which the numbers are all 'Decimals', using the 'decimal' module. I need this because the numbers are precise to many decimal places.

I then need to take the gradient of a plot of y versus x using

slope,intercept = np.polyfit(x,y,1)

but I get the following error:

TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'

I assume that the polyfit doesn't like me using decimals in this way? Is there any way around it?

Thank you!

6
  • It doesnt make sense to use the decimal module in this context, switch to floats
    – fraxel
    Jan 18, 2013 at 1:04
  • but floats don't have enough decimal places for the precision I need... Jan 18, 2013 at 1:07
  • Joining @fraxel in suggesting floats, since the result is a parameter estimate with surely enough uncertainty.
    – s.bandara
    Jan 18, 2013 at 1:07
  • ... but implementing your own linear fitting procedure with decimals won't be difficult either.
    – s.bandara
    Jan 18, 2013 at 1:09
  • 1
    if all the numbers have the same value for the first 10 decimal places (or whatever), all you need to do is rescale your data. ie. 2.3453453453457 and 2.3453453453459 become 7 and 9 etc. Thats the easy way to solve the problem.
    – fraxel
    Jan 18, 2013 at 2:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.