-6

I'm trying to understand the math behind the Math.tan method but it doesn't make any sense. Can someone please explain to me how it works?

Firstly the mathematical formula to solve for a tangent angle is Tangent = Opposite/Adjacent. Which means I need to know two sides of the triangle to figure out the tangent angle. However the Math.tan method only accept a single argument in radians, not the length of two sides, so I don't understand how it's figuring out the angle of the tangent.

Next in examples they show passing impossibly huge radian values into the method and getting back a value. For example, W3 schools shows the example of Math.tan(90) but 90 radians equals 5,156.6 degrees which is an impossible angle for a corner of a right triangle.

How does this method work, what's happening behind the scenes that turns 90 radians into a tangent angle of -1.995200412208242

efarley
  • 8,371
  • 12
  • 42
  • 65
  • Did you read the fine docs? – Braiam Sep 14 '16 at 23:44
  • 7
    I'm voting to close this question as off-topic because the problem is a lack of minimal understanding of the underlying math concept. The question belongs to http://math.stackexchange.com/ – Oriol Sep 14 '16 at 23:44
  • It would presumably do `angle % (2 * PI)` behind the scenes to get an angle into range. – Ken Y-N Sep 14 '16 at 23:45
  • I understand the math behind figuring out a tangent angle, the question is that the Method doesn't use that math like you would expect. Asking this question outside of javascript developers is just going to result in the same response as googling how to find the angle of a tangent. I.e. Tangent = Opposite/Adjacent, which obviously this method isn't using. – efarley Sep 14 '16 at 23:46
  • That doesn't work out @KenY-N, for example 90 / 2 * PI = 14.3239, where as Math.tan(90) = -1.995200412208242 – efarley Sep 14 '16 at 23:49
  • `%`, not `/`, @efarley – Ken Y-N Sep 14 '16 at 23:50
  • "Tangent" is more general than "Opposite/Adjacent". And 90 radians make completely sense, there is no need to restrict to [0,pi/2]. You are trying to use a function which does something you don't understand. You should first learn what the function is supposed to do. – Oriol Sep 14 '16 at 23:50
  • You don't need Opposite and Adjacent, because they correspond to an angle, and `Math.tan()` takes the corresponding angle as the argument instead. – Barmar Sep 15 '16 at 00:20
  • Unbelievable. Has our math education fallen to this depth? – duffymo Sep 15 '16 at 10:58

1 Answers1

0

First let's talk about what a tangent is: Tangent is y/x for the coordinate at a specific point (see this YouTube video for an example).

So if you want the tangent of pi over 2, on a graph, that's at a 90 degree angle, so the coordinate is (0, 1). Then you just divide 1/0.

However, Math.tan isn't very precise. In Chrome 52, Math.tan(Math.PI / 2) (from the video above) is 16331239353195370, even though it should evaluate to the Infinity value in JavaScript (the mathematical value is actually undefined since it works out to 1/0).

According to this answer, the V8 implementation is:

function MathTan(x) {
  return MathSin(x) / MathCos(x);
}

(As a side note: The value that Chrome actually outputs is larger than Number.MAX_SAFE_INTEGER, which you can prove by running Number.MAX_SAFE_INTEGER < Math.tan(Math.PI / 2) // => true, so Chrome takes it to be "close to infinity".)

Edit

The reason for the lack of a precise value is that pi is generally represented as a fixed value (since we have limitations in computer memory), even though it should be "Infinity" or "undefined" outside of the context of programming. For example, in my browser, Math.PI is fixed at 3.141592653589793.

Community
  • 1
  • 1
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • 3
    "even though it should" --- it should for a `π / 2`, while you have `Math.PI / 2`. `π` is irrational. "However, Math.tan isn't very precise" -- it's not `Math.tan` problem. – zerkms Sep 14 '16 at 23:53
  • it divides the sin by the cos, that's it! :) – efarley Sep 14 '16 at 23:54
  • @zerkms thanks for your comment. Good clarification. I meant that in the world outside of programming, `pi/2` should be "Inifinity" or "undefined", but we have limitations on what numbers we can hold in computer memory. So `Math.PI` in my browser, for example, is only `3.141592653589793`. – Josh Beam Sep 14 '16 at 23:54
  • 2
    @efarley which is entry level trigonometry... – VLAZ Sep 14 '16 at 23:55
  • Yep. You better fix complaining on `Math.tan` precision though :-) – zerkms Sep 14 '16 at 23:55
  • @vlaz not really, since entry level trig tells you that it's the lengths of the opposite side over the adjacent side, nothing mentions dividing the sin over the cos. http://www.mathopenref.com/trigtangent.html – efarley Sep 14 '16 at 23:57
  • @zerkms, haha good call ;) edited my answer. Thanks. – Josh Beam Sep 14 '16 at 23:58
  • The question was about 90 rad, not 90° – Oriol Sep 15 '16 at 00:00
  • @Oriol, correct, thanks. I was giving a generalized example, but it's valid for 90 radians as well (the example was to describe exactly what tangent is, and pi/2, which is 90-degrees, is easier to reason about for the sake of the explanation). – Josh Beam Sep 15 '16 at 00:00
  • 1
    Btw, that's how `Math.tan` is implemented in a modern v8 (not a simple `sin/cos` division anymore): https://github.com/v8/v8/blob/master/src/base/ieee754.cc#L2546 – zerkms Sep 15 '16 at 00:04