Current location - Training Enrollment Network - Mathematics courses - The difference between float and double in java
The difference between float and double in java
Float means that a single-precision floating-point number occupies 4 bytes in the machine and is described by 32-bit binary.

Double means that double-precision floating-point numbers occupy 8 bytes in the machine and are described by 64-bit binary.

Floating-point numbers are expressed in exponential form in machines, which are divided into four parts: number symbols, mantissa, exponential symbols and exponents.

The number symbol occupies 1 binary, indicating the sign of the number.

Exponential symbols occupy 1 binary, and represent the symbols of exponents.

The mantissa represents the significant digit 0 of floating-point number. 0. 0.xxxxxxx, but excluding leading 0 and dot.

The index holds the significant figures of the index.

How many digits the exponent occupies and how many digits the mantissa occupies are determined by the computer system.

It may be that the number sign plus mantissa accounts for 24 bits, and the exponent sign plus exponent accounts for 8 bits of floating point.

The number sign plus mantissa accounts for 48 bits, and the exponent sign plus exponent accounts for 16-bit-double.

Knowing the positions of these four parts, we can estimate the size range in binary and then convert it into decimal, that is, the numerical range of floating-point numbers.

For programmers, the difference between double and float lies in the high precision of double, the effective number of digits is 16, and the precision of float is 7. But double consumes twice as much memory as float, and the operation speed of double is much slower than float. In java language, the names of mathematical functions are different, so don't make mistakes, and don't use double precision when you can use single precision (to save memory and speed up the operation).