Mathematical Functions
Sooner or later you are likely to need mathematical functions in your programs, even if it’s only obtaining an absolute value or calculating a square root. Java provides a range of methods that support such functions as part of the standard library stored in the package java.lang, all these are available in your program automatically.
The methods that support various additional mathematical functions are implemented in the class Math, so to reference a particular method you need to write Math and a period in front of the method name. For example, to use sqrt( ) which calculates the square root of whatever you place between parentheses, you should write Math.sqrt(aNumber).
The class Math includes a range of numerical functions. Some of the important ones are listed below in the table:
Method Function
abs(arg) Calculates absolute value of the argument
max(arg1, arg2) Returns the larger of the two argument
min(arg1, arg2) Returns the smaller of the two arguments
ceil(arg) Returns the smallest integer that is greater than or equal to the argument
floor(arg) Returns the largest integer that is less than or equal to the argument
round(arg) Calculates the nearest integer to the argument value
The mathematical functions available in the class Math are:
Method Function
sqrt(arg) Calculates the square root of the argument
pow(arg1, arg2) Calculates the first argument raised to the power of the second argument
exp(arg) Calculates e raised to the power of the argument
log(arg) Calculates the natural logarithm (base e) of the argument
random( ) Returns a pseudorandom number between 0.0 and 1.0
The Math class also defines double values for e and pi, which you can access as Math.E and Math.PI respectively. To find out more about the Math class and the methods it provides please refer to Java Documentation.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment