Why is Java float Double Precision lost? Discussion on the floating point Precision of Java

Source: Internet
Author: User

Because of float or double
Improper use may cause loss of precision. The problem can be understood through the following code:

Public class floatdoubletest {<br/> Public static void main (string [] ARGs) {<br/> float F = 20014999; <br/> double D = F; <br/> double D2 = 20014999; <br/> system. out. println ("F =" + F); <br/> system. out. println ("d =" + d); <br/> system. out. println ("D2 =" + D2); <br/>}< br/>

The result is as follows:

F = 2.0015e7

D = 2.0015e7

D2 = 2.0014999e7

The output result shows that double
The value 20014999 is correct.
And float
No way to indicate 20014999
Is an approximate value. These results are quite surprising. 20014999
Such a small number cannot be expressed in float. So I asked this question.
I made a simple sharing on float and double learning, hoping to help you understand Java
Floating
Point Understanding.

 

About
Java

Of
Float

And
Double

Java

The language supports two basic floating point types: Float

And double
. Java
Based on the floating point type
IEEE 1, 754
Standard. IEEE
754
32 defined
Bitwise AND
64
Bitwise dual-precision two floating point binary decimal standards.

IEEE
754
The base number is 2 in scientific notation

To represent the floating point. 32

The bitwise floating point uses 1.

The digit represents the digit.

Bits to represent the index, with 23

Bits to indicate the ending number, that is, the decimal part. Indexes that are signed integers can be positive or negative. Decimal part in binary (base number
2
. For 64
Double-precision floating point number,
1
Symbol used to represent a number.
11
Digit indicates the index, 52

Bits indicate the ending number. Two diagrams are shown as follows:

Float (32-bit ):

Double (64-bit ):

There are three parts:

(1)
I
Separate symbol (s)
Direct encoding symbol S
.

(2) k
Bit
Power Index e
,Shift
Code representation


.

(3) N
Bit
Decimal,Original code representation

.

So 20014999


Why?

Float


No way to indicate it correctly?


Combined with the float and double representation methods, through analysis

20014999



You can find the answer.

The following procedure can be concluded:

20014999



In

Double



And

Float



In binary format.

Public class floatdoubletest3 {<br/> Public static void main (string [] ARGs) {<br/> double D = 8; <br/> long l = double. doubletolongbits (d); <br/> system. out. println (Long. tobinarystring (l); <br/> float F = 8; <br/> int I = float. floattointbits (f); <br/> system. out. println (integer. tobinarystring (I); <br/>}< br/>

The output result is as follows:

Double: 100000101110011000101100111100101110000000000000000000000000000

Float: 1001011100110001011001111001100


The output result is analyzed as follows. None

Double



To the left of the binary

0



You can get

64



The binary number of bits. Based on the double table
The display method consists of three parts: symbol number, power index, and tail number:

0
10000010111
0011000101100111100101110000000000000000000000000000


For

Float

Pad on the left
Number

0

You can get

32

The binary number of bits.


According to the float representation,


Also divided


The symbol number, power index, and ending number are as follows:


:

0
10010111
00110001011001111001100


The green part is the symbol bit, the red part is the power index, and the blue part is the ending number.

The comparison shows that:All symbols


0

The power index is a shift code representation, and the two are exactly the same. The only difference is the ending number.


In

Double

Tail Number
Is:


001100010110011110010111
0000000000000000000000000000



, Omitting the following zero. At least 24 digits are required for the correct representation.


.

In

Float

Tail number below
Is:

00110001011001111001100

, Total

23

Bit.

Why? The reason is obvious, because

Float tail count

Up

23

Bit, so

24

Bit

001100010110011110010111

In

Float

The following is rounded

23

Bit

00110001011001111001100

. So

20014999

In

Float

The following changes

20015000

.
That is to say


20014999

Although it is within the float representation range
In

IEEE
754

Of

Float

There is no way to express the length of the representation precision

20014999

Instead, you can only obtain an approximate value by rounding.

 

Summary:


Floating point operations are rarely accurate, and errors are generated if they exceed the range that can be expressed with precision. Errors are often not
Because of the size of the number, it is because of the precision of the number. Therefore, the result is similar to but not equal to the expected result. Especially in use

Float

And

Double

Precise operation
Be especially careful when calculating.
Some alternatives can be considered for implementation. If you pass

String

Integration

Bigdecimal

Or
By using

Long

Type to convert.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.