The logarithmic outputs can be easily generated in
java using simpler math functions. If you want to know more about Basic Math
functions in Java, do check HERE.
Math.log()
This math function outputs the logarithmic value of
the input variable.
The input can be of any data type like byte, short,
int, long, float and double.
class java {
public static void main(String args[]) {
//Declaring variables
byte p = 112;
short q=3200;
int r=21129;
long s=2911143;
float x=11.29f;
double y=11.29143;
//Logarithmic outputs
System.out.println(Math.log(p));
System.out.println(Math.log(q));
System.out.println(Math.log(r));
System.out.println(Math.log(s));
System.out.println(Math.log(x));
System.out.println(Math.log(y));
}
}
This Math function returns the base 10 logarithmic
value of the input variable. This function is also applicable to all primitive
Data types.
class java {
public static void main(String args[]) {
//Declaring variables
byte p = 112;
short q=3200;
int r=21129;
long s=2911143;
float x=11.29f;
double y=11.29143;
//Logarithmic base 10 outputs
System.out.println(Math.log10(p));
System.out.println(Math.log10(q));
System.out.println(Math.log10(r));
System.out.println(Math.log10(s));
System.out.println(Math.log10(x));
System.out.println(Math.log10(y));
}
}

Math.log1p(x)
This logarithmic function is quite interesting and
different. It generates different outputs depending on the case under which it
follows:
Case (i) Positive input
When the input is a positive variable and when this
log1p operation is applied, it results in positive logarithmic output.
Case (ii) Negative input
When the input is Not a Number (NaN) or a negative
variable, the output is also NaN.
Case (iii) Input = -1
When the input is -1, the output becomes a negative
infinity value.
Case (iv) Input = Infinity
When the input is positive infinity (1.0/0), the
output is Infinite and if the input is a negative infinity value (-1.0/0), the
output is NaN.
Case (v) Input =0
When the input applied to the log1p function is 0, the
output is 0 and in case of -0, the output is also -0.
class java {
public static void main(String args[]) {
//Declaring variables
byte p = 112;
short q=-3200;
int r=-1;
double x=1.0/0;
double y=0.0;
double z=-0.0;
//Log1p outputs
System.out.println("Output for
positive input");
System.out.println(Math.log1p(p));
System.out.println("Output for
negative input");
System.out.println(Math.log1p(q));
System.out.println("Output for Input=
-1");
System.out.println(Math.log1p(r));
System.out.println("Output for
infinity input");
System.out.println(Math.log1p(x));
System.out.println("Output for
positive 0");
System.out.println(Math.log1p(y));
System.out.println("Output for
negative 0");
System.out.println(Math.log1p(z));
}
}
Math.exp(x)
As the name suggests, this math function returns the exponential value of the input variable.
|
Case |
Input |
Output |
|
(i) |
Positive/ Negative variable |
e^x |
|
(ii) |
Positive
Infinity |
Infinity |
|
(iii) |
Negative Infinity |
0 |
|
(iv) |
Positive/
Negative 0/ NaN |
1 |
class java {
public static void main(String args[]) {
//Declaring variables
byte p = 112;
short q=-34;
double x=1.0/0;
double x1=-1.0/0;
double y=0.0;
double z=-0.0;
double z1=0.0/0;
//Log1p outputs
System.out.println("Output for
positive input");
System.out.println(Math.exp(p));
System.out.println("Output for
negative input");
System.out.println(Math.exp(q));
System.out.println("Output for
Positive infinity");
System.out.println(Math.exp(x));
System.out.println("Output for
Negative infinity");
System.out.println(Math.exp(x1));
System.out.println("Output for
positive 0");
System.out.println(Math.exp(y));
System.out.println("Output for
negative 0");
System.out.println(Math.exp(z));
System.out.println("Output when
input is NaN");
System.out.println(Math.exp(z));
}
}
Output
Math.expm1()
The following outputs are generated for these input cases stated below:
|
Case |
Input |
Output |
|
(i) |
Positive/ Negative |
(e^x)-1 ((exponential(x)-1)
|
|
(ii) |
Positive Infinity |
Infinity |
|
(iii) |
Negative Infinity |
-1 |
|
(iv) |
Positive zero |
0 |
|
(v) |
Negative zero/ NaN |
-0 |
class java {
public static void main(String args[]) {
//Declaring variables
double p = 112;
short q=-34;
double x=1.0/0;
double x1=-1.0/0;
double y=0.0;
double z=-0.0;
double z1=0.0/0;
//Log1p outputs
System.out.println("Output for
positive input");
System.out.println(Math.expm1(p));
System.out.println("Output for
negative input");
System.out.println(Math.expm1(q));
System.out.println("Output for
Positive infinity");
System.out.println(Math.expm1(x));
System.out.println("Output for
Negative infinity");
System.out.println(Math.expm1(x1));
System.out.println("Output for
positive 0");
System.out.println(Math.expm1(y));
System.out.println("Output for
negative 0");
System.out.println(Math.expm1(z));
System.out.println("Output when
input is NaN");
System.out.println(Math.expm1(z));
}
}
Output






0 Comments