Java floating Point arithmetic

Source: Internet
Author: User

Portability is one of the design goals of the Java language, and the same operation should get the same results regardless of which virtual machine is running. However, for the arithmetic operation of floating-point numbers, it is very difficult to achieve such portability. The double type uses 64 bits to store out a numeric value, while some processors use 80-bit floating-point registers, which increase the computational accuracy of intermediate processes. For example, the following calculations:

Double w = x * y/z;

Many INTEL processors compute x * Y and store the result in a register of 80, divided by Z and truncating the result to 64 bits, which results in a more accurate calculation and avoids exponential overflow. However, this result may not be the same as the result that is always calculated on a 64-bit machine.

The initial specification requires that all intermediate computations of a Java virtual machine must be truncated to ensure that floating-point computations, whether 80-bit or 64-bit, can maintain exactly the same results, but this may not only result in overflow but also more time consuming. To this end, Java acknowledges the conflicts between optimal performance and desirable results and gives improvements.

By default, Java virtual machines allow extended precision for intermediate calculations (if supported by the processor), but for methods marked with the STRICFP keyword, you must use strict floating-point calculations to generate reproducible results. If you can mark the main method as

public static strictfp void Main (string[] args)

In this way, all instructions in the main method will use a strict floating-point calculation. Or mark a class as STRICTFP, all methods in this class use strict floating-point calculations.

Above, from the "Java Core technology Volume I", slightly modified.

Summarize:

STRICTFP, or strict float point.

STRICTFP can modify a class or method so that it uses a strict floating-point calculation.

Strict floating-point calculations are not error-free without calculation results, and the opposite error may be greater, and may lead to overflow and loss performance.

If any rounding errors are not allowed in the numerical calculation, the BigDecimal class should be used.

Java floating Point arithmetic

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.