- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Mathematical Functions in Java
The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. This class provides mathematical functions in Java.
Let us see some of these functions −
Sr.No | Method & Description |
---|---|
1 | static double abs(double a) This method returns the absolute value of a double value. |
2 | static float abs(float a) This method returns the absolute value of a float value. |
3 | static int abs(int a) This method returns the absolute value of an int value. |
4 | static long abs(long a) This method returns the absolute value of a long value. |
5 | static double acos(double a) This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi. |
6 | static double asin(double a) This method returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2. |
7 | static double atan(double a) This method returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2. |
8 | static double atan2(double y, double x) This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). |
9 | static double cbrt(double a) This method returns the cube root of a double value. |
10 | static double ceil(double a) This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. |
Let us now see an example to return the absolute value of a long value using the abs(long a) function in Java. Here, a is the argument whose absolute value is to be determined −
Example
import java.util.*; public class Demo { public static void main( String args[] ) { // get some longs to find their absolute values long x = 76487687634l; long y = -1876487618764l; // get and print their absolute values System.out.println("Math.abs(" + x + ")=" + Math.abs(x)); System.out.println("Math.abs(" + y + ")=" + Math.abs(y)); System.out.println("Math.abs(-18885785959l)=" + Math.abs(-18885785959l)); } }
Output
Math.abs(76487687634)=76487687634 Math.abs(-1876487618764)=1876487618764 Math.abs(-18885785959l)=18885785959
Example
Let us see another example wherein we are returning the hyperbolic sine of a double value −
import java.util.*; public class Demo { public static void main( String args[] ) { // get two double numbers numbers double x = 45; double y = -180; // convert them to radians x = Math.toRadians(x); y = Math.toRadians(y); // print the hyperbolic sine for these doubles System.out.println("sinh(" + x + ")=" + Math.sinh(x)); System.out.println("sinh(" + y + ")=" + Math.sinh(y)); } }
Output
sinh(0.7853981633974483)=0.8686709614860095 sinh(-3.141592653589793)=-11.548739357257748
Advertisements