0
UInt64 intValue = 999999900;
float tt = intValue;
NSLog(@"float tt = %f", tt);

the output result is "float tt = 999999872", as you can see the UInt64 convert to float lose something, the Max float is bigger than 999999900, so I think the value 999999900 can be cast to float, so my question is why lose 28 in iOS?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What about converting to a `NSNumber` first and then getting the `floatValue`of it? Something like `float tt = [@(intValue) floatValue];` (not sure about this particular syntax for `UInt64`). Maybe `float tt = [[NSNumber numberWithUnsignedLongLong:intValue] floatValue];`. According to the next question, that's the "correct" way of handling `UInt64` for getting a `NSNumber` instance: http://stackoverflow.com/questions/3746028/converting-uint64-t-to-nsnumbers – Alejandro Iván Mar 13 '16 at 04:34

2 Answers2

1

float has a limited amount of precision. It's not the size of the number, it's the number of significant digits (9 in this case).

Use double instead of float to get more precision.

UInt64 intValue = 999999900;
double tt = intValue;
NSLog(@"double tt = %f", tt);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks for your answer, an i know the solution, but I don't know how it happens. This is my puzzle, the Max float is bigger than 999999900, so i think a float param can express the the number 999999900, it should not lose 28, and your answer means the float can not express all numbers which small than MAX float? so if I want to use float and assignment a number to float I don't know whether it will lose something? – user2331687 Mar 15 '16 at 01:09
  • You misunderstand. A `float` can store a value up to `FLOAT_MAX`. But it can only keep roughly 7 digits of precision. For example, it can store `1,000,000,000` just fine. It can store `1,234,567,000` just fine. But it can't store `1,234,123,400` since that has too much precision for `float`. – rmaddy Mar 15 '16 at 01:15
  • So, we don't know what happen when assignment a number to a float, and how can we use this data type? Because I assignment a number to a float, it will changed! – user2331687 Mar 15 '16 at 01:24
  • It will change if you attempt to use a number with too many digits of precision. Use `double` unless you have a specific need to use `float`. Or use an integer type if you don't need any decimals. – rmaddy Mar 15 '16 at 01:26
0

Why are you using float and not double? Has nobody told you that float has very limited precision (around 7 digits) while double has about 15 digits?

As a rule, you should NEVER use float instead of double unless you yourself can give a reasonable explanation why float would be more suitable than double.

So your question is: Why do I lose precision when I intentionally throw away 8 digits and precision, and what can I do? The answer is very simple: You lost precision because you threw it away yourself. Use double instead of float.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Thanks for your answer, an i know the solution, but I don't know how it happens. This is my puzzle, the Max float is bigger than 999999900, so i think a float param can express the the number 999999900, it should not lose 28, and your answer means the float can not express all numbers which small than MAX float? so if I want to use float and assignment a number to float I don't know whether it will lose something? – user2331687 Mar 15 '16 at 01:10