Java Classic example: comparing floating-point numbers

Source: Internet
Author: User

/*** Created by Frank * Compare floating point numbers*/ Public classfloatcmp {//Tolerances    Private Final StaticDouble EPSILON = 0.0000001;  Public Static voidMain (string[] args) {DoubleDa = 3 *. 3333333333; Doubledb = 0.99999992857; //the base type can be compared by using = = First        if(Da = =db) {System.out.println ("Java considers" + da + "= =" +db); }        //using the overloaded Equals method        Else if(Equals (DA, DB, 0.0000001) {System.out.println ("Equal within Epsilon" +EPSILON); } Else{System.out.println (da+ "!=" +db); }    }    /*** Compare 2 double type values within a given range of accuracy * *@parama parameter A *@paramB Parameter B *@paramthe tolerance value given by EPS *@returnCompare Results*/     Public Static BooleanequalsDoubleADoubleBDoubleEPS) {        returnA = = B | | Math.Abs (A-B) <EPS; }    /*** Compare 2 double type values within the default error range * *@parama parameter A *@paramB Parameter B *@returnCompare Results*/     Public Static BooleanequalsDoubleADoubleb) {returnequals (A, b, EPSILON); }}

Java Classic example: comparing floating-point numbers

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.