Home  >  Article  >  Computer Tutorials  >  What are java floating point constants

What are java floating point constants

WBOY
WBOYforward
2024-01-13 19:51:311169browse

What are java floating point constants

What are java floating point constants

Floating point data type represents real numbers, including decimal parts. Real-world data is complex and requires data types that support real numbers.

1. Floating-point constants Java’s real constants have two representation forms: decimal number form, which consists of numbers and decimal points, and must have a decimal point, such as 0.123, .123, 123., 123.0. 2. Scientific notation form. For example: 123e3 or 123E3, where there must be a number before e or E, and the exponent after e or E must be an integer. Real constants occupy 64 bits in the machine and have double values. For float type values, f or F must be added after the number, such as 12.3F, which occupies 32 bits in the machine and has low representation precision.

Floating point variables have two types: float and double. Float occupies 32 bits, and the value range is 3.4e-038~3.4e 038; while double occupies 64 bits, and the value range is 1.7e-308~1.7e 308. The double type has higher precision and a larger representation range than the float type, and is usually used in scenarios that require high precision, such as scientific calculations.

In Java, you can use float and double to define floating-point variables. For example: float f; // Specify variable f as float type double d; // Specify variable d as double type Unlike C and C, Java does not have an unsigned integer type and specifies the number of memory bytes occupied by integer and floating-point data. This provision ensures the security, robustness and platform independence of Java programs. This means that different Java implementations have consistent behavior when dealing with integer and floating point data, and the results are predictable regardless of the platform they are running on.

java floating point calculation problem

Bit operations are usually used in hardware control. The expression itself cannot determine the specific meaning and needs to be combined with the actual scenario.

& is called "bitwise AND", and its rules are: 0&0=0 1&0=0 0&1=0 1&1=1

"|" is called "bitwise OR", and the operation rules are: 0|0=0 1|0=1 0|1=1 1|1=1

0x7fffff is expressed in 32-bit binary as 0000 0000 0111 1111 1111 1111 1111 1111

According to the bitwise AND operation rule, the high 9 bits of bits are cleared to zero.

0x800000 is expressed in 32-bit binary as 0000 0000 1000 0000 0000 0000 0000 0000

According to the bitwise OR algorithm, the 24th bit is set to 1.

To put it in perspective, ((bits & 0x7fffff)|0x800000) means to clear the high 9 bits of bits first, and then set the 24th bit to 1.

Regarding the first half, "

As for the question mark, there’s no need to explain it, right?

A JAVA question about floating point types

Floating point numbers will have errors after operations, which is normal.

double a=2.00-1.10;

double b=0.90;

System.out.print(a==b);

The result will be false

So sometimes a method is used when judging the equality of floating point numbers, that is

public static boolean isEqual(double a,double b){

final double epsilon=0.00000001;

return Math.abs(a-b)

}

To sum up, errors will occur after floating point numbers are processed. 0.7799999713897705 is the error caused by converting float to double, and 0.78 is the normal operation of float

The above is the detailed content of What are java floating point constants. 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