Java.lang.StrictMath class in Java | Set 2
Last Updated :
06 Aug, 2021
Java.lang.StrictMath Class in Java | Set 1
More methods of java.lang.StrictMath class
13. exp() : java.lang.StrictMath.exp(double arg) method returns the Euler’s number raised to the power of double argument.
Important cases:
- Result is NaN, if argument is NaN.
- Result is +ve infinity, if the argument is +ve infinity.
- Result is +ve zero, if argument is -ve infinity.
Syntax:
public static double exp(double arg)
Parameters:
arg - argument passed.
Returns:
Euler’s number raised to the power of passed argument
14. cosh() : java.lang.StrictMath.cosh() method returns the hyperbolic cosine of the argument passed.
Special cases :
- Result is NaN, if argument is NaN.
- Result is 1.0, if the argument is zero.
- Result is +ve infinity, if argument is infinite.
Syntax:
public static double cosh(double arg)
Parameters:
arg - The number whose hyperbolic cosine is to be returned.
Returns:
the hyperbolic cosine of the argument arg.
15. decrementExact() : java.lang.StrictMath.decrementExact() method decrements the value of passed argument by one.
Syntax:
public static int decrementExact(int arg)
or
public static long decrementExact(long arg)
Parameters:
arg - argument passed.
Returns:
return argument decremented by one.
Throws:
Exception if the result overflows long or int datatype, according to the
argumented data type.
Java code explaining exp(), decrementExact(), cosh() method in lang.StrictMath class.
Java
// Java program explaining lang.StrictMath class methods
// exp(), decrementExact(), cosh()
import java.math.*;
public class NewClass
{
public static void main(String[] args)
{
// Use of cosh() method
double value = 2;
double coshValue = StrictMath.cosh(value);
System.out.println("Hyperbolic Cosine of " + coshValue);
System.out.println("");
// Use of decrementExact() method
int result = StrictMath.decrementExact(3051);
System.out.println("Use of decrementExact() : " + result);
System.out.println("");
// Use of exp() method
// declare the exponent to be used
double exponent = 34;
// raise e to exponent declared
double expVal = StrictMath.exp(exponent);
System.out.println("Value of exp : "+ expVal);
}
}
Output:
Using addExact() : 9
acos value of Asini : NaN
acos value of Asinj : 0.054858647341251204
cube root : 6.0
16. log10() : java.lang.StrictMath.log10() method returns the base10 logarithmic value of the passed argument.
Syntax:
public static double log(double arg)
Parameters:
arg - argument passed.
Returns:
base10 logarithmic value of the argument passed.
17. pow() : java.lang.StrictMath.pow(double b, double e) method returns the value as be
Syntax:
public static double pow(double b, double e)
Parameters:
b : base
e : exponent
Returns:
value as baseexponent
18. incrementExact() : java.lang.StrictMath.incrementExact() method returns the argument by incrementing it's value.
Syntax:
public static int incrementExact(int arg)
or
public static long incrementExact(long arg)
Parameters:
arg - the argument
Returns:
incremented value of the argument
JAVA code explaining incrementExact(), log10(), pow() method in lang.StrictMath class.
Java
// Java program explaining lang.MATH class methods
// incrementExact(), log10(), pow()
import java.lang.*;
public class NewClass
{
public static void main(String[] args)
{
// Use of incrementExact() method
int f1 = 30, f2 = -56;
f1 = StrictMath.incrementExact(f1);
System.out.println("Incremented value of f1 : " + f1);
f2 = StrictMath.incrementExact(f2);
System.out.println("Incremented value of f2 : " + f2);
System.out.println("");
// Use of log10() method
double value = 10;
double logValue = StrictMath.log10(value);
System.out.println("Log10 value of 10 : " + logValue);
System.out.println("");
// Use of pow() method
double b = 10, e = 2;
double power = StrictMath.pow(b, e);
System.out.println("Use of pow() : " + power);
}
}
Output :
Incremented value of f1 : 31
Incremented value of f2 : -55
Log10 value of 10 : 1.0
Use of pow() : 100.0
19. signum() : java.lang.StrictMath.signum() method returns the signum value of the argument passed.
-1 if x < 0
signum fun(x) = 0 if x = 0
1 if x > 0
Note:
Result is NaN, if passed the argument is Nan.
Syntax:
public static double signum(double x)
or
public static float signum(float x)
Parameters:
x - the argument whose signum value we need
Returns:
signum value of x
20. max() : java.lang.StrictMath.max(double v1, double v2) method returns the greater value out of the two passed argument values.
This method just compares using magnitude without considering any sign.
Syntax:
public static double max(double v1, double v2)
Parameters:
v1 - first value
v2 - second value
Returns:
v1 or v2 based on which number is greater.
It can return either of the two if v1 = v2.
21. round() : java.lang.StrictMath.round() method round off the passed argument upto closest decimal places.
Note: Result is 0, if the argument is NaN.
Syntax:
public static long round(long arg)
or
public static double round(double arg)
Parameters:
arg - argument needs to round off
Returns:
round off value of the argument
Java code explaining signum(), round(), max() method in lang.StrictMath class.
Java
// Java code explaining the lang.StrictMath Class methods
// signum(), round(), max()
import java.lang.*;
public class NewClass
{
public static void main(String args[])
{
// Use of signum() method
double x = 10.4556, y = -23.34789;
double signm = StrictMath.signum(x);
System.out.println("Signum of 10.45 = " + signm);
signm = StrictMath.signum(y);
System.out.println("Signum of -23.34 = " + signm);
System.out.println("");
// Use of round() method
double r1 = StrictMath.round(x);
System.out.println("Round off 10.4556 = " + r1);
double r2 = StrictMath.round(y);
System.out.println("Round off 23.34789 = " + r2);
System.out.println("");
// Use of max() method on r1 and r2
double m = StrictMath.max(r1, r2);
System.out.println("Max b / w r1 and r2 = " + r2);
}
}
Output:
Signum of 10.45 = 1.0
Signum of -23.34 = -1.0
Round off 10.4556 = 10.0
Round off 23.34789 = -23.0
Max b/w r1 and r2 = -23.0
22. ulp() : java.lang.StrictMath.ulp() method returns Unit of least precision(ulp) ie. the least distance between two floating point numbers.
Here, it is the least distance b/w the argument and next larger value.
Syntax:
public static double ulp(double arg)
or
public static float ulp(float arg)
Parameters:
arg - argument passed.
Returns:
least distance b/w the argument and next larger value.
23. log1p() : java.lang.StrictMath.log1p() method returns natural log of (passed argument + 1).
Syntax:
public static double log1p(double arg)
Parameters:
arg - the argument
Returns:
log of (argument + 1).
This result is within 1 unit in the last place of exact result.
Java code explaining ulp(), log1p() method in lang.StrictMath class.
Java
// Java code explaining the lang.StrictMath Class methods
// ulp(), log1p()
import java.lang.*;
public class NewClass
{
public static void main(String args[])
{
// Use of ulp() method
double x = 34.652, y = -23.34789;
double u = StrictMath.ulp(x);
System.out.println("ulp of 34.652 : " + u);
u = StrictMath.ulp(y);
System.out.println("ulp of -23.34789 : " + u);
System.out.println("");
// Use of log() method
double l = 99;
double l1 = StrictMath.log1p(l);
System.out.println("Log of (1 + 99) : " + l1);
l1 = StrictMath.log(100);
System.out.println("Log of 100 : " + l1);
}
}
Output:
ulp of 34.652 : 7.105427357601002E-15
ulp of -23.34789 : 3.552713678800501E-15
Log of (1 + 99) : 4.605170185988092
Log of 100 : 4.605170185988092
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read