Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top