1. Trigonometric functions in Java
When we studied rounding numbers earlier, we got acquainted with the Math
class and some of its methods. We will now look at this class in more detail.
As the name implies, the Math
class contains methods for the mathematical operations most frequently used by programmers. Here are the most interesting ones:
Method | Description | |
---|---|---|
|
Returns the sine of the angle d , in radians |
|
|
Returns the cosine of the angle d , in radians |
|
|
Returns the tangent of the angle d , in radians |
|
|
Returns the arcsine | |
|
Returns the arccosine | |
|
Returns the arctangent | |
|
Returns the hyperbolic sine | |
|
Returns the hyperbolic cosine | |
|
Returns the hyperbolic tangent |
The Math.sin()
, Math.cos()
and Math.tan()
methods take an angle expressed in radians. To convert an angle from degrees to radians and vice versa, the Math
class offers two special methods:
Method | Description |
---|---|
|
Converts an angle from degrees to radians |
|
Converts an angle from radians to degrees |
By the way, in addition to methods, the Math
class also has two constant variables (static fields of the class):
Constant | Description |
---|---|
|
The "Pi" constant is equal to 3.141592653589793 |
|
The "E" constant is equal to 2.718281828459045 |
All these functions can be very useful to you if you decide to write your own games, work with graphics, or simply calculate the length of a path on a map.
For example, if you want to calculate sin(45°)
, here's how you do it:
Math.sin( Math.toRadians(45) )
Here's an example:
public class Main
{
public static int N = 10;
public static void drawValue(double y)
{
int value = (int) (y * N) + N;
for (int i = 0; i < 2 * N; i++)
{
char c = i == N ? '|': '.';
if (i == value)
c = '*';
System.out.print(c);
}
System.out.println();
}
public static void main(String[] args)
{
for (int i = 0; i < 10 * N; i++)
{
double x = i * 1.0 / N;
double y = Math.sin(x);
drawValue(y);
}
}
}
2. Algebraic functions in Java
In most cases, high school math is enough for a programmer: even sines and cosines are very rarely found in code. Most often they are needed when working with games, maps or game engines. 90% of programmers never encounter this.
But besides geometry, programmers sometimes have to use algebraic functions. And, of course, the Math
class contains the most common ones:
Method | Description |
---|---|
|
square root of a |
|
cubic root of a |
|
exponentiation: ab |
|
exponential (Euler's number raised to a power): ea |
|
natural logarithm of a : ln(a) |
|
base-10 logarithm of a : log10(a) |
|
natural logarithm of x + 1 : ln(x + 1) |
|
ex-1 |
If you want the square or cube root of a number, the sqrt(a)
and cbrt(a)
methods are at your service.
The square root can be calculated as follows:
Math.sqrt(2)
If you want to get a root of a higher power, then use the exponentiation method: a
to the power of ¼
is the fourth root, etc.
Math.pow(2, 0.25)
For logarithms and exponents, there are the log(a)
(natural logarithm) and exp(x)
(exponential) methods. To calculate the base-10 logarithm, we have log10(a)
.
If you want the logarithm of a number b
to base a
, use this simple formula: loga(b) = ln(b) / ln(a)
Useful methods
If you are performing calculations involving very small values of x
, then the last two functions — log1p()
and expm1()
— may be useful to you.
When adding very small and very large double
variables, you will often find that very small values are simply ignored (discarded) as insignificant. In fact, this will happen if you use the log()
and exp()
methods. To solve this, programmers came up with functions that return only the "small significant part"
Example:
Suppose you want to calculate the natural logarithm of 1 + x
, where x
is 10-20
. You can't simply pass this number to the log()
method, since if you add 1
and 10-20
, you get 1
. 10-20
is such a small number that it will be completely discarded when the numbers are added.
Because mathematics often involves calculating the log()
of numbers close to 1
, programmers came up with a way to get around this problem: instead of passing the number itself to the method, pass only its difference from 1
.
3. Minimum and maximum
Two more useful functions are min(a, b)
and max(a, b)
. As you probably already guessed, the first returns the minimum of two numbers:
Math.min(a, b)
And the second returns the maximum of two numbers:
Math.max(a, b)
Why do we need these functions when you can always write if
or even use the ternary operator (a < b ? a: b)
?
It's all about the readability of the code. Sometimes your code is overloaded with if
statements and you want to use more compact notation. Let's compare:
Code | Description |
---|---|
|
Using an if-else statement (the longest way to write it) |
|
Using the ternary operator Cons: - bulky code - calculations are performed twice |
|
This is a great way to write it, but it's a little redundant |
|
Just right 🙂 |
4. Minimum and maximum of several numbers
There's another great way to use the min()
and max()
methods.
To calculate the minimum (or maximum) of several numbers or variables. It's very convenient to make nested calls to these methods.
Here's how to find the minimum of 3 numbers:
Math.min(a, Math.min(b, c))
So what? It's super convenient: calculate the minimum of two numbers, and then return the minimum of this number and the third number.
The minimum of four numbers is obtained in the same way:
Math.min(a, Math.min(b, Math.min(с, d)))
That said, we can write this formula a little more clearly:
Math.min(Math.min(a, b), Math.min(c, d))
It all works the same for the max()
method.
Using the if-else
operator or ternary operator would make these code snippets a little more cumbersome. But using the min()
and max()
methods is just perfect.
GO TO FULL VERSION