Examples: sin and ln functions on a calculator
|
|
Furthermore, a computer allows you to use variables as arguments to a Mathematical function
In other words:
|
|
Note: a bit of history
|
| Java's method name | Corresponding Mathematical function |
|---|---|
| Math.sin(x) | Sine function of value x |
| Math.cos(x) | Cosine function of value x |
| Math.tan(x) | Tangent function of value x |
| Math.asin(x) | Arc sine (inverse of sine) function of value x |
| Math.acos(x) | Arc cosine function of value x |
| Math.atan(x) | Acr tangent function of value x |
| Math.exp(x) | ex |
| Math.log(x) | Natural log function of value x |
| Math.log10(x) | 10-based log function of value x |
| Math.pow(a, b) | ab |
| Math.sqrt(x) | Squared root of the number x |
Math.sqrt(2.0) = √2
|
Examples:
double a;
Math.sqrt(a) will compute the squared root on the value
that is current stored in the variable a
Math.sqrt(a+1) will compute the squared root
on the value a+1
|
Consider the following Java program:
public class CompVal
{
public static void main(String[] args)
{
Math.sqrt(2.0); // Computes √2
}
}
|
Interesting result:
|
How to run the program:
|
|
Example:
public class CompVal2
{
public static void main(String[] args)
{
System.out.println( Math.sqrt(2.0) ); // Print !!
}
}
|
This program will print: 1.4142135623730951 (which is the decimal value of √2)
|
Example: storing a computed value
public class CompVal3
{
public static void main(String[] args)
{
double a;
a = Math.sqrt(2.0); // Save computed value in variable
System.out.println(a); // You can print the saved value later
}
}
|
|
Solutions:
x1 =
|
x2 =
|
|
public class Abc
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
a = 1.0;
b = 0.0;
c = -4.0;
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("a = ");
System.out.println(a);
System.out.print("b = ");
System.out.println(b);
System.out.print("c = ");
System.out.println(c);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
|
How to run the program:
|