Precise Calculation of float and double Precision in java

Source: Internet
Author: User

 

In Java, the double Type format follows the IEEE 754 standard. Although decimal places are continuous in mathematics, double represents only some discrete points, and the set composed of these discrete points is counted as S. The size of S is still limited. If the fractional P to be saved is within the set S, the double type can accurately represent P; otherwise, the double type can only find a discrete point closest to P from the set S to replace P.

The above statement is also true for float. In IEEE 754, float and double have identical representation policies. The difference is only reflected in the number of bits in each field (index field and decimal field.

That is to say, float and double are both inaccurate, so sometimes some strange things happen:

 

Double d = 1e16d; double d2 = d + 1.0d-d; // the following code outputs 0.0 instead of 1.0 System. out. println (d2 );
The above results may not be interesting, because your teacher may remind you when learning C language. But what's interesting is how inaccurate float and double are? Given a double d = XXX, what is the value of the other two double closest to d?

I used Java to write a StructuredFloat class to parse the original data types float and double. Given a float or double value P, it can calculate the floating point that is closest to the absolute value of P and can be represented by float/double. The usage is as follows:

    double cur = 3.23d; // any valid double value     StructuredFloat sd = com.vigour.StructuredFloat.StructedFloatBuilder.buildDouble(cur);    StructuredFloat smaller = sd.absSmaller();    StructuredFloat bigger = sd.absBigger();        if(smaller != null){      double smaller_double = smaller.getBigDecimal().doubleValue();      // now you get the nearest double value whose absolute value is smaller    }    if(bigger!= null){      double bigger_double = bigger.getBigDecimal().doubleValue();      // now you get the nearest double value whose absolute value is bigger    }

Below are some interesting outputs. We can see that the precision of double in the vicinity of 1e16d is smaller than 1, and in the vicinity of 1e7f (10 million), the float precision is equal to 1, float is really embarrassing.

 

------------- Some Interesting Resule for Double ------------------ // double Nearest smaller double: 0.09999999999999999167332731531132594682276248931884765625 Current double: 0.1000000000000000055511151231257827021181583404541015625 Nearest bigger double: 0.10000000000000001942890293094023945741355419158935546875 // double Nearest smaller double near 1.0d: 0.99999999999999988897769753748434595763683319091796875 Current double: 1 Nearest bigger double: 1.0000000000000002220446049250313080847263336181640625 // double Nearest smaller double: 9.9999999999999982236431605997495353221893310546875000 Current double: 10.00 Nearest bigger double: 10.0000000000000017763568394002504646778106689453125000 // doubleNearest smaller double near 1e14d: 99999999999999.9843750000000000000000000000000000000000000000000000 Current double: 100000000000000.00000000000000000000000000000000 Nearest bigger double: 100000000000000.0156250000000000000000000000000000000000000000000000 // doubleNearest smaller double: 999999999999999.8750000000000000000000000000000000000000000000000000 Current double: 1000000000000000.0000000000000000000000000000000000 Nearest bigger double: 1000000000000000.1250000000000000000000000000000000000000000000000000 // doubleNearest smaller double near 1e16d: 9999999999999998.0000000000000000000000000000000000000000000000000000 Current double: 10000000000000000.0000000000000000000000000000000000000 Nearest bigger double: 10000000000000002.0000000000000000000000000000000000000000000000000000 // doubleNearest smaller double: 99999999999999984.0000000000000000000000000000000000000000000000000000 Current double: 100000000000000000.000000000000000000000000000000000000000 Nearest bigger double: 100000000000000016.0000000000000000000000000000000000000000000000000000 // doubleNearest smaller double near 1e304d: 9999999999999998174371273630364736815867488735718786093662414371947263704524926751224722911637244940234972882804879769415602664816552507597839565690480126952738889402600333599657997758603312171995012866291845554976690497648524473448849371595248581587050582985041870802940253992811266476846330599148879872.0000000000000000000000000000000000000000000000000000 Current double: 9999999999999999392535525055364621860040287220117324953190771571323204563013233902843309257440507748436856118056162172578717193742636030530235798840866882774987301441682011041067710253162440905843719802548551599076639682550821832659549112269607949805346034918662572406407604380845959862074904348138143744.000000000000000000000000000000000000000000000000 Nearest bigger double: 10000000000000000610699776480364506904213085704515863812719128770699145421501541054461895603243770556638739353307444575741831722668719553462632031991253638597235713480763688482477422747721569639692426738805257643176588867453119191870248852943967318023641486852283274009874954768880653247303478097127407616.0000000000000000000000000000000000000000000000000000 ------------- Some Interesting Resule for Float ------------------ // floatNearest smaller float: 0.0999999940395355224609375 Current float: 0.100000001490116119384765625 Nearest bigger float: 0.10000000894069671630859375 // floatNearest smaller float: 0.999999940395355224609375 Current float: 1 Nearest bigger float: 1.00000011920928955078125 // floatNearest smaller float: 9.99999904632568359375000 Current float: 10.00 Nearest bigger float: 10.00000095367431640625000 // floatNearest smaller float: 99999.99218750000000000000000 Current float: 100000.00000000000 Nearest bigger float: 100000.00781250000000000000000 // floatNearest smaller float: 999999.93750000000000000000000 Current float: 1000000.0000000000000 Nearest bigger float: Nearest: 1000000.06250000000000000000000 // floatNearest smaller float: 9999999.00000000000000000000000 Current float: 10000000.0000000000000000 Nearest bigger float: 10000001.00000000000000000000000 // floatNearest smaller float: 99999992.00000000000000000000000 Current float: 100000000.000000000000000000 Nearest bigger float: Nearest: 100000008.00000000000000000000000 // floatNearest smaller float: 99999986661652122824821048795547566080.00000000000000000000 Current float: 99999996802856924650656260769173209088.00000000000000000000000 Nearest bigger float: 100000006944061726476491472742798852096.0000000000000000000000 near 1e38f


 

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.