简体   繁体   English

传递给objective-c中的方法时浮点值更改

[英]float value changes when passed to a method in objective-c

Ok here goes my situation: 好的,这就是我的情况:

I have a core-data generated class Quantity which has one of its attributes 'Value', marked as a float. 我有一个核心数据生成类Quantity,它的一个属性'Value',标记为float。

There's a convenience method in the Quantity class which helps me set this property: Quantity类中有一个方便的方法,它可以帮助我设置这个属性:

- (void)setValueValue:(float)value_ {

    [self setValue:[NSNumber numberWithFloat:value_]];

}

Now when I set try set this value from a Controller like this, 现在,当我设置尝试从这样的控制器设置此值,

Quantity *q = (Quantity *) [NSEntityDescription insertNewObjectForEntityForName:@"Quantity" inManagedObjectContext:self.managedObjectContext];

float qv = 100.0f;

[q setValueValue:qv];

the value gets set as 1.40129846e-43 该值设置为1.40129846e-43

I put breakpoints in both locations above, and while the tooltip correctly shows the value as 100.0 in the Controller class, it shows the incorrect value in the setter method. 我在上面的两个位置都放置了断点,虽然工具提示在Controller类中正确显示值为100.0,但它在setter方法中显示的值不正确。

And ultimately, the value that gets stored in the object and the db is the incorrect value. 最终,存储在对象和db中的值是不正确的值。

Any clues as to what's happening here?? 关于这里发生了什么的任何线索?

1.40129846e-43 is a very interesting number, and tells us what's happening here. 1.40129846e-43是一个非常有趣的数字,并告诉我们这里发生了什么。

Specifically: 特别:

1.40129846e-43 = 100 * 2^-149

More simply, the value that you're observing is what you get if you interpret the integer value 100 as the encoding of a float . 更简单地说,如果将整数值100解释为float的编码,那么您正在观察的值就是您所获得的值。

What is most likely happening is that you haven't included the header that declares the setValueValue:(float) method, or that the declaration is otherwise not in scope at the location from which you are calling the method. 最可能发生的是您没有包含声明setValueValue:(float)方法的标头,或者声明在您调用方法的位置不在范围内。 This results in the compiler assuming that the method takes an integer argument instead of a floating-point argument. 这导致编译器假定该方法采用整数参数而不是浮点参数。

Thus, the caller converts your 100.f value into the integer 100 , which is then interpreted by the function as the encoding of the floating-point number you are observing. 因此,调用者将您的100.f值转换为整数100 ,然后由函数将其解释为您正在观察的浮点数的编码。

The other possibility is that a similar type impedance mismatch is happening somewhere in your logging code. 另一种可能性是在日志记录代码中的某处发生类似的阻抗不匹配。

TLDR: make sure you declare your functions before you use them in C-based languages. TLDR:确保在使用基于C语言的语言之前声明函数。

There is nothing wrong with the code you posted beyond the possibility of a naming collision. 除了发生命名冲突的可能性之外,您发布的代码没有任何问题。 The dynamic accessor will have the name setValue: so setValueValue: runs the risk of confusing the runtime. 动态访问器将具有名称setValue:因此setValueValue:存在混淆运行时的风险。

However, most likely, you are looking at a logging error ie the data is fine you are just displaying it incorrectly. 但是,很可能,您正在查看日志记录错误,即数据很好,您只是错误地显示它。 If you use an NSLog with the wrong type in the format you will get very large, nonsensical outputs. 如果您使用格式错误的NSLog,您将获得非常大的无意义输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM