Math Class Elico
Math Class Elico
78
48
-2147483648
b.) Math.max()
- It returns the Largest of two values.
Example:
Output:
50
c.) Math.min()
- It is used to return the Smallest of two
values.
Example:
1. public class MinExample1
2. {
3. public static void main(String args[])
4. {
5. int x = 20;
6. int y = 50;
7. //print the minimum of two numbers
8. System.out.println(Math.min(x, y));
9. }
}
Output:
20
d.) Math.round()
- It is used to round of the decimal numbers
to the nearest value.
Example:
80
e.) Math.sqrt()
- It is used to return the square root of a
number.
Example:
1. public class SqrtExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 81.0;
6. // Input positive value, Output square root of x
7. System.out.println(Math.sqrt(x));
8. }
9. }
Output:
9.0
f.) Math.cbrt()
- It is used to return the cube root of a
number.
Example:
1. public class CbrtExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 729;
6. //return the cube root of x
7. System.out.println(Math.cbrt(x));
8. }
9. }
Output:
9.0
g.) Math.pow()
- It returns the value of first argument
raised to the power to second argument.
Example:
public class PowExample1
{
public static void main(String[] args)
{
double x = 5;
double y = 4;
//returns 5 power of 4 i.e. 5*5*5*5
System.out.println(Math.pow(x, y));
}
}
Output:
625.0
h.) Math.signum()
- It is used to find the sign of a given value.
Example:
1. public class SignumExample1
2. {
3. public static void main(String[] args)
4. {
5. double a= 82.7;
6. System.out.println(Math.signum(a));
7. }
8. }
Output:
1.0
i.) Math.ceil()
- It is used to find the smallest integer value
that is greater than or equal to the
argument or mathematical integer.
Example:
public class CeilExample1
{
public static void main(String[] args)
{
double x = 83.56;
// Input positive value, Output ceil value of x
System.out.println(Math.ceil(x));
}
}
Output:
84.0
j.) Math.copySign()
- It is used to find the Absolute value of first
argument along with sign specified in
second argument.
Example:
1. public class CopySignExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 740.4;
6. double y = -29.1;
7. // return -740.4 because second argument is negative
8. System.out.println(Math.copySign(x, y));
9. }
}
Output:
-740.4
k.) Math.nextAfter()
- It is used to return the floating-point
number adjacent to the first argument in
the direction of the second argument.
Example:
1. public class NextAfterExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 84352.24;
6. double b = 154.284
7. // print the next number for a towards b
8. System.out.println(Math.nextAfter(a, b));
9. // print the next number for b towards a
System.out.println(Math.nextAfter(b, a));
}
}
Output:
84352.23999999999
154.28400000000002
l.) Math.nextUp()
- It returns the floating-point value adjacent
to d in the direction of positive infinity.
Example:
1. public class NextUpExample2
2. {
3. public static void main(String[] args)
4. {
5. double x = 744.93;
6. // Input double value, Output adjacent floating-point
7. System.out.println(Math.nextUp(x));
8. }
9. }
Output:
744.9300000000001
m.) Math.nextDown()
- It returns the floating-point value adjacent
to d in the direction of negative infinity.
Example:
1. public class NextDownExample2
2. {
3. public static void main(String[] args)
4. {
5. double x = 744.93;
6. // Input double value, Output adjacent floating-point
7. System.out.println(Math.nextDown(x));
8. }
9. }
Output:
744.9299999999998
n.) Math.floor()
- It is used to find the largest integer value
which is less than or equal to the
argument and is equal to the
mathematical integer of a double value.
Example:
1. public class FloorExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 94.69;
6. // Input positive value, Output floor value of x
7. System.out.println(Math.floor(x));
8. }
9. }
Output:
94.0
o.) Math.floorDiv()
- It is used to find the largest integer value
that is less than or equal to the algebraic
quotient.
Example:
1. public class FloorDivExample1
2. {
3. public static void main(String[] args)
4. {
5. int x = 25;
6. int y= 3;
7. // 25/3 value is 8.33 so floor(8.33) = 8
8. System.out.println(Math.floorDiv(x, y));
9. }
}
Output:
8
p.) Math.random()
- It returns a double value with a positive
sign, greater than or equal to 0.0 and less
than 1.0.
Example:
1. public class RandomExample1
2. {
3. public static void main(String[] args)
4. {
5. // generate random number
6. double a = Math.random();
7. double b = Math.random();
8. // Output is different every time this code is executed
9. System.out.println(a);
System.out.println(b);
}
}
Output:
0.2594036953954201
0.08875674000436018
q.) Math.rint()
- It returns the double value that is closest
to the given argument and equal to
mathematical integer.
Example:
1. public class RintExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 81.68;
6. // Input positive value, Output round the x
7. System.out.println(Math.rint(x));
8. }
9. }
Output:
82.0
r.) Math.hypot()
- It returns sqrt(x2+y2) without
intermediate overflow or underflow.
Example:
1. public class HypotExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 8;
6. double b = 6;
7. // Return the value of sqrt(a power of 2 + b power of 2)
8. System.out.println(Math.hypot(a, b));
9. }
}
Output:
10.0
s.) Math.ulp()
- It returns the size of an ulp of the
argument.
Example:
1. public class UlpExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 8.1;
6. // Input positive double value, Output ulp(a)
7. System.out.println(Math.ulp(a));
8. }
9. }
Output:
1.7763568394002505E-15
t.) Math.getExponent()
- It is used to return the unbiased exponent
used in the representation of a value.
Example:
1. public class GetExponentExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 50.45;
6. // Input double value, Output exponent of it
7. System.out.println(Math.getExponent(a));
8. }
9. }
Output:
5
u.) Math.IEEEremainder()
- It is used to calculate the remainder
operation on two arguments as
prescribed by the IEEE 754 standard and
returns value.
Example:
1. public class IEEEremainderExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 387.1;
6. double b = 4.2;
7. System.out.println(Math.IEEEremainder(a, b));
8. }
9. }
Output:
0.7000000000000064
v.) Math.addExact()
- It is used to return the sum of its
arguments, throwing an exception if the
result overflows an int or long.
Example:
public class AddExactExample1
{
public static void main(String[] args)
{
int a = 469;
int b = 737;
// Input two positive value, Output addition of a and b
System.out.println(Math.addExact(a, b));
}
}
1.
Output:
1206
w.) Math.subtractExact()
- It returns the difference of the arguments,
throwing an exception if the result
overflows an int.
Example:
542
x.) Math.multiplyExact()
- It is used to return the product of the
arguments, throwing an exception if the
result overflows an int or long.
Example:
1. public class MultiplyExactExample1
2. {
3. public static void main(String[] args)
4. {
5. int a = 739;
6. int b = 5;
7. // Input two values, Output multiplication of a and b
8. System.out.println(Math.multiplyExact(a, b));
9. }
}
Output:
3695
y.) Math.incrementExact()
- It returns the argument incremented by
one, throwing an exception if the result
overflows an int.
Example:
1. public class IncrementExactExample1
2. {
3. public static void main(String[] args)
4. {
5. int a = 674;
6. System.out.println(Math.incrementExact(a));
7. }
8. }
Output:
675
z.) Math.decrementExact()
- It is used to return the argument
decremented by one, throwing an
exception if the result overflows an int or
long.
Example:
1. public class DecrementExactExample1
2. {
3. public static void main(String[] args)
4. {
5. int a = 830;
6. System.out.println(Math.decrementExact(a));
7. }
8. }
Output:
829
aa.) Math.negateExact()
- It is used to return the negation of the
argument, throwing an exception if the
result overflows an int or long.
Example:
public class NegateExactExample1
{
public static void main(String[] args)
{
int a = 379;
// Input a, Output -a
System.out.println(Math.negateExact(a));
}
}
2.
Output:
-379
bb.) Math.toIntExact()
- It returns the value of the long argument,
throwing an exception if the value
overflows an int.
Example:
public class ToIntExactExample1
{
public static void main(String[] args)
{
long a = 230;
System.out.println(Math.toIntExact(a));
}
}
3.
Output:
230
3.6609942506244004
b.) Math.log10()
- It is used to return the base 10 logarithm
of a double value.
Example:
public class Log10Example1
{
public static void main(String[] args)
{
double x = 38.9;
// Input positive double, output logarithm of x
System.out.println(Math.log10(x));
}
}
5.
Output:
1.5899496013257077
c.) Math.log1p()
- It returns the natural logarithm of the sum
of the argument and 1.
Example:
1. public class Log1pExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 26;
6. // Input positive value, Output natural logarithm of x
7. System.out.println(Math.log1p(x));
8. }
9. }
6.
Output:
3.295836866004329
d.) Math.exp()
- It returns E raised to the power of a
double value, where E is Euler's number
and it is approximately equal to 2.71828.
Example:
public class ExpExample1
{
public static void main(String[] args)
{
double a = 2.0;
// return (2.718281828459045) power of 2
System.out.println(Math.exp(a));
}
}
Output:
7.38905609893065
e.) Math.expm1()
- It is used to calculate the power of E and
subtract one from it.
Example:
1. public class Expm1Example1
2. {
3. public static void main(String[] args)
4. {
5. double a = 2.0;
6. // return {(2.718281828459045) power of 2} - 1
7. System.out.println(Math.expm1(a));
8. }
9. }
Output:
6.38905609893065
Output:
0.8660254037844386
b.) Math.cos()
- It is used to return the trigonometric
Cosine value of a Given double value.
Example:
1. public class CosExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 60;
6. // converting values to radians
7. double b = Math.toRadians(a);
8. System.out.println(Math.cos(b));
9. }
}
Output:
0.5000000000000001
c.) Math.tan()
- It is used to return the trigonometric
Tangent value of a Given double value.
Example:
1. public class TanExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 45;
6. // converting values to radians
7. double b = Math.toRadians(a);
8. System.out.println(Math.tan(b));
9. }
}
Output:
0.9999999999999999
d.) Math.asin()
- It is used to return the trigonometric Arc
Sine value of a Given double value.
Example:
1. public class AsinExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 1.0;
6. System.out.println(Math.asin(a));
7. }
8. }
Output:
1.5707963267948966
e.) Math.acos()
- It is used to return the trigonometric Arc
Cosine value of a Given double value.
Example:
public class AcosExample1
{
public static void main(String[] args)
{
double a = 1.0;
System.out.println(Math.acos(a));
}
}
7.
Output:
0.0
f.) Math.atan()
- It is used to return the trigonometric Arc
Tangent value of a Given double value.
Example:
public class AtanExample1
{
public static void main(String[] args)
{
double a = 6.267;
System.out.println(Math.atan(a));
}
}
8.
Output:
1.4125642791467878
Output:
5.343237290762231E12
b.) Math.cosh()
- It is used to return the trigonometric
Hyperbolic Sine value of a Given double
value.
Example:
1. public class CoshExample1
{
2. public static void main(String[] args)
3. {
4. double a = 60.0;
5. System.out.println(Math.cosh(a));
6. }
}
Output:
5.710036949078421E25
c.) Math.tanh()
- It is used to return the trigonometric
Hyperbolic Tangent value of a Given
double value.
Example:
1. public class TanhExample1
2. {
3. public static void main(String[] args)
4. {
5. double a = 12.0;
6. System.out.println(Math.tanh(a));
7. }
8. }
Output:
0.9999999999244973
#5 ANGULAR MATH METHODS
a.) Math.toDegrees
- It is used to convert the specified Radians
angle to equivalent angle measured in
Degrees.
Example:
1. public class ToDegreesExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = Math.PI;
6. // converting value of PI in degrees
7. System.out.println(Math.toDegrees(x));
8. }
9. }
Output:
180.0
b.) Math.toRadians
- It is used to convert the specified Degrees
angle to equivalent angle measured in
Radians.
Example:
1. public class ToRadiansExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 180.0;
6. // converting x from degree to radian
7. System.out.println(Math.toRadians(x));
8. }
9. }
Output:
3.141592653589793