gpt4 book ai didi

iphone - Objective C 中的浮点值除法错误

转载 作者:行者123 更新时间:2023-11-28 18:42:31 25 4
gpt4 key购买 nike

当我运行这段代码时:

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

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

在哪里,您可以看到在输出的最后一个 block (int)(f/tickIncrement) : 1 必须返回 2,但它返回 1。

为什么会这样?

最佳答案

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

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

与此同时,尝试简单地使用:

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

关于iphone - Objective C 中的浮点值除法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9513396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com