The Wikipedia page on it is a good place to start.
To sum up:
float
is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the significand (or what follows from a scientific-notation number: 2.33728*1012; 33728 is the significand).
double
is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of significand.
By default, Java uses double
to represent its floating-point numerals (so a literal 3.14
is typed double
). It's also the data type that will give you a much larger number range, so I would strongly encourage its use over float
.
There may be certain libraries that actually force your usage of float
, but in general - unless you can guarantee that your result will be small enough to fit in float
's prescribed range, then it's best to opt with double
.
If you require accuracy - for instance, you can't have a decimal value that is inaccurate (like 1/10 + 2/10
), or you're doing anything with currency (for example, representing $10.33 in the system), then use a BigDecimal
, which can support an arbitrary amount of precision and handle situations like that elegantly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…