acos(): the arc cosine (inverse cosine) of a given number in radians
PHP Math
<?Php
echo acos(0); // Output is 1.5707963267949
echo "<br>";
echo acos(1); // Output 0
echo "<br>";
echo acos(-1); // Output is 3.1415926535898
echo "<br>";
echo acos(-5); // Output is NAN
?>
Syntax
acos(X);
X is the angular value in radian. We get output as acos of the input angle.
It takes input range from +1 to -1 , for any other input it returns NAN ( not a number ). How to check the output by using is_nan() ?
deg2rad() with acos()
We can use deg2rad() to convert Degree value to radian and then use acos().
<?Php
echo acos(deg2rad(0)); // 1.5707963267949
echo "<br>";
echo acos(deg2rad(90)); // NAN
echo "<br>";
echo acos(deg2rad(180)); // NAN
?>
Example: Handling Out-of-Range Values
$value = 2; // Out of range
$result = acos($value);
if (is_nan($result)) {
echo "Invalid input for acos";
}
Output
Invalid input for acos
Example: Calculating Angles in a Triangle Using Law of Cosines
$a = 5; $b = 6; $c = 7;
$angleC = acos(($a*$a + $b*$b - $c*$c) / (2 * $a * $b));
echo rad2deg($angleC); // Output in degrees
Output
78.463040967185
Questions
What does the `acos()` function in PHP do?
What is the purpose of the `acos()` function in PHP?
How is the `acos()` function used to calculate the inverse cosine of a number?
What is the range of values returned by the `acos()` function in PHP?
Can the `acos()` function be used with both degrees and radians?
What happens if you pass an invalid argument to the `acos()` function?
How can you use the `acos()` function to calculate the angle in radians given the cosine value?
Are there any alternative ways to calculate the inverse cosine in PHP besides using the `acos()` function?
Can the `acos()` function return a complex number?
What type of value does the `acos()` function return: integer, float, or something else?
Plotting of SIN & SIN curves by using PHP GD function with deg2rad()
MIN() function →
← Math Functions
Author & Instructor at
plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com