1

How I could find max two decimal precision vale that can be stored in float ?

From my understanding, In 32 bit float we have 24(23+1) for storing the number excluding exponent. is 2^24 is the max value we could store ?

Thanks in advance. Sriraman

mskfisher
  • 3,291
  • 4
  • 35
  • 48
user458236
  • 191
  • 3
  • 7
  • 1
    The limits of a `float` are given by `Float.MIN_VALUE` and `Float.MAX_VALUE`. What do you mean by "two decimal precision"? – Oliver Charlesworth Feb 18 '11 at 08:43
  • 1
    if you are thinking of storing a monetary value in a float (2 decimals places is where I'm coming from) please think again and use a more suitable datatype than either float or double – Gareth Davis Feb 18 '11 at 09:01

4 Answers4

2

2^24 is the largest integer you can store accurately. The largest two decimal places value you can store without loss of precision. 2^24/100.

Note: even 0.1 & 0.01 cannot be stored accurately but with rounding you can get this value without error. So taking the question literally, the largest value is 0.00. ;)

The largest value with two digits of precision is close to Float.MAX_VALUE, but I don't think that is what you mean.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Nope, you can store `0.25` (and many other values) perfectly in a `float`, so `0.00` isn't the largest one. – Joachim Sauer Feb 18 '11 at 08:52
  • You can also store `Float.MAX_VALUE` accurately. Its the jeopardy of having to guess the question as well as the answer. Its a matter of interpretation. The most interesting answer is what is the largest value you can add 0.01 to without a precision error. i.e. what is the range of values you can safely store in a float if you need two decimal places. – Peter Lawrey Feb 18 '11 at 08:56
  • @Joachim Sauer: the original question implies that the "largest value" means all values (with a decimal precision of 2) from 0 to this largest value. Therefore 0.0 is the correct answer. – Robert Feb 18 '11 at 18:39
0

Information on the IEEE 754 single precision binary floating-point format can be found on wikipedia

I think there is no possibility to find such a number. You are talking about a number with two decimal digits in base 10, however you want to store this number as binary value.

While you are perfectly able to store the exact value of 1.00 in a 32-bit-float you won't be able to store 0.99.

Danilo Tommasina
  • 1,740
  • 11
  • 25
0

You cannot specify the precision in a float value. If you limit yourself to 2 decimal places, you don't get more space for something else in return.

user unknown
  • 35,537
  • 11
  • 75
  • 121
0

If you simply want to store numbers with exactly two decimal digits after the point, store 100 times the number as an integer (or long, depending on how big your numbers get), and add the point (or comma) on formatting (remove on parsing).

This is much easier to control.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210