How many significant digits does float and double have in java?

Does float have 32 binary digits and double have 64 binary digits? The documentation was too complicated to understand.

Are all bits converted to significant digits? Or does the decimal point location take several bits?

+77
java floating-point
Nov 24
source share
6 answers

float : 32 bits (4 bytes), where 23 bits are used for the mantissa (about 7 decimal digits). For the exponent, 8 bits are used, so the float can "move" the decimal point to the right or left using these 8 bits. This avoids the accumulation of a large number of zeros in the mantissa, as in 0.0000003 (3 × 10 -7 ), or 3000000 (3 × 10 7 ). As a sign bit, 1 bit is used.

double : 64 bits (8 bytes), where 52 bits are used for the mantissa (about 16 decimal digits). For the exponent, 11 bits are used, and 1 bit is the sign bit.

Since we use binary code (only 0 and 1), one bit in the mantissa is implicitly equal to 1 (both float and double use this trick) when the number is non-zero.

Also, since everything is in binary (mantissa and exponent), conversions to decimal numbers are usually not accurate. Numbers, such as 0.5, 0.25, 0.75, 0.125, are exactly preserved, but 0.1 is not. As others have said, if you need to store cents exactly, do not use float or double, use int, long, BigInteger or BigDecimal.

Sources:

http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers

http://en.wikipedia.org/wiki/Binary64

http://en.wikipedia.org/wiki/Binary32

+94
Nov 24 '12 at 17:22
source share
— -

From the java specification :

The floating point types are float and double, which are conceptually associated with 32-bit and double-precision single-precision 64-bit values ​​and IEEE 754 operations, as specified in the IEEE Standard for Binary Floating-Point Arithmetic, ANSI / IEEE 754-1985 Standard (IEEE, New York).

How difficult it is to do anything with numbers without understanding the basics of IEEE754, here is another link .

It is important to understand that accuracy is uneven and that it is not an exact memory of numbers, as is done for integers.

Example:

double a = 0.3 - 0.1; System.out.println(a); 

prints

 0.19999999999999998 

If you need arbitrary precision (e.g. for financial purposes), you may need Big Decimal .

+17
Nov 24 '12 at 16:11
source share

The usual mathematical answer.

Understanding that a floating point number is implemented as some bits representing the exponent, and the rest, mainly for digits (in the binary system), has the following situation:

With a high figure, say 10²³, if the least significant bit changes, a big difference appears between two adjacent distinghuishable numbers. In addition, the ten-digit base point 2 makes many base numbers 10 only approximate; 1/5, 1/10 - infinite numbers.

In the general case: floating point numbers should not be used if you need signed digits. For monetary amounts with calculations, e, a, it is best to use BigDecimal .

For floating point physics doubles are adequate, floats almost never. In addition, the floating point part of the processors, the FPU, may even use a little more precession internally.

+7
Nov 24 '12 at 16:30
source share

Floating-point numbers are encoded using exponential form, something like m * b ^ e , that is, not like integers. The question you ask will make sense in the context of the number of fixed points . There are many fixed point arithmetic libraries .

As for floating point arithmetic: the number of decimal digits depends on the representation and system of numbers. For example, there are periodic numbers ( 0.33333 ) that do not have a finite representation in decimal, but have one in binary and vice versa.

It is also worth mentioning that floating point numbers up to a point have a difference greater than unity, i.e. value + 1 gives value , since value + 1 cannot be encoded using m * b ^ e , where m , b and e fixed in length. The same thing happens for values ​​less than 1, i.e. All possible code points do not have the same distance.

Because of this, there is no accuracy of exactly n digits, like fixed-point numbers, since not every number with n decimal digits is IEEE encoded.

There is an almost mandatory document that you should read after that that explains floating point numbers: What every computer scientist needs to know about floating point arithmetic .

+3
Nov 24 '12 at 16:26
source share

See Float.intBitsToFloat and Double.longBitsToDouble explanation of how bits correspond to floating point numbers. In particular, the bits of a normal float look something like this:

  s * 2^exp * 1.ABCDEFGHIJKLMNOPQRSTUVW 

where A ... W - 23 bits - 0s and 1s - represents a fraction in binary format - s +/- 1, represented by 0 or 1, respectively, and exp is a signed 8-bit integer.

+1
Nov 24 '12 at 16:33
source share

If we look at the minimum positive value of Float .

 Float.MIN_VALUE = 1.4E-45f; // 0.000000000000000000000000000000000000000000001401298464324817 

Thus, technically, a float can contain up to 60 values, so for ranges between (1 and -1) you probably want to use only 44 places.

For general purposes, I like to be between 9995.9995 and -9995.9995 looking at a minimum accuracy of 0.0001.

For pleasure, you can try:

System.out.println(new DecimalFormat("#####.#####").format(9996.9996f));

Result: 9997

If we look at the minimum positive value of Double .

 Double.MIN_Value = 4.9E-324; // 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000049 

Thus, technically, a double can contain up to 326 values, so for ranges between (1 and -1) you probably want to use only 324 places.

+1
May 18 '16 at 19:28
source share



All Articles