-1

Like the title says, I don't understand how Java's primitive data type long maximum and minimum values are smaller than a float's maximum and minimum. Despite the long being 64bit and float being 32bit.

What is going on?

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • Take a look at [this](https://stackoverflow.com/questions/15004944/max-value-of-integer). It is not a direct answer, but it contains the exact explanation. – campovski Sep 04 '17 at 22:21

2 Answers2

4

the reason is because a float using floating point precision. A Long can store a high number of precise digits, while I float can store a high value but without the same precision in its lower bits.

In a sense, a float stores values in scientific/exponential notation. thus a large value can be stored in a small number of bits. Think 2 x10^200, this is huge number but can be stored in a small number of bits.

adamM
  • 1,116
  • 10
  • 29
3

A 32-bit float isn't just a simple number, like a long; it's broken up into fields. The fields are:

  • 1 bit for the sign;
  • 8 bits for the exponent, which is a power of 2;
  • 23 bits for the mantissa.

The exponent field is encoded (usually, it's just an offset from the real exponent), but it represents exponents from -126 to 127. (That covers 254 of the possible 256 values; the other two are used for special purposes.) The mantissa represents a value that can be from 1 to a value just below 2 (actually 2 - 2-23). This means that the largest possible float is 2127 x (2 - 2-23), or almost 2128, which is larger than the largest possible long value which is 263-1.

More information:

https://en.wikipedia.org/wiki/IEEE_754

http://www.adambeneschan.com/How-Does-Floating-Point-Work/showfloat.php?floatvalue=340282346638528859811704183484516925440&floattype=float

ajb
  • 31,309
  • 3
  • 58
  • 84