与NSUInteger奇怪的行为 - 不能转换为浮动正确 [英] Odd behavior with NSUInteger - can't convert to float properly
问题描述
下面是我的情况。它的驾驶我坚果:
我有一个NSMutableArray与517的计数值我有一个double值我的倍增器。
双乘数= 0.1223;
double结果= [myArray的数] *乘数; // 63连(错了!)
在事实上,它应该是63.2291。如果我去:
双重结果= [myArray的数] * 0.1223; // 63.2291(右!)
或..
双重结果= 517 *乘数; // 63.2291(右!)
这是否任何意义,任何人吗?
附录:
这是我的实际功能:
- (双)getValueForPercentage:(双)percVal
{
INT adjustedCount = [originalData数] - 1;
双打决赛= percVal *(双)adjustedCount;
最终回报;
}
我的从不的得到任何的数字超出了小数点当我这样做。它的确实的工作,但是如果我摆脱了-1,一拉:
- (双)getValueForPercentage:(双)percVal
{
INT adjustedCount = [originalData计数]
双打决赛= percVal *(双)adjustedCount;
最终回报;
}
当然,我需要有-1。
二编:
我注意到另一个有趣的事情是,如果我通过一个硬codeD编号给这个函数它工作正常,但如果我通过,我需要使用双重价值,它失败:
INT pointCount = [srcData getDayCount]的for(int i = 0; I< pointCount;我++){
双进度=(双)I /(双)(pointCount - 1);
双SATV = [srcData getValueForPercentage:进展]
// SATV是始终没有超越小数位数的任何一个数字
}
好吧,当我开始有这些问题,我四处张望了一下,发现没有任何理由或解释。
我现在要做的就是让一切都成为一个NSNumber,然后调用的doubleValue就可以了。这应该得到你正在寻找的结果:
的NSNumber * pointCount = [NSNumber的numberWithUnsignedInt:[srcData getDayCount]];
为(NSInteger的I = 0; I&下; [pointCount的intValue];我++){
*的NSNumber数= [NSNumber的numberWithInt:我]。
双进度= [计数的doubleValue] / [pointCount的doubleValue] - 1.0;
双SATV = [srcData getValueForPercentage:进展]
// SATV是始终没有超越小数位数的任何一个数字
}
希望它帮助。
Here is my situation. Its driving me nuts:
I have an NSMutableArray with a count value of 517. I have a double value that is my multiplier.
double multiplier = 0.1223;
double result = [myArray count] * multiplier; // 63 even (wrong!)
In fact it should be 63.2291. If I go:
double result = [myArray count] * 0.1223; // 63.2291 (right!)
or..
double result = 517 * multiplier; // 63.2291 (right!)
Does this make any sense to anyone?
Addendum:
here is my actual function:
- (double) getValueForPercentage:(double)percVal
{
int adjustedCount = [originalData count] - 1;
double final = percVal * (double)adjustedCount;
return final;
}
I never get any digits beyond the decimal point when I do this. It does however work if I get rid of the "-1", a-la:
- (double) getValueForPercentage:(double)percVal
{
int adjustedCount = [originalData count];
double final = percVal * (double)adjustedCount;
return final;
}
Of course, I need to have the -1.
Second addendum:
Another interesting thing I noted was, if I pass a hard-coded number to this function it works fine, but if I pass the double value that I need to use, it fails:
int pointCount = [srcData getDayCount];
for (int i = 0; i < pointCount; i++) {
double progress = (double)i/(double)(pointCount - 1);
double satv = [srcData getValueForPercentage:progress];
// satv is always a number without any digits beyond the decimal
}
Well, when I started to have these issues i looked around a bit and found no reason or explanation.
What I do now is make everything become an NSNumber and then call doubleValue on it. This should yield the results you're looking for:
NSNumber * pointCount = [NSNumber numberWithUnsignedInt: [srcData getDayCount]];
for (NSInteger i = 0; i < [pointCount intValue]; i++) {
NSNumber * count = [ NSNumber numberWithInt: i ];
double progress = [count doubleValue]/[pointCount doubleValue] - 1.0;
double satv = [srcData getValueForPercentage:progress];
// satv is always a number without any digits beyond the decimal
}
Hope it helps.
这篇关于与NSUInteger奇怪的行为 - 不能转换为浮动正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!