Question

NSLog(@"original float value = %f", [anNSNumber floatValue]);
                 
float newFloat = [anNSNumber floatValue] - 1.0f;
                 
NSLog(@"newFloat = %f", newFloat);

Gives me

orginal float value = 215987480044765184.000000

newFloat = 215987480044765184.000000

How do you simply subtract 1 from a float?

Was it helpful?

Solution

No, you cannot simplify it further. Floating point number is different from integer.

Another thing to note is that float type can only contain up to 24 bit of precision (around 6-7 decimal digits of precision). The number you have there exceeds the precision available for float, and the subtraction amount is too small to affect in the 24 bit of precision, so the operation simply does not make any change to the result.

The problem is not fixed even if you do the operation in double, since the number even exceeds the 53 bit precision for double.

Depends on your requirement, a 64-bit integer type (long long type, or more specific int64_t) may be what you need. If such precision is necessary for even bigger number, basic type won't be sufficient. External library may be needed if you need very high precision.

OTHER TIPS

Why do you have on your 3rd line: NSLog(@"newFloat = %f", idOfLastTweet); instead of NSLog(@"newFloat = %f", newFloat); ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top