Simple floating-point number types in Java float and double are not capable of precise operation

Source: Internet
Author: User

In Java, simple floating-point types, float and double, are not capable of operation. Let's take a look at the following two program codes:

Code One:

Import Java.util.Scanner;

Class Circle {
Double radius;
static final double pi=3.14;
Public Circle () {this.radius=0;}
Public Circle (Double R) {this.radius=r;}
Public double Getarea () {return pi*this.radius*this.radius;}
Public double Getperimeter () {return 2*pi*this.radius;}
}

public class Cylinder extends Circle {
private double height;
Public Cylinder (Double r,double h) {
This.height=h;
This.radius=r;
}
Public double getheight () {return this.height;}
Public double Getvol () {return this.height*pi*this.radius*this.radius;}
public void Dispvol () {System.out.println ("Volume of Cylinder:" +this.getvol ());}
}

public class Test {

public static void Main (string[] args) {
Scanner input=new Scanner (system.in);
System.out.println ("Please enter the radius of the circle and the height of the cylinder:");
Double r=input.nextdouble ();
Double h=input.nextdouble ();
Cylinder A =new Cylinder (r,h);
A.dispvol ();
}

}

Code two: We do not modify the circle parent class and the main function, we change the class cylinder in the Getvol () method of the method body public double Getvol () {return This.height*this.getarea ();}, From the above program we can look at, in fact, can be understood to achieve the same effect of the program.

But when we enter 7 and 6, or 6, 5, these two sets of data, we will find that there is a certain discrepancy in the results.

The results are as follows:

Code One: Code two:

Please enter the radius of the circle and the height of the cylinder: Enter the radius of the circle and the height of the cylinder:
7 7
6 6
Volume of Cylinder: 923.16 volume of cylinder: 923.1600000000001

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------

Code One: Code two:

Please enter the radius of the circle and the height of the cylinder: Enter the radius of the circle and the height of the cylinder:
6 6
5 5
Volume of Cylinder: 565.2 volume of cylinder: 565.1999999999999

Why does the above result occur?

We can say that floating-point numbers are not suitable for accurate calculations and are suitable for scientific calculations

Give a small example: 1.double a= (1.2-0.4)/0.1; System.out.println (a); 2.double a=0.8/0.1; System.out.println (a); The results of these two codes are different, the first result is 7.9999999999999999, the second result is 8.0. We all know that the calculation of the number of computers is binary computing, we actually enter the decimal number, some decimal number conversion to binary is accurate conversion, and some conversion is not the exact conversion, get a number nearest to it, so there is an error in the face. In addition, if the floating-point number is not calculated, the floating-point number can be displayed correctly in decimal, if the floating-point number participates in the calculation, then the conversion process between the floating-point binary and the decimal will become unknown and irreversible. So how do we use floating-point numbers for accurate calculations?

Method One: Math.Round (value*100)/100.0; If value is 4.015, then the result holds two decimal places as 4.01, but we are thinking of 4.02, because rounding.

Method Two: DecimalFormat ("0.00"). Format (4.015); The result of this code is the same as preserving two decimal places for 4.01, not 4.02

Method Three: New Java.math.BigDecimal (Double.tostring (4.015)). Setscale (2,java.math.bigdecimal.round_half_up). Doublevalue The result of this code is also reserved two decimal places, but 4.02, the real is rounding

For the code of the three methods above, refer to the Java API documentation. The above is a small summary of a Java beginner, I hope you have more comments and discussion! Thank!

Simple floating-point number types in Java float and double are not capable of precise operation

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.