Iphone 目标C中的浮点值分割错误

Iphone 目标C中的浮点值分割错误,iphone,ios,ipad,ios4,Iphone,Ios,Ipad,Ios4,当我运行这段代码时: float angle = startAngle; int i = 0; for (float f = self.minNumber; f <= self.maxNumber; f += minorTickIncrement) { points[i++] = CGPointMake(centerX + cos(angle) * (radius - tickInset), centerY + sin(angle) * (radius - tickInset))

当我运行这段代码时:

float angle = startAngle;
int i = 0;

for (float f = self.minNumber; f <= self.maxNumber; f += minorTickIncrement) {
    points[i++] = CGPointMake(centerX + cos(angle) * (radius - tickInset), centerY + sin(angle) * (radius - tickInset));

    CGFloat myTickLength;

    NSLog(@"f : %f",f);
    NSLog(@"tickIncrement : %f",tickIncrement);
    NSLog(@"f / tickIncrement : %f",f / tickIncrement);
    NSLog(@"(int)(f / tickIncrement) : %d",(int)(f / tickIncrement));
    NSLog(@"(f / tickIncrement - (int)(f / tickIncrement)) : %f",(f / tickIncrement - (int)(f / tickIncrement)));
    NSLog(@"fabs((f / tickIncrement - (int)(f / tickIncrement))) : %f",fabs((f / tickIncrement - (int)(f / tickIncrement))));

    float checkValue = (f / tickIncrement - (int)(f / tickIncrement));

    if (fabs(checkValue) < 0.05) { // if is major tick
        myTickLength = self.tickLength;
        NSString *string = [[NSString alloc] initWithFormat:@"%0.1f", f];
        float textWidth = textHeight * [string length] / 2;
        CGContextShowTextAtPoint(ctx, centerX + cos(angle) * (radius - textInset) - textWidth / 2.0, centerY + sin(angle) * (radius - textInset) + textHeight / 4.0, [string UTF8String], [string length]);
        [string release];

    } else {
        myTickLength = self.minorTickLength;
    }

    points[i++] = CGPointMake(centerX + cos(angle) * (radius - myTickLength - tickInset), centerY + sin(angle) * (radius - myTickLength - tickInset));

    angle += minorTickAngleIncrement;
}
其中,您可以看到在输出的最后一块
(int)(f/tickIncrement):1必须返回2,但它返回1


为什么会发生这种情况?

最可能的情况是,您遇到了浮点表示问题。
尝试使用double,但要知道它不会解决根本问题,您看到的
2.0
实际上是
1.99999…
它只是格式化为显示
2.0

阅读“

同时,试着简单地使用:

(int)(f / tickIncrement + tickIncrement / 5.0)

最有可能的是,您遇到了浮点表示问题。
尝试使用double,但要知道它不会解决根本问题,您看到的
2.0
实际上是
1.99999…
它只是格式化为显示
2.0

阅读“

同时,试着简单地使用:

(int)(f / tickIncrement + tickIncrement / 5.0)