如何在java代码中使用这个等式?

如何在java代码中使用这个等式?,java,math,equation,Java,Math,Equation,这就是我所做的,但无论我做什么,我都会得到无穷: public double calcr(){ double cot = 1 / Math.tan(0); return .5 * sideLength * cot * (Math.PI / numSides); } 主要内容: RegularPolygon poly = new RegularPolygon(4, 10); System.out.println(poly.calcr()); 输出: Inifinity 我

这就是我所做的,但无论我做什么,我都会得到无穷:

 public double calcr(){
  double cot = 1 / Math.tan(0);
  return  .5 * sideLength * cot * (Math.PI / numSides);
}
主要内容:

RegularPolygon poly = new RegularPolygon(4, 10);   
System.out.println(poly.calcr());
输出:

Inifinity 
我做错了什么?

tan(0)
是0,所以这行

double cot = 1 / Math.tan(0);
cot
设置为
Infinity
。下面的计算也将计算到无穷大,如您所见

因为它看起来像是要计算
cot(pi/n)
,所以需要
1/Math.tan(Math.pi/n)
而不是使用
cot*(Math.pi/numSides)
cot
tan(0)
的值不正确,所以这一行是0

double cot = 1 / Math.tan(0);
cot
设置为
Infinity
。下面的计算也将计算到无穷大,如您所见


因为它看起来像是要计算
cot(pi/n)
,所以您需要
1/Math.tan(Math.pi/n)
而不是使用
cot*(Math.pi/numSides)
cot
的值不正确,问题是您需要这样做

double cot = 1 / Math.tan(0);
这将使
cot
成为
Infinity

你会想要:

double cot = 1 / Math.tan(Math.PI / numSides);
return .5 * sideLength * cot;
或者,在一行中:

return .5 * sideLength / Math.tan(Math.PI / numSides);

问题是你会

double cot = 1 / Math.tan(0);
这将使
cot
成为
Infinity

你会想要:

double cot = 1 / Math.tan(Math.PI / numSides);
return .5 * sideLength * cot;
或者,在一行中:

return .5 * sideLength / Math.tan(Math.PI / numSides);

但是我应该传递什么作为参数呢?因为要找到cot,它是tan的倒数,但我需要传入一些内容。@有人正在编辑,请给我一点时间。使用
1/Math.tan(Math.PI/n)
?那么作为参数传入什么呢?因为要找到cot是tan的倒数,但我需要传递一些信息。@编辑人员,请给我一秒钟。使用
1/Math.tan(Math.PI/n)
返回边长/(2.0*Math.tan(Math.PI/numSides))
返回边长/(2.0*Math.tan(Math.PI/numSides))