# Mean distance to the centroid of a regular n-gon of circumradius R.
import numberdb.sage as numberdb
from sage.rings.real_arb import RealBallField
def mean_regular_polygon(n, R=1, digits=100):
if n < 3:
raise ValueError("n must be at least 3")
F = RealBallField(numberdb.bits(digits, losing=80))
theta = F.pi()/n
h = F(R)*theta.cos()
return h/3*(1/theta.cos()
+(1/theta.cos()+theta.tan()).log()/theta.tan())
print(mean_regular_polygon(6))
The eight irrational entries are evaluated throughout in Arb ball arithmetic, using 80 guard bits beyond 100 significant digits. The largest measured ball radius is less than $4.3\times10^{-124}$, and every relative radius is less than $10^{-120}$. The five rational entries are returned exactly. The regular-polygon and rectangle closed forms are given in equation (23) and Examples 1–3 of [3]. The square and cube forms follow from equation (4) and Section 7 of [4].
Independent quadratures integrate the radial distance density for polygons and balls, polar coordinates for rectangles, and a face-pyramid decomposition for the cube. They are run at 130 and 170 decimal digits, after normalization and second-moment controls, and agree with every closed form to absolute tolerance $10^{-120}$. Scaling is checked for every region. These numerical quadratures are agreement checks; the certified error bounds come from Arb evaluation of the closed forms. Direct uniform sampling of 300000 points in each region also checks the centroid, second moment and mean. The attached checks can compare all independent values and certified balls with the intervals read from the database.