How to find out the format of a float?
I'm working with someone else's code, and there is a float with some unusual qualities.
If I output the float using:
NSLog(@"theFloat: %f", record.theFloat);
I get:
theFloat: 0.000000
However, if I use:
NSLog(@"(int)theFloat = %i", (int) record.theFloat);
I get:
(int)theFloat: 71411232
How do I discover the real format and value of theFloat? I k开发者_StackOverflownow that it should contain a large number.
Incidentally, the Record class which contains the float propertizes it in such a way:
@property (assign) float* theFloat;
There is also floatLength:
@property (assign) int floatLength;
And has this method, which seems to indicate that the float is of variable length (?):
- (void) copyFloat:(float*)theF ofLength:(int)len
{
float *floatcopy = malloc(len*sizeof(float));
memcpy(floatcopy, theF, len*sizeof(float));
self.theFloat = floatcopy;
}
Your field, theFloat, is not a primitive type but a pointer. float* means it is a pointer to a float. You need to dereference the field in order to get it's value.
Use *theFloat to get the actual value.
Also, I suggest you review your format specifiers.
http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
The address in memory where the float is stored is given by : (int)theFloat: 71411232 You probably want to use something like :
NSLog(@"theFloat = %f", (*record.theFloat));
Which will dereference the pointer and give you the actual data.
精彩评论