Jump to content

Problem with trigonometric functions

- - - - -

  • Please log in to reply
2 replies to this topic

#1
hampus.tagerud

hampus.tagerud

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
I have been trying to let the user put a value (in degrees), called angle, into my program and then the code will return the sine value of that angle. This works with this code:


double answer = Math.sin(Math.toRadians(angle));


It returns 0.5 when I set angle to 30 so that works correctly... But when I try to reverse it and get the arch sine value it doesn't! I use this code:


double answer = Math.asin(Math.toRadians(angle));


and try it by putting 0.5 into angle which would result in the answer being 30 but it doesn't...
I get the answer 0.008726757025787037 for some reason...

So I tried to convert the answer into degrees again:


double answer = Math.toDegrees(Math.asin(Math.toRadians(angle)));


This code gives me the answer 0.5000063464137362...

What am I doing wrong? Thanks in advance for any help!

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You are required to provide a sine to produce the inverse of the sine (arc is the inverse function of the set, i.e. Attached File  gif.latex?\sin^{-1}#.gi.gif   270bytes   17 downloads,) providing the angle in radians is not a value of which can be calculated.

i.e.
Attached File  gif.latex?y%20=%20\sin%20x#.gi.gif   369bytes   16 downloads
Attached File  gif.latex?x%20=%20\arcsin%20y#.gi.gif   464bytes   15 downloads
Math.toDegrees(Math.asin(Math.sin(radians)));

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
hampus.tagerud

hampus.tagerud

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
I modified my code so it looked more like yours:

Result:
Math.toDegrees(Math.asin(value))

and it works perfectly, so thanks alot!! :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users