java - Calculate the area of a regular polygon issue -


i trying write bit of code calculate area of regular polygon formula (sides*length^2)/4*tan(pi/sides) code looks like

double area = (nsides * math.pow(slength, 2)) /             4 * math.tan ((math.pi) / nsides); 

i expected nsides == 5 , slength == 6.5, should 72.69017017488385 outputs 38.370527260283126 instead.

it's called order of operations. java documentation says unclearly here:

the operators in following table listed according precedence order

the problem division , multiplication have equal precedence.

operators on same line have equal precedence. ... binary operators except assignment operators evaluated left right;

so given have equal precedence, division 4 evaluated before multiplication math.tan. need explicitly specify order inserting set of brackets:

double area = (nsides * math.pow(slength, 2)) /               (4 * math.tan ((math.pi) / nsides)); 

Comments