开发者

Objective c - float returning wrong value

I'm using objective c and trying to output a value from a function. Apparently I am doing something wrong bec开发者_如何学JAVAause I'm receiving an incorrect value.

This is my code:

-(float) getAngleBetween {
float num = 0.0;
return num;

}

and I'm calling it as follows:

float *theAngleBetween = [self getAngleBetween];
NSLog(@"Angle.. = %f", theAngleBetween);

Any help please?


float theAngleBetween = [self getAngleBetween];
//   ^

There should be no *.

Since you are returning a float, the receiver should have type float as well. float* means a pointer to float, which is entirely different from float.

BTW, make sure you declare -(float)getAngleBetween; before you call [self getAngleBetween]. Put it in the @interface. If it is not declared before, the method will be assumed to have the type -(id)getAngleBetween;. On x86 returning a id and a float use different API (objc_msgSend vs objc_msgSend_fpret), which may be the cause of wrong result.


You should have:

float theAngleBetween = [self getAngleBetween];

Get rid of the *, that's for objects only, float is a primitive data type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜