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

Question:

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******Desktopintegrals.py", line 13, in <module>
    integral=integral+dx*f(n*dx)
  File "C:Users******Desktopintegrals.py", line 3, in f
    f=math.sqrt(1+(6*x+4)^2)
TypeError: unsupported operand type(s) for ^: 'float' and 'int'
Asked By: Aris Pap

||

Answers:

Python’s exponentiation operator is **, not ^. ^ is bitwise XOR.

f=math.sqrt(1+(6*x+4)**2)
Answered By: ilyas patanam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.