regina_fallangi
regina_fallangi

Reputation: 2198

Semantic error in Python. Mathematical mistake

I am trying to write a program in Python to calculate the area of a polygon. The formula is :

area = n * s * a / 2
a = s / 2 * tan(pi/n)

My code is:

import math

def area(s,n):
    a = s/(2* math.tan((math.pi/n)))
    b = (n*s*a)/2
    return b

The problem is that it calculates a total different thing. For example: area (2,5) = 3.63, when it is really 6.887.

Does anyone see a mistake?

Upvotes: 1

Views: 189

Answers (1)

Veedrac
Veedrac

Reputation: 60147

import math

def area(s,n):
    a = s/(2* math.tan((math.pi/n)))
    b = (n*s*a)/2
    return b

area(2, 5)
#>>> 6.881909602355868

def area(s,n):
    a = s/2* math.tan((math.pi/n))
    b = (n*s*a)/2
    return b

area(2, 5)
#>>> 3.6327126400268046

Really though, don't use single letter names. Making things unreadable does nobody any good.

If your mathematics doesn't make semantic sense, rewrite it so that it does. Your confusion is a testament to this.

Upvotes: 4

Related Questions