3
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?

Community
  • 1
  • 1
daihovey
  • 3,485
  • 13
  • 66
  • 110

2 Answers2

5

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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • One day I will understand floats. Thanks, will try integers now. – daihovey Jun 22 '12 at 02:49
  • Ah, ints though are too small. – daihovey Jun 22 '12 at 02:51
  • So how do you minus 1 from a large NSNumber? – daihovey Jun 22 '12 at 03:07
  • 1
    @daidai: Use `longLongValue` to get the 64-bit number from NSNumber, and assign it to a `long long` variable, or `int64_t` variable (make it unsigned if it is some sort of ID: `unsigned long long` and `uint64_t`), then work on the variable as you would with an integer. – nhahtdh Jun 22 '12 at 03:17
0

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

inwit
  • 935
  • 6
  • 11