Board index » delphi » How To convert COS(n) to an angle? "Simple Maths ??"

How To convert COS(n) to an angle? "Simple Maths ??"

Hello, Thanks for reading this message.

To do with mathematics. Namely TrigONOmetry!

For a right angle triangle.  COS angle = Adjacent Side / Hypotenuse .

ie for a 3,4,5 Triangle...

cos (4/5) or cos(0.8) returns 0.69 in radians, And my book tells me to
first press the INV key, the inverse key,  then the COS key, followed by
0.8.  Which works fine on a calculator returning 36.9 degrees. But not so
in Turbo ver 3, as the INVerse function is un supported.

So, How can I write code to return degrees when supplied radians.
Moreover, are there sites containing mathematical functions to do just
this, which I may be allowed to use, and that will work OK in TP3.

Thanks again, Brian.

 

Re:How To convert COS(n) to an angle? "Simple Maths ??"


In article <19971117013801.UAA28...@ladder02.news.aol.com>,

Quote
BRI MELLO <brime...@aol.com> wrote:
>Hello, Thanks for reading this message.

>To do with mathematics. Namely TrigONOmetry!

>For a right angle triangle.  COS angle = Adjacent Side / Hypotenuse .

>ie for a 3,4,5 Triangle...

>cos (4/5) or cos(0.8) returns 0.69 in radians, And my book tells me to
>first press the INV key, the inverse key,  then the COS key, followed by
>0.8.  Which works fine on a calculator returning 36.9 degrees. But not so
>in Turbo ver 3, as the INVerse function is un supported.

>So, How can I write code to return degrees when supplied radians.

What you want to convert radians to degrees? You do not need any
trigonometric functions for that, just multiply the value with (180/pi)

Osmo

Re:How To convert COS(n) to an angle? "Simple Maths ??"


I'll solve half your question.

        ArcCos(x) = ArcTan(Sqrt(1 - Sqr(x)) / x);
        ArcSin(s) = ArcTan(x / Sqrt(1 - Sqr(x)));
        Tan(x) = Sin(x) / Cos(x);

ArcCos is the inverse of Cos etc.

As far as converting radians to degrees are concerned, just remember that
Pi = 180 degrees. Thus if x is a value in radians.
Then
        Degrees(x) = (180 * x) / Pi

Now you go write some code!

Re:How To convert COS(n) to an angle? "Simple Maths ??"


In article <01bcf36d$31ba2980$02531...@lexlines.iafrica.com>,

Quote
BASTARD <lexli...@iafrica.com> wrote:
>I'll solve half your question.

>    ArcCos(x) = ArcTan(Sqrt(1 - Sqr(x)) / x);
>    ArcSin(s) = ArcTan(x / Sqrt(1 - Sqr(x)));

Note that the two above fail at (or close to) 0 and 1 respectively. One
might solve this for example something like:

Function arcsin(x:real):real; forward;

Function arcCos(x:real):real;
begin
  if abs(x)>0.001 then
     ArcCos:=ArcTan(Sqrt(1 - Sqr(x)) / x)
   else
     if x<0 then Arccos:=-pi/2+arcSin(-x)
            else Arccos:=pi/2-arcSin(x)
End;

Function ArcSin(x:real):real;
Begin
  if abs(abs(x)-1)>0.001 then
     ArcSin := ArcTan(x / Sqrt(1 - Sqr(x)))
  else
    if x<0 then ArcSin:=-pi/2+arcCos(-x)
           else ArcSin:=pi/2-arcCos(x)
End;

This is based on the fact that sin(x)=cos(pi/2-x). The threshold is drawn
from a hat.

A simple test for 0 or 1/-1 respectively might also work in most cases.
Note that handling these errors is important as they are caused in a
situation where the actual function is defined, unlike say the error
caused by tan() when cos(x)=0.

Quote
>    Tan(x) = Sin(x) / Cos(x);

Osmo

Re:How To convert COS(n) to an angle? "Simple Maths ??"


In article <64p8ju$...@kruuna.Helsinki.FI>, ronka...@cc.helsinki.fi (Osmo

Quote
Ronkanen) writes:

>What you want to convert radians to degrees? You do not need any
>trigonometric functions for that, just multiply the value with (180/pi)

>Osmo

Thanks for the tip Osmo,

Using the example I gave ie Cos(0.8) returning 0.69 radians...
0.69 * (180/pi) =  39.92 degrees

Unfortunately this differs from my text book example.

ie 0.8 inverse cos = 36.9 degrees a full 3 degrees difference!

Brian.

Re:How To convert COS(n) to an angle? "Simple Maths ??"


On 17 Nov 1997 20:37:22 +0200, ronka...@cc.helsinki.fi (Osmo Ronkanen)
wrote:

Quote
>Note that the two above fail at (or close to) 0 and 1 respectively. One
>might solve this for example something like:

>Function arcsin(x:real):real; forward;

>Function arcCos(x:real):real;
>begin
>  if abs(x)>0.001 then
>     ArcCos:=ArcTan(Sqrt(1 - Sqr(x)) / x)
>   else
>     if x<0 then Arccos:=-pi/2+arcSin(-x)
>            else Arccos:=pi/2-arcSin(x)
>End;

>Function ArcSin(x:real):real;
>Begin
>  if abs(abs(x)-1)>0.001 then
>     ArcSin := ArcTan(x / Sqrt(1 - Sqr(x)))
>  else
>    if x<0 then ArcSin:=-pi/2+arcCos(-x)
>           else ArcSin:=pi/2-arcCos(x)
>End;

>This is based on the fact that sin(x)=cos(pi/2-x). The threshold is drawn
>from a hat.

Thresholds inside a mathematical function should be avoided if
possible because the optimal threshold depends on the implementation
of the floating point data type. Here it is easy to avoid it by force
using a little bit of trigonometry.

{$N+}
type float=extended;

function arccos(c:float):float;
{        ArcusCosinus
  Input:    1 >=    c   >= -1
  Output:   0 <= arccos <= pi
          0 deg          180 deg

Quote
}

var
  val:float;
begin
  val:=2*arctan(sqrt((1-abs(c))/(1+abs(c))));
  if c<0 then val:=pi-val;
  arccos:=val
end;

function arcsin(s:float):float;
{        ArcusSinus
   Input :  -1      <=   s    <=  1
   Output:  -pi/2   <= arcsin <= pi/2
           -90 deg              90 deg

Quote
}

var
  val:float;
begin
  val:=pi/2-2*arctan(sqrt((1-abs(s))/(1+abs(s))));
  if s<0 then val:=-val;
  arcsin:=val
end;

function atan2(y,x:float):float;
{
  Oriented rotation angle from (1,0) to (x,y).
  May be interpreted as:
  Argument of the complex number x+i*y.
  x+iy = sqrt(sqr(x)+sqr(y))*exp(i*atan2(y,x))
  Angle phi of the representation of (x,y) in
  polar coordinates.
  r=sqrt(sqr(x)+sqr(y)) ; phi=atan2(y,x)
  x=r*cos(phi) ; y=r*sin(phi)  

  For compatibility with other programming
  languages, the order of parameters is (y,x)
  and the output range is

    -pi     < atan2 <=   pi
    -180 deg             180 deg

Hint: In order to compute the oriented rotation angle
from (x1,y1) to (x2,y2) for arbitrary non-null vectors
use the formula:

     phi = atan2(x1*y2-y1*x2,x1*x2+y1*y2) { *180/pi };

Quote
}

var
  val:float;
begin
  val:=2*arctan(abs(y)/(sqrt(sqr(y)+sqr(x))+abs(x)));
  if x<0 then val:=pi-val;
  if y<0 then val:=-val;
  atan2:=val
end;

A more pedestrian and still safe implementation avoiding overflows
would be

function arcsin(s:float):float;
begin
  if s=-1 then arcsin:=-pi/2
  else if s=1 then arcsin:=pi/2
  else arcsin := arctan(s/sqrt(1-sqr(s)))
end;

function arccos(c:float):float;
begin
  if c=-1 then arccos:=pi
  else if c=1 then arccos:=0
  else arccos := pi/2 - arctan(c/sqrt(1-sqr(c)))
end;

or just

function arccos(c:float):float;
begin
  arccos := pi/2 - arcsin(c)
end;

If x compares FALSE to +-1 then abs(x)=1-delta and delta is not less
than the float-epsilon 'eps' for the chosen data type. With any
correct implementation of *, sqr(x) will be less than 1-eps and
1-sqr(x) greater than eps. As sqrt(eps) is greater than eps, the
denominator sqrt(1-sqr(x)) is greater than eps. As abs(x) is smaller
than 1 abs(quotient) is smaller than 1/eps. For every implementation
of floating point numbers 1/eps < MAXFLOAT. arctan(x) is never a
potential overflow problem because it is calculated via arctan(1/x)
and a transformation of the angle.

Regards
Horst

Re:How To convert COS(n) to an angle? "Simple Maths ??"


In article <19971118123400.HAA01...@ladder02.news.aol.com>,

Quote
BRI MELLO <brime...@aol.com> wrote:
>In article <64p8ju$...@kruuna.Helsinki.FI>, ronka...@cc.helsinki.fi (Osmo
>Ronkanen) writes:

>>What you want to convert radians to degrees? You do not need any
>>trigonometric functions for that, just multiply the value with (180/pi)

>>Osmo

>Thanks for the tip Osmo,

>Using the example I gave ie Cos(0.8) returning 0.69 radians...

That statement makes no sense. The 0.8 is in radians. The value given by
cosine is just a value.

Quote
>0.69 * (180/pi) =  39.92 degrees

>Unfortunately this differs from my text book example.

>ie 0.8 inverse cos = 36.9 degrees a full 3 degrees difference!

I said that you do not need any trigonometric functions to convert
between radians and degrees. Just multiply by (180/pi). 0.8*180/pi=
45.84 degrees.

Now what on earth do you want to achieve?

Osmo

Re:How To convert COS(n) to an angle? "Simple Maths ??"


In article <01bcf36d$31ba2980$02531...@lexlines.iafrica.com>, "BASTARD"

Quote
<lexli...@iafrica.com> writes:

>As far as converting radians to degrees are concerned, just remember that
>Pi = 180 degrees. Thus if x is a value in radians.
>Then
>    Degrees(x) = (180 * x) / Pi

>Now you go write some code!

I posed the origional subject of converting radians to degrees, given INV cos
0.8 returning 36.9 deg on a calculator, and applied the above formula to TP3's
Cos function, which did not. I am still confused about  this but relived to say
that tp3 ArcTan (opp/adj) works perfectly with the above formula.

That is..
  opp := 3;
  adj := 4;
  Hyp :=5;

  Angle := 180 * ArcTan(opp/adj)/pi { Returned 36.9 degrees! }

However...

  Angle := 180 * Cos(adj/hyp)/ Pi ; { Returned 39.?? degrees }

Why one works and the other not is beyond me.  I am grateful to all, for your
response and grateful that I found a solution. Which is to be used in the code
for the old Atari Classic, Missile Command.

Peace!

Brian.

Re:How To convert COS(n) to an angle? "Simple Maths ??"


In article <19971121020900.VAA19...@ladder02.news.aol.com>,

Quote
BRI MELLO <brime...@aol.com> wrote:
>In article <01bcf36d$31ba2980$02531...@lexlines.iafrica.com>, "BASTARD"
><lexli...@iafrica.com> writes:

>>As far as converting radians to degrees are concerned, just remember that
>>Pi = 180 degrees. Thus if x is a value in radians.
>>Then
>>        Degrees(x) = (180 * x) / Pi

>>Now you go write some code!

>I posed the origional subject of converting radians to degrees, given INV cos
>0.8 returning 36.9 deg on a calculator, and applied the above formula to TP3's
>Cos function, which did not. I am still confused about  this but relived to say
>that tp3 ArcTan (opp/adj) works perfectly with the above formula.

>That is..
>  opp := 3;
>  adj := 4;
>  Hyp :=5;

>  Angle := 180 * ArcTan(opp/adj)/pi { Returned 36.9 degrees! }

>However...

>  Angle := 180 * Cos(adj/hyp)/ Pi ; { Returned 39.?? degrees }

You are mixing Cos with ArcCos:

Cos(36.9*pi/180) = 0.8 = adj/hyp

As I said before the result of cosine is not in radians. It is just a
number. Cosine takes an angle and produces the fraction between
adjacent side and hypotenuse.

If you want ArcCos you may try following:

function ArcSin(x:real):real;
begin
  if x=1.0 then ArcSin:=pi/2
    else if x=-1.0 then ArcSin:=Pi/2
      else ArcSin:=ArcTan(x/Sqrt(1.0-Sqr(x)));
End;

function ArcCos(x:real):real;
begin
  ArcCos:=pi/2-ArcSin(x)
end;

Try those and you will get 36.86990

Quote

>Why one works and the other not is beyond me.  I am grateful to all, for your
>response and grateful that I found a solution. Which is to be used in the code
>for the old Atari Classic, Missile Command.

>Peace!

>Brian.

Osmo

Other Threads