User
User

Reputation: 53

Calculate a simple expression in python

I want to calculate (π/4)^2-sin⁡(π/4) in python:

import math
a=((math.pi)^2/16)-math.sin(math.pi/4)
print(a)

It gives the error:

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

I also tried using

a=float(((math.pi)^2/16)-math.sin(math.pi/4))

But still doesn't work

Upvotes: 0

Views: 60

Answers (3)

WJS
WJS

Reputation: 40057

No sense in evaluating pi/4 twice.

import math
pi4 = math.pi/4
a = pi4**2 - math.sin(pi4)

Upvotes: 0

Erick Cruz
Erick Cruz

Reputation: 66

^ is not supported in python. Instead, you can use math.pow or **

math.pow(math.pi,2)

math.pi ** 2

Upvotes: 1

Richa Bhuwania
Richa Bhuwania

Reputation: 178

^ is not supported in python. Instead, you can use math.pow.

import math
a=(math.pow((math.pi),2)/16.0)-math.sin(math.pi/4)
print(a)

Upvotes: 1

Related Questions