The difference between java single precision and double precision

Why is there a problem with accuracy?

Computer processing data involves data conversion and various complex operations, such as different unit conversion, different hexadecimal (such as binary decimal) conversion, etc., many division operations can not be divided, such as 10 ÷ 3 = 3.3333. . . . . . Infinitely endless, and the precision is limited, 3.3333333x3 is not equal to 10. The decimal data obtained after complicated processing is not accurate, and the higher the precision, the more accurate. Float has 8 significant digits, double has 16 significant data, float and double are all up to a certain value, automatically start using scientific notation, and retain the significant digits of the relevant precision, so the result is an approximate number. If you are more precise in calculating decimals (such as finance, mathematics), you should use Bigcimal if you want the result to be more in line with the expected value. The calculator should also have precision problems, as well as binary decimal conversions.

The difference between java double precision and single precision

In reality, there are not only integer values, but also decimals. The Java language also provides storage types for decimals, which are float and double.

The floating-point type of the Java language has two different representations: decimal numbers and scientific notation. The decimal number form consists of numbers and decimal points, and must have decimal points, such as 0.123, 12.85, 26.98, etc.; scientific notation forms, such as: 2.1E5, 3.7e-2, etc. There must be a number before e or E, and the index after e or E must be an integer.

Scientific notation

A method of simplifying counting, scientific counting has no advantage for numerical values ​​with a small number of digits, but the advantage of the counting method is very obvious for numerical values ​​with a large number of digits. For example, the speed of light is 300,000,000 meters / second, and the population of the world is about 6,100,000,000. Similar to the speed of light and the number of people in the world, it is inconvenient to read and write. If you look closely at the above two values, the speed of light can be written as 3*108, and the population of the world can be written as 6.1*109. This counting method is the scientific counting method. The Java language science counting method indicates that the speed of light is 3E8, and the world population is about 6.1E9.

A larger value ("0) can be expressed in the following way:

a * 10n (1 "=a"10, n is an integer)

Where a is the effective number of digits of the value, the number of significant digits from the first digit on the left is not 0, to the end digit, all digits (including 0, the scientific notation does not count 10 n power), called effective number. For example, the speed of light is 3E8, the effective number is 1 digit, the value is 3; the world population is about 6.1E9, the effective number is 2 digits, and the value is 6.1.

n is a positive integer minus one of the integer part of the value.

A smaller value ("0") can be expressed in the following way:

a * 10-n (1 "=a"10, n is an integer)

The value of a is the same as above, and the value of n is the number of all 0s before the first digit of the original number that is not 0 (including the 0 before the decimal point).

In the Java language, floating-point types are float and double, which represent single-precision and double-precision values, respectively. Accuracy refers to the accuracy of describing a numerical value. In mathematical operations, an approximate number is often used. The approximate number is very similar to the original value, but it does not completely conform to the original value. It can only be said to be approximated to some extent. The precision is similar to the approximate number, and the original value is replaced by a number that is very close to the original value.

As mentioned earlier, the more bytes used to store a value, the higher the accuracy and the larger the range of values. From this point of view, the precision is closely related to the number of bytes of storage. The memory type of the float type is 4 bytes, which represents a value range of about 10-38 to 1038, and the storage space of the double type is 8 bytes. The range of values ​​represented is approximately 10-308 to 10308, and the precision and extent of the float value stored is less than the precision and range of the double stored value. Therefore, float is a single-precision value and double is a double-precision value.

The difference between java single precision and double precision

The float variable can be declared as follows:

The double variable can be declared as follows:

When a value is assigned to a float variable, the lower end of the value is appended with a lowercase "f" or an uppercase "F" declared as a float value. Otherwise, the compiler will give "an error that may lose precision" as shown in the following figure. Because in the Java language, the value with a decimal is defaulted to a double type, and the double type is converted to a float type, which naturally loses precision.

When using floating-point and fractional numbers in a Java program, you cannot assume that floating-point calculations must produce integer or exact results. Floating-point numbers are not exact values, and using them can cause rounding errors.

48V 100Ah

For over ten years, UFO POWER has dedicated to supply reliable but afforable 48V 100Ah Telecom Battery. We do our best to ensure the reliability of the batteries and safety operation by selecting high quality battery cells and equipping with our own in-house developed BMS. The BMS provides a series of battery protection, including over charge and discharge detection, over current detection, temperature and short circuit protection and balance function.


Ufo Battery Application


Protection of Rack Mount LiFePO4 Battery for Telecom Towers
● Over charge/ discharge Protection
● Over current Protection
● Over voltage Protection
● Over temperature Protection
● Short Circuit Protection

Solar Storage Battery,48V Solar Battery,48V Battery In Parallel,Lithium Battery Packs

ShenZhen UFO Power Technology Co., Ltd. , https://www.ufobattery.com

Posted on