Floating-point analysis _java programming in Java

Source: Internet
Author: User
Article Source: csdn Author: treeroot

Floating point numbers are divided into single and double precision, and single and double in Java. Do you know how float and double are stored?

Float is 4 bytes, double is 8 bytes, and for convenience, only float types are discussed here.
Float is actually the same size as an int, with a total of 32 digits, the first digit is the symbol, 2-9 is the exponent, and the following 23 digits represent the decimal part. Here is not much to say, please refer to: http://blog.csdn.net/treeroot/archive/2004/ 09/05/95071.aspx

Here is just one example, I hope to explore, is to study the floating point 0.1 storage form, run this program first.


public class test{
public static void Main (string[] args) {
int x = 0x3d800000;
int i = 1 << 22;
Int j = 1 << 4;
float F = 0.1f;
int y = float.floattointbits (f);
Float rest = f-((float) 1)/J;

while (i > 0) {
J <<= 1;
float Deta = ((float) 1)/J;
if (rest >= deta) {
Rest-= Deta;
x |= i;
}
I >>= 1;
}
PR (x);
Pr (y);
}

static void Pr (int i) {
System.out.println (integer.tobinarystring (i));
}

}

Results:
111101110011001100110011001101
111101110011001100110011001101

Program Description:
int x=0x3d80000;
Because the floating-point representation is 1.f*2n-127 we want to say 0.1, we can know n-127=-4, to n=123
The symbol is positive, know that the first 9 is 001111011, temporarily do not consider the following 23 decimal places, so we first assume x=0x3d800000;


int i = 1 << 22;
I start with the first right and the 23rd digit is 1, which is the 10th digit of x.

Int j = 1 << 4;

I start with 4, because n-127 is 4, here is to ask for its reciprocal.

float F = 0.1f;
int y = float.floattointbits (f);

Y is its 32-bit representation.

Float rest = f-((float) 1)/J;

This rest represents 1 of the rest of the 1.F, which is 0.f.

while (i > 0) {
J <<= 1;
float Deta = ((float) 1)/J;
if (rest >= deta) {
Rest-= Deta;
x |= i;
}
I >>= 1;
}

This loop calculates the 23-bit decimal part, and if rest is not less than deta, it means that the bit can be set to 1.

The other said, the input results are the same, you can say that 0.1 of this floating-point number is certainly inaccurate, but 0.5 can be precise, think about why

Related Article

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.