“算法”一书中的这两个代码片段似乎在做一些数学的东西。代码我没有理解,但我想知道正在进行的数学计算。
1
double t=c;
while(Math.abs(t-c/t) > 1e-15*t)
t=(c/t+t)/2.0;
2
for(int i=0;i<=N;i++)
StdOut.println(2*Math.Pi*i/N);
// prints the value of circumference of a circle of radius 0, 1/N,2/N .. 1 ?
答案 0 :(得分:2)
第一个是approximating square roots的标准算法。它可以被视为牛顿方法的一个特例,但在牛顿之前已知数千年,有时被称为巴比伦算法。第二个真的应该是不言自明的,因为它被评论并且只是let newAttributedString = NSMutableAttributedString(attributedString: label.attributedText!)
// Enumerate through all the font ranges
newAttributedString.enumerateAttribute(NSAttributedStringKey.font, in: NSMakeRange(0, newAttributedString.length), options: [])
{
value, range, stop in
guard let currentFont = value as? UIFont else {
return
}
// An NSFontDescriptor describes the attributes of a font: family name, face name, point size, etc.
// Here we describe the replacement font as coming from the "Hoefler Text" family
let fontDescriptor = currentFont.fontDescriptor.addingAttributes([UIFontDescriptor.AttributeName.family: "Hoefler Text"])
// Ask the OS for an actual font that most closely matches the description above
if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptor.AttributeName.family]).first {
let newFont = UIFont(descriptor: newFontDescriptor, size: currentFont.pointSize)
newAttributedString.addAttributes([NSAttributedStringKey.font: newFont], range: range)
}
}
label.attributedText = newAttributedString
定义的直接应用,作为圆周直径的比例,是半径的两倍。