Current location - Training Enrollment Network - Mathematics courses - How to calculate the power of a function in Java?
How to calculate the power of a function in Java?
In java, the usual mathematical operations are in the mathematical class, and the power of the function is the pow method in the mathematical class: public static double pow(double? First,? Double? b),? Returns the quadratic value of the first parameter.

For example, to calculate the third power of 2, the code is as follows:

Common class test {

Public static void main(String[] args) {

Double a = math.pow (2,3);

}

}

The running result is 8.

Extended data:

Math class contains methods to perform basic mathematical operations, such as initial exponent, logarithm, square root and trigonometric function.

Different from some Mathematical methods of StrictMath class, not all equivalent functions of math class are defined to return the same result bit by bit. This class can perform better where strict repetition is not required.

By default, many Math methods only call the equivalent methods in StrictMath to complete their implementation. It is recommended that the code generator use the platform-specific native library or microprocessor instructions (if available) to provide a higher performance mathematical method for implementation. The realization of this higher performance must still conform to the mathematical norms.

The quality of the implementation specification involves two attributes, namely, the accuracy of the returned result and the monotonicity of the method. The accuracy of floating-point mathematical methods is measured according to ulp (last bit unit). For a given floating-point format, the ulp of a specific real value is the difference between two floating-point values including that value.

When discussing the accuracy of the whole method rather than the accuracy of specific parameters, the introduced ulp number is used for the worst-case error of any parameter.

If the error of a method is always less than 0.5 ulp, then the method always returns the floating-point number closest to the accurate result; This method is correct rounding. A correct rounding method can usually get the best floating-point approximation; However, for many floating-point methods, it is impractical to round correctly.

On the contrary, for mathematics, some methods allow errors in the range of 1 or 2 ulp. Unofficially, for the error range of 1 ulp, when the accurate result is a representable value, the accurate result should be returned according to the calculation result; Otherwise, one of two floating-point values containing an exact result is returned. For accurate results with large values, one end of the bracket can be infinite.

In addition to the accuracy of a single parameter, it is also important to maintain the correct relationship between methods with different parameters.

Therefore, most methods that require an error greater than 0.5 ulp are semi-monotonous: as long as the mathematical function is non-decreasing, the floating-point approximation is non-decreasing; Similarly, as long as the mathematical function is non-incremental, floating-point approximation is non-incremental. Not all approximations with an accuracy of 1 ulp can automatically meet the monotonicity requirement.

References:

/javase