浮点值除以目标C中的错误

时间:2012-03-01 09:11:06

标签: 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));

    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;
}

显示以下输出:

 f : 0.100000
 tickIncrement : 0.100000
 f / tickIncrement : 1.000000
 (int)(f / tickIncrement) : 1
 (f / tickIncrement - (int)(f / tickIncrement)) : 0.000000
  fabs((f / tickIncrement - (int)(f / tickIncrement))) : 0.000000

  f : 0.120000
 tickIncrement : 0.100000
 f / tickIncrement : 1.200000
 (int)(f / tickIncrement) : 1
 (f / tickIncrement - (int)(f / tickIncrement)) : 0.200000
 fabs((f / tickIncrement - (int)(f / tickIncrement))) : 0.200000

 f : 0.140000
 tickIncrement : 0.100000
 f / tickIncrement : 1.400000
 (int)(f / tickIncrement) : 1
 (f / tickIncrement - (int)(f / tickIncrement)) : 0.400000
 fabs((f / tickIncrement - (int)(f / tickIncrement))) : 0.400000

 f : 0.160000
 tickIncrement : 0.100000
 f / tickIncrement : 1.600000
 (int)(f / tickIncrement) : 1
 (f / tickIncrement - (int)(f / tickIncrement)) : 0.600000
 fabs((f / tickIncrement - (int)(f / tickIncrement))) : 0.600000

 f : 0.180000
 tickIncrement : 0.100000
 f / tickIncrement : 1.800000
 (int)(f / tickIncrement) : 1
 (f / tickIncrement - (int)(f / tickIncrement)) : 0.800000
 fabs((f / tickIncrement - (int)(f / tickIncrement))) : 0.800000

 f : 0.200000
 tickIncrement : 0.100000
 f / tickIncrement : 2.000000
 **(int)(f / tickIncrement) : 1**
 (f / tickIncrement - (int)(f / tickIncrement)) : 1.000000
 fabs((f / tickIncrement - (int)(f / tickIncrement))) : 1.000000

...................

在哪里,您可以看到在输出(int)(f / tickIncrement) : 1的最后一个块中必须返回2,而是返回1.

为什么会这样?

1 个答案:

答案 0 :(得分:5)

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

阅读“What Every Computer Scientist Should Know About Floating-Point Arithmetic

在此期间,请尝试使用:

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