Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top