Js,java, floating-point arithmetic errors and coping methods

Source: Internet
Author: User

Js,java, floating-point arithmetic errors and coping methods One, why does the floating-point number have an arithmetic error

The IEEE 754 standard specifies the interchange, arithmetic format, and method of binary and decimal floating-point numbers in the computer programming environment.

The existing storage media is 2 binary. The 2 binary base is 2, so a number will produce an infinite loop of decimals as long as the number of prime numbers containing more than 2 are included in the factor. The infinite loop of fractional and irrational numbers is not possible, and the rational number of non-infinite loops is stored in the same way on the storage medium while maintaining the computational compatibility.
For Infinite loop decimals, you can design a format to be stored on the media, but at the same time and the rational number of non-infinite loops can be calculated, the efficiency should become very low. For irrational numbers, the number of decimal digits has no regularity to follow, so the exact value cannot be saved at all, only approximate values can be saved.
High-precision calculation, you can generally forward the number to a string, minus the decimal point, the bitwise calculation and then save back to the string, and then add back the decimal point.

Two, error example

document.write (0.01 + 0.05); Output is 0.060000000000000005

document.write (0.09 + 0.01); Output is 0.09999999999999999

document.write (11*22.9); Output is 251.89999999999998

third, how to deal with JS

Expansion Multiplier Method : The number of decimal places to expand 10 of the N-th square

document.write ((0.01*100+0.09*100)/100); Output is 0.1

Rounding Method :

document.write ((0.01+0.09). toFixed (2)); Retains 2 decimal places and outputs 0.10

document.write (Math.Round ((0.01+0.09) *100)/100); Output is 0.1

Four, how Java should respond

Expansion Multiplier Method : The number of decimal places to expand 10 of the N-th square

System.out.println ((0.01*100+0.09*100)/100); Output is 0.1

Rounding Method :

System.out.println (Math.rint (0.01*100+0.09*100)/100); Output is 0.1

System.out.println (Math.Round (0.01*100+0.09*100)/100); Output is 0.1

System.out.println (New BigDecimal (0.099). Setscale (2, roundingmode.half_up). Doublevalue ()); Output is 0.10

Note: The Java Support 7 Rounding Method

1, Round_up: Rounding away from 0 directions. Rounds in the direction of the absolute value, as long as the discard bit is not 0.

2, Round_down: Trend 0 Direction rounding. Enter in the direction of the smallest absolute value, all the bits are discarded, there is no rounding.

3, Round_ceiling: Rounding to positive infinity. To the positive maximum direction. If positive, the rounding behavior is similar to round_up, and if negative, the rounding behavior is similar to Round_down. The Math.Round () method is the use of this pattern.

4. Round_floor: Rounding in negative infinity direction. To the negative Infinity direction. If positive, the rounding behavior is similar to Round_down, and if negative, the rounding behavior is similar to ROUND_UP.

5, Half_up: The nearest number is rounded (5 in). This is our most classic rounding.

6, Half_down: The most recent number rounding (5 homes). Here 5 is to abandon.

7, Hail_even: Banker rounding Method. Banker Rounding Method.

Js,java, floating-point arithmetic errors and coping methods

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.