2.8 Predefined Functions
The possibility of defining new functions is fundamental for increasing the language’s flexibility and its ability to adapt to the problems we want to solve. The new functions, however, must be defined in terms of others that were either defined by the user or, at most, predefined in the language.
As we will see, Julia has a vast set of predefined functions. In many cases, they suffice for what we want to do. Nevertheless, we should not restrain from defining new functions whenever we deem it necessary.
The name sqrt is a contraction of the words square and root
Some mathematical functions predefined in Julia.
Function
Arguments
Result
abs
A number
The absolute value of the argument.
sin
A number
The sine of the argument (in radians).
cos
A number
The cosine of the argument (in radians).
atan
One or two numbers
With only one argument, the arc tangent of the argument (in radians). With two arguments, the arc tangent of the division between the first and the second, where the sign of the arguments is used to determine the quadrant.
sqrt
A number
The square root of the argument.
exp
A number
The exponential value with base \(e\) of the argument.
^
Two numbers
The first argument raised to the power of the second argument.
log
One or two arguments
With one argument, the natural logarithm of the argument. With two arguments, the logarithm of the second argument with the first argument as its base.
max
Multiple Numbers
The highest argument.
min
Multiple Numbers
The lowest argument.
round
A number
Rounds the argument to the nearest integer.
floor
A number
Rounds down the argument to the nearest integer.
ceil
A number
Rounds up the argument to the nearest integer.
Julia’s predefined math functions.
2.8.1 Exercises 5
2.8.1.1 Question 13
\(\sqrt{\frac{1}{\log 2^{\left|(3-9\log 25)\right|}}}\)
\(\frac{\cos^4 \frac{2}{\sqrt 5}}{\arctan 3}\)
\(\frac{1}{2} + \sqrt 3 + \sin^{\frac{5}{2}} 2\)
2.8.1.2 Question 14
log(sin(2^4+floor(atan(pi))/sqrt(5)+pi))
cos(cos(cos(0.5)))^5
sin(cos(sin(pi/3)/3)/3)
2.8.1.3 Question 15
Define the function odd that, for a given number, evaluates if it is odd, i.e., if the remainder of that number when divided by two is one.
2.8.1.4 Question 16
The area \(A\) of a pentagon inscribed in a circle of radius \(r\) is given by the following expression: \[A = \frac{5}{8}r^2\sqrt{10 + 2\sqrt{5}}\] Define a function that calculates this area and test it with values of your choice.
2.8.1.5 Question 17
Define a function that calculates the volume of an ellipsoid with semi-axes \(a\), \(b\) and \(c\). This volume can be obtained by using the formula: \(V=\frac{4}{3}\pi a b c\)