在为视图框架分配CGRect
时,我遇到了一个非常奇怪的浮动不精确。我可以缩小再现步骤:
CGRect frame = self.label.frame;
frame.origin.x = 13.f;
NSLog(@"boundingRect = %@", NSStringFromCGRect(boundingRect));
frame.size = boundingRect.size;
NSLog(@"Before assignment, x=%f", frame.origin.x);
self.label.frame = frame;
NSLog(@"After assignment, x=%f", self.label.frame.origin.x);
输出:
boundingRect = {{0, 0}, {116.072, 13.123}}
Before assignment, x=13.000000
After assignment, x=12.999996 <= ???
CGRect frame = self.label.frame;
frame.origin.x = 13.f;
//NSLog(@"boundingRect = %@", NSStringFromCGRect(boundingRect));
//frame.size = boundingRect.size;
NSLog(@"Before assignment, x=%f", frame.origin.x);
self.label.frame = frame;
NSLog(@"After assignment, x=%f", self.label.frame.origin.x);
输出:
Before assignment 13.000000
After assignment 13.000000
知道这里发生了什么吗?