Home >Computer Tutorials >Computer Knowledge >In-depth analysis of double type floating point operations in JAVA

In-depth analysis of double type floating point operations in JAVA

王林
王林forward
2024-01-15 09:57:221283browse

JAVA’s double type floating point operations need to be explained

double f=0.0005;

double i=3;

double d=f*i;

double f1=0.0005;

double j=3;

double d1=f1*j;

if(d==d1){

System.out.println("aaa");

}This is equivalent. The basic data types in Java are called automatic variables. Automatic variables store literal values. Since the data size and lifetime of literal values ​​are known, they are placed on the stack for speed reasons. , the data in the stack can be shared. For example, int a=3 int b=3, the compiler first processes int a=3; first it will create a reference to the variable a on the stack, and then look for the address whose literal value is equal to 3, If not, open an address to store the literal value 3, and then process int b=3; after creating the application with the variable b, check if there is an address with the literal value 3. Now that it exists, it points to the address of 3, which When using == (when judging whether the addresses are the same), it will be True

What is cpu floating point operation

1. Floating point performance, formerly called coprocessor, 486 was not included in the CPU before (8086~8087, 80286~80287, 80386~80387=80386DX, 80486SX~80487=80486DX, 586=586 587 ...) Floating point arithmetic is a high-precision arithmetic method mainly used in science and multimedia.

2. It can be understood as an operation method in which the decimal point can be moved. Current speed AMD>>Inter

3. Floating point numbers refer to rational numbers with limited decimal places, such as -10.8, 0.00, 25.01, etc.

4. Floating point operation, the result is a floating point number, and the decimal part of the calculation result will be retained.

For example: when using floating point operations, 100.0÷3.0=33.33333333.

5. For example, if a bullet hits the wall and a piece of soil falls off, with strong floating point calculations, it may be possible to display the falling dust.

Hope to adopt

The difference between single precision and double precision floating point types in C language! What kind of 3 14 is it? 3 14159

3.14 is single precision, 3.14159 is double precision.

The difference between the two is as follows:

1. Different references

1. Single precision: refers to a way for computers to express real number approximations.

2. Double precision: This data type is similar to the single precision data type (float), but has higher accuracy than float.

In-depth analysis of double type floating point operations in JAVA

2. Different occupied space

1. Single precision: The range is from -3.402823E38 to -1.401298E-45 when the number is negative, and from 1.401298E-45 to 3.402823E38 when the number is positive.

2. Double precision: Double precision type occupies 8 bytes (64 bits) of memory space, and its value range is -1.79769313486232E308 to 1.79769313486232E308.

3. Different characteristics

1. Single precision: When the value is smaller than the above value, the accuracy will be gradually lost due to the reduction of the number of significant digits of the mantissa (as specified by IEEE 754), or some systems directly use the value 0 to simplify the processing process.

2. Double precision: The memory space occupied during compilation varies according to different compilers. It is a double float data type, a variable type that represents real variables in C/C.

Reference source: Encyclopedia single precision

Reference source: Encyclopedia-Double Precision Floating Point

How to represent floating point in computers

Floating point is obtained by multiplying an integer or fixed-point number (that is, the mantissa) by an integer power of a certain base (usually 2 in computers). This representation method is similar to the scientific notation with base 10.

Floating point number a is represented by two numbers m and e: a = m * b^e (b raised to the power of e).

In any such system, we choose a base b (the base of the notation system) and a precision p (that is, how many bits to use for storage). m (that is, the mantissa) is a number of p digits in the form ±d.ddd...ddd (each bit is an integer between 0 and b-1, including 0 and b-1). If the first bit of m is a non-zero integer, m is called normalized.

Some descriptions use a separate sign bit (s represents or -) to indicate positive or negative, so m must be positive. e is the exponent.

We can use these 3 methods to represent floating point numbers:

1. BCD code.

2. Exponent code mantissa representation.

3. We can transform the second method to get a better method. First move the decimal point of a floating point number to the end and express it with N=M*RC, R=10, then convert M into binary B, then use C as the exponent code, and use B as the mantissa to express it using the exponent code mantissa method.

For example: 3.14159=314159*10^(-5).

314159 is expressed in binary as 1001100101100101111.

Using this method can not only accurately represent the value of floating point numbers, but also make full use of storage space.

In-depth analysis of double type floating point operations in JAVA

Extended information:

Floating point unit (FPU)

Floating point operations are different from integer operations, so the operation units are of course different. Early floating-point processors appeared as "external co-processors" for the CPU. x87 FPU specifically refers to the floating-point coprocessor architecture that is used with x86 processors.

The following points are included:

1. The floating-point register adopts a stack structure.

The depth is 8 and the width is 80 bits, that is, 8 80-bit registers.

The names are ST(0) ~ ST(7), the top of the stack is ST(0), and the numbers are 0 ~ 7 respectively.

2. All floating point operations are performed with 80-bit extended precision.

3. Floating point numbers are transferred between floating point registers and memory.

(1) Float, double, and long double type variables are represented in memory by IEEE 754 single precision, double precision, and extended precision respectively, occupying 32 bits, 64 bits, and 96 bits respectively (the first 16 bits are invalid).

(2) Float, double, and long double type variables are all represented with 80-bit extended precision in floating-point registers.

(3) From floating point register to memory: 80-bit extended precision format is converted to 32-bit or 64-bit.

(4) From memory to floating point register: Convert 32-bit or 64-bit to 80-bit extended precision format.

Reference source: Encyclopedia-Floating Point

Encyclopedia-Floating point representation

The above is the detailed content of In-depth analysis of double type floating point operations in JAVA. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete