Java, since there is a deviation in the calculation result when the power of the Double Type X 2 is n

Source: Internet
Author: User

In Java, the calculation result is biased when the power of the Double Type X 2 is N!
CodeAs follows:
Package com. aftvc. Tsing;

Public class example {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Exe p = new EXE (8 );
// P. decrease ();
// P. Add ();
// P. Mult ();
Figure tri = New Triangle (3, 4, 5 );
System. Out. println ("the circumference of this triangle is:" + tri. getgirth ());
System. Out. println ("the Triangle Area is:" + tri. getarea ());
Figure CIR = new circle (5 );
System. Out. println ("the circumference of this circle is:" + CIR. getgirth ());
System. Out. println ("the area of this circle is:" + CIR. getarea ());
Figure tra = new trapezoid (3,9, 4,5, 5 );
System. Out. println ("the circumference of this trapezoid is:" + tra. getgirth ());
System. Out. println ("this trapezoid area is:" + tra. getarea ());
}

}

Package com. aftvc. Tsing;

public class circle implements figure {
double r;
Public circle (double R1) {
r = R1;
}< br> Public double getarea () {
return pI * r * 2;
}< br> Public double getgirth () {
return pI * r * 2; // If * 3 Results 47.1*4 results are very long !! I tested 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16. As long as it is 2, The Npower shows a long result.
}< BR >}< br> the circumference of the triangle is: 12.0
the area of the triangle is: 6.0
the circumference of the circle is: 31.400000000000002
the area of the circle is: 157.0
the circumference of the trapezoid is: 22.0
the area of the trapezoid is: 24.0
return pI * r * 2;

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.