Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

Aris Pap picture Aris Pap · Dec 14, 2015 · Viewed 76k times · Source

I wrote a simple program which approximates the evaluation of a definite integral using numerical integration. However, I am stumped when it comes to why I am getting the error in the title. Keep in mind that I haven't touched python in one and a half years so it might be something incredibly obvious that I'm missing, however I'd still be grateful if you could help me :) Here is the code:

import math
def f(x):
    f=math.sqrt(1+(6*x+4)^2)
    return f


lbound=int(input("Input lower bound for the integral"))
ubound=int(input("Input upper bound for the integral"))
n=int(input("Input number of intervals"))
dx=((ubound-lbound)/n)
integral=0
for i in range(1,n):
    integral=integral+dx*f(i*dx)

print (integral)

Here is the full error report IDLE gives me when trying to run the code:

Traceback (most recent call last):
  File "C:\Users\******\Desktop\integrals.py", line 13, in <module>
    integral=integral+dx*f(n*dx)
  File "C:\Users\******\Desktop\integrals.py", line 3, in f
    f=math.sqrt(1+(6*x+4)^2)
TypeError: unsupported operand type(s) for ^: 'float' and 'int'

Answer

ilyas patanam picture ilyas patanam · Dec 14, 2015

When trying to raise to a power use the operand use ** and not ^.

f=math.sqrt(1+(6*x+4)**2)