views:

901

answers:

3

I'm currently trying to get something to run involving trigonometry but I've run across a hitch involving the math.asin function (it also applies to acos and atan but in those cases it less affects what I'm trying to do). The issue is best summarised by two posts from a help thread I found about it elsewhere;

Sorry, I have just tried it again and found that

a = sin(2)
b = asin(a)
b doesnt = 2

but

a = cos(2)
b = acos(a)
b DOES= 2

Because y = sin(x) is a repetitive function, there is more than one value of x for every value of y. ie sin(2) = sin(1.14) = 0.909

Therefore, when you do x = asin(y), you will only ever get a value between -PI/2 <= x <= PI/2

I understand mathematically why this is but I was wondering if anyone could give me a hand in finding all the solutions within a range rather than just the one it gives automatically. Thanks =]

+4  A: 

Let's consider the range [0, 2π).

For acos, each value x also has another possible value at 2π - x. (Picture the cosine graph and you'll see it.)

For asin, each positive value x has another possible value at π - x; each negative value has a possible value at 3π - x.

Feel free to draw further graphs to generalise to greater ranges. :-)

Chris Jester-Young
You can then generalise outside of that range by adding multiples of 2π to those results
andygeers
aah yes, basic trigonometric graphs... thanks for that, I should focus more on actual maths when I code :)
+1 Agree. I don't want to take credit for your comment, so I'll leave the post as is. Thanks!
Chris Jester-Young
3π - x is π - x ;)
SilentGhost
SilentGhost: True in general, but in this case will yield a result outside [0, 2π).andygeers: That's an open invitation to edit my post accordingly, if you wish.
Chris Jester-Young
You should perhaps be a little bit careful here, Chris. In the usual definitions acos has the range [0,2π], but asin has the range [-π/2,π/2]. The OP is looking for all x that map under sin to the same preimage as a did.
simon
+2  A: 

All the solutions for acrsin(a) will be:

b, pi - b, 2pi + b, 2pi + (pi - b), etc.

FarmBoy
+1  A: 

As others have already explained in detail you picked a value for a that leads to indetermined results for asin() due to the (repetitive) nature of the trigonometric functions.

Nevertheless I just wanted to point out that expecting to be able to get the exact same result back on an invers operation with floating points will probably fail due to a more general Floating point accuracy problem

With floating point you can not guarantee that

a == asin(sin(a))

or

a == (a / b) * b

for that matter. Just be careful.

lothar
no it's not. >>> math.pi == 2 + math.asin(math.sin(2)) True
SilentGhost
I agree that he picked the worst number where the result is undefined, but the accuracy problem remains.
lothar
@SilentGhost with floating point there is ALWAYS an accuracy problem. You can not even guarantee a == (a / b) * b with floating point.(And I already acknowledged that he picked the wring number, where the result from sin() is not invertable by asin().
lothar
lothar, you're missing a point. It's true that there is always an issue of floating point, but that's not the problem seen here, which is fundamental. asin(sin(2)) ~= sin(2) even in real numbers, since 2 > pi/2. But it is perfectly well defined.
simon
@simon I edited the answer to make it more clear that I just wanted to add an additional warning about floating point arithmetic.
lothar