The Curse of atan() and the Story of atan: A Story of Disorientation in the Coding World

DDD
Release: 2024-10-24 19:02:02
Original
962 people have browsed it

atan() এর অভিশাপ এবং atan এর কাহিনী: কোডিং জগতে দিশা হারানোর গল্প

Hello Coders Pirates ?‍☠️ Have you ever found yourself lost in the coding sea, just trying to figure out the angle using atan(), and frustrated that you can't find the right direction. you are not alone Today we are going on an adventure, where we will find the treasure called atan2() in PHP. Let's see why atan() can put you in danger, while atan2() will get you safely to port.

Let's start the journey then. ⛵

Imagine you have a treasure map. There is talk of an island, which is navigated to a point (x, y) on the map. You can use atan()—but stop! It is only part of the story. atan() can take you straight to Davy Jones' locker, or worse, to a codebase full of bugs. ? So if you want to get the right path, you need atan2(), the Captain Jack Sparrow of this story.

The story of atan()'s curse

This atan() function can be found in almost all languages. We talk about php. atan() is mainly used to calculate angles. But here's a problem — atan() only cares about the y/x ratio, it doesn't care what quadrant you're in. If you start trusting him completely, you may find that you are going in the wrong direction. Why? Because atan() doesn't return the whole image—it only tells the angle relative to the first quadrant.

Why? Because atan() doesn't give the whole image — it only tells the angle relative to the first quadrant. That means when you are going west, you should actually be traveling east! atan2(), on the other hand, takes both x and y coordinates into account, and—like a good compass—knows exactly which quadrant you're in.

Now let's know a little more about the difference between these 2.

Difference between atan() and atan2()

Now let's say it in code language:

  • atan() calculates the arctangent of the ratio y/x, but doesn't know the whole image. Is the target in the second quadrant? Third? atan() has no idea and doesn't even bother.

The parameter of the atan() function is a number, which is the value of the ratio y/x, where y and x are two variables or number values.

 atan(float $num): float
Copy after login
Copy after login
  • On the other hand, atan2() knows exactly where the (x, y) point is and returns the correct angle in any quadrant.
The parameters of

atan2() function are 2.

  • $y: Y coordinate value.
  • $x: X coordinate value.
 atan2(float $y, float $x): float
Copy after login
Copy after login

Let's try to understand the matter with an example.

// $y এবং $x এর মান নির্ধারণ
$y = 5;
$x = -10;

// atan() ব্যবহার - শুধুমাত্র y/x অনুপাত জানে
$angle1 = atan($y / $x);
echo "atan() angle: " . rad2deg($angle1) . " degrees\n";
// Outputs: -26.57 degrees

// atan2() ব্যবহার - $x এবং $y উভয়ই বিবেচনায় নেয়
$angle2 = atan2($y, $x);
echo "atan2() angle: " . rad2deg($angle2) . " degrees\n";
// Outputs: 153.43 degrees
Copy after login

As you can see from the example, atan() does return an angle. But the direction is not telling. That is, it is not understood which quadrant you are in. On the other hand, the value we get with the atan2() function clearly shows that the point (x, which) is in the 2nd quadrant. Now we can easily understand which direction the treasure island is.

?Radians vs Degrees (It's not just math, it's living!)

PHP's atan() and atan2() functions give you an angle in radians. Which looks like a joke! Don't worry—it's just a different method of measuring angles. For humans and pirates to understand, I converted it to degrees using the rad2deg() function.

My own atan() and atan2()

Let us now try to make atan() and atan2() our own, to better understand their operation.

⚠️ If you feel like this is going over your head, skip this section! ?

Let's create atan()

atan() calculates the arctangent of a number. One way to approximate this function is to use Gregory's series. Here is a commonly used series for atan(z):

atan( x)n=0(1)nx2n 12n 1 here(x) approx sum_{n=0}^{infty} frac{(-1)^n x^{2n 1}}{2n 1} here(x)≈n=0 2n 1(−1)nx2n 1

which if written a little straighter stands -

atan(x)xx33 x55x77 atan(x) approx x - frac{x^3}{3} frac{x^5}{5} - frac{x^7}{7}ldots atan(x)≈x−3x3 5x57x7

Now let's write this series in code

 atan(float $num): float
Copy after login
Copy after login

Let's create atan2()

Now we will use the atan() function to create

atan2(). The main task of this function is to identify the correct quadrant only. So why delay, let's write -

 atan2(float $y, float $x): float
Copy after login
Copy after login

Let me tell you the whole story now

Now we look at the four quadrant results and explain each quadrant:

  • First Quadrant (Quadrant I): when x=10x = 10x=10 And y=5y = 5y=5 Then atan($y / $x) is 26.57 degrees and atan2($y, $x) is 26.56 degrees. Here in both cases it is in the first quarter.
  • Second Quadrant (Quadrant II): when x=10x = -10 x=−10 And y=5y = 5y=5 Then atan($y / $x) is -26.57 degrees and atan2($y, $x) is 153.43 degrees. Here atan2() is clearly indicating the second quarter.
  • Third Quadrant (Quadrant III): when x=10x = -10 x=−10 And y=5y = -5 y=−5 Then atan($y / $x) is -26.57 degrees and atan2($y, $x) is -233.43 degrees. It is in the third quarter.
  • Fourth Quadrant (Quadrant IV): when x=10x = 10x=10 And y=5y = -5 y=−5 Then atan($y / $x) is -26.57 degrees and atan2($y, $x) is -26.56 degrees. Here it is in the fourth quarter.

☠️ Learning from mistakes

So next time you're navigating the dangerous ocean of angles and coordinates, don't just rely on atan(). Use atan2() and steer your ship in the right direction every time. It's the compass that will keep you out of the dreaded Davy Jones locker of miscalculations!

Hope your treasure journey goes well.

If you want to know more about atan() and atan2(), visit the following links:

  • https://www.php.net/manual/en/function.atan.php
  • https://www.php.net/manual/en/function.atan2.php

The above is the detailed content of The Curse of atan() and the Story of atan: A Story of Disorientation in the Coding World. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!