Explain the difference between float and double in java in detail

  • 2021-07-13 05:27:17
  • OfStack

float is a single-precision type, the precision is 8 significant digits, the value range is 10-38th power to 10 38th power, float takes up 4 bytes of storage space

double is a double-precision type with a precision of 17 significant digits and values ranging from-308 to 308 of 10. double takes up 8 bytes of storage space

When you don't declare it, the default decimal is double, so if you want to use float, you should add f after it

For example: float a=1. 3;

It will be indicated that double cannot be converted to float, which is a narrow conversion

If you want to decorate it with float, you should use float a = 1.3 f

Note that float is an 8-digit significant digit, and the 7th digit will produce 4 rounded 5 inputs

So if an float variable is defined as: float a = 1.32344435; Then the seventh place will produce 4 rounding and 5 entering (all those below 5 and 5 will be discarded)

1. The difference between the two definitions

1) float memory allocates 4 bytes, 32 bits, ranging from 10 ^-38 to 10 ^ 38 and-10 ^ 38 to-10 ^-38

Examples float x=123. 456f, y=2e20f; Note that float type definition must have "f" or "F" at the end of the data, in order to distinguish it from double

(2) double memory allocates 8 bytes, ranging from 10 ^-308 to 10 ^ 308 and-10 ^-308 to-10 ^-308

Examples double x=1234567.98, y=8980. 09d; You can have "d" at the end or not

2. Special attention should be paid to the arithmetic operation of two floating-point numbers

Problems with using +,-, *,% operators directly


 public class Test{
        public static void main(String args[]){
        System.out.println(0.05+0.01);
        System.out.println(1.0-0.42);
        System.out.println(4.015*100);
        System.out.println(123.3/100);
        }
    }

Results:

0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999

Reason:

First of all, we must discuss this problem from the computer itself. As we know, computers can't recognize any data except binary data. No matter what programming language we use and what compiling environment we work in, we must first translate the source program into binary machine code before it can be recognized by the computer. Take the situation mentioned above as an example, 2.4 in our source program is decimal, which cannot be directly recognized by the computer, so it should be compiled into binary first. But here's the problem. The binary representation of 2.4 is not the exact 2.4, but the closest binary representation is 2.39999999999999999. The reason is that floating-point numbers are composed of two parts: exponent and mantissa, which should not be difficult to understand if you know how to convert binary and decimal floating-point numbers. If floating-point numbers are involved in the process of this transformation, the transformation process will become unpredictable and irreversible. We have reason to believe that it is in this process that the precision is lost. As for why some floating-point calculations get accurate results, it should also happen that the binary and decimal calculations can be accurately converted. When outputting single floating-point data, it can be output correctly, such as


double d = 2.4;
System.out.println(d);

The output is 2.4, not 2.39999999999999999. That is to say, when floating-point calculation is not performed, floating-point numbers can be displayed correctly in decimal system. This further confirms my idea that if floating-point numbers are involved in the calculation, the conversion process between binary and decimal floating-point numbers will become unpredictable and irreversible.

In fact, floating-point numbers are not suitable for accurate calculation, but for scientific calculation. Here is a little knowledge: Since float and double are used to represent numbers with decimal points, why don't we call them "decimal" or "real numbers" and call them floating-point numbers? Because these numbers are stored in the form of scientific notation. When a number such as 50.534 is converted into a scientific notation form of 5.053 e1, its decimal point moves to a new position (i.e. floats). It can be seen that floating-point numbers are originally used for scientific calculation, and it is really inappropriate to use them for accurate calculation.


Related articles: