Board index » delphi » Math problems RadToDeg & Sin

Math problems RadToDeg & Sin

Hi everyone!

I'm currently expering problems in a project of mine. I would like to
calculate the sinand cos for a angel. Delphi calculates the value in
radians. so far everythings works accodring to plan.
But I want to convert it to DEG. When I use the function RadToDeg the answer
returned doesn't match the value I get when calculating the Degree sin on a
ordinary calculator.

for example sin 45 degrees should be approx 0,707xxx. when calculated with
Delphi and then converted using RadToDeg I get an answer saying 30.xx

Can someone give me a clue on how to get around this problem?

Thank you on beforehand

Fredrik Tholander
Sweden

 

Re:Math problems RadToDeg & Sin


Hello Fredrik

You have to think different. (some smartass said that some time back,
can't remember who though :)

I think you're trying to do this (because you mention RadToDeg, which is
completely useless in this context):

X := RadToDeg(Sin(45));

The manuals are a little confusing here:

Quote
>The Sin function returns the sine of the argument.
>X is a real-type expression. Sin returns the sine of the angle X in >radians.                                          

It says "returns the sine of the value X in radians", which can make
you think it returns a radian value if you read it a little too fast :)
However, Sin, Cos and Tan should only return values between 0.0 and 1.0
(according to my math teacher).

The right interpretation of the manuals is that you give the
function a radian angle, and it returns a value between 0 and 1:
X := Sin(DegToRad(45)); (returns 0.707*** which is correct!)

Happy hacking,
- David

Quote
Fredrik T wrote:

> Hi everyone!

> I'm currently expering problems in a project of mine. I would like to
> calculate the sinand cos for a angel. Delphi calculates the value in
> radians. so far everythings works accodring to plan.
> But I want to convert it to DEG. When I use the function RadToDeg the answer
> returned doesn't match the value I get when calculating the Degree sin on a
> ordinary calculator.

> for example sin 45 degrees should be approx 0,707xxx. when calculated with
> Delphi and then converted using RadToDeg I get an answer saying 30.xx

> Can someone give me a clue on how to get around this problem?

> Thank you on beforehand

> Fredrik Tholander
> Sweden

Re:Math problems RadToDeg & Sin


Quote
Fredrik T wrote:

> Hi everyone!

> I'm currently expering problems in a project of mine. I would like to
> calculate the sinand cos for a angel. Delphi calculates the value in
> radians. so far everythings works accodring to plan.
> But I want to convert it to DEG. When I use the function RadToDeg the answer
> returned doesn't match the value I get when calculating the Degree sin on a
> ordinary calculator.

> for example sin 45 degrees should be approx 0,707xxx. when calculated with
> Delphi and then converted using RadToDeg I get an answer saying 30.xx

> Can someone give me a clue on how to get around this problem?

> Thank you on beforehand

> Fredrik Tholander
> Sweden

David Skogvold answered already correctly. But let me mention here some
basic issue. Is it really necessary that in Delphi all needs a
prefabricated
procedure or function or a component even for the most primitive jobs?

Simply multiply the radians with 180/pi and you get the degrees and vice
versa.

Every 2 weeks there comes a mail from a C programmer, looking for the
power function in Pascal or Delphi. It is a similar primitive thing:
Power := Exp(ln(base) * ex);

It seems that Delphi is very very bound to "modern times"...

Franz Glaser

Re:Math problems RadToDeg & Sin


Quote
Fredrik T wrote in message <6qd4a2$g...@forums.borland.com>...
>Hi everyone!

>I'm currently expering problems in a project of mine. I would like to
>calculate the sinand cos for a angel. Delphi calculates the value in
>radians. so far everythings works accodring to plan.
>But I want to convert it to DEG. When I use the function RadToDeg the
answer
>returned doesn't match the value I get when calculating the Degree sin on a
>ordinary calculator.

>for example sin 45 degrees should be approx 0,707xxx. when calculated with
>Delphi and then converted using RadToDeg I get an answer saying 30.xx

>Can someone give me a clue on how to get around this problem?

>Thank you on beforehand

>Fredrik Tholander
>Sweden

I had the same problem a while a go . i wrote these functions to solve it.

Steve Beaulieu
S...@Mediaone.net

{***************************************************************************
*  Radians    - converts degrees to radians                                *
*  Degrees    - converts radians to degrees                                *
*  CosD       - cosine in degrees                                          *
*  SinD       - sine in degrees                                            *
*  TanD       - tangent in degrees                                         *
*  CotD       - CoTangent in degrees                                       *
*  ArcTanD    - ArcTangent in degrees                                      *
*  Power      - power a^n                                                  *
*  DMS_TO_RAE - Converts two sets of lat lons                              *
*               to range azimuth and elevation                             *
****************************************************************************

Quote
}

Unit LatMath;

Interface

Const
  Ln10        = 2.30258509299405E+000;  { Ln( 10 )     =  2.3025651 }
  OneOverLn10 = 0.43429448190325E+000;  { 1 / Ln( 10 ) =  0.4342945 }
  PiOver180   = 1.74532925199433E-002;  { Pi / 180     =  0.0174533 }
  PiUnder180  = 5.72957795130823E+001;  { 180 / Pi     = 57.2957795 }

{**************************************************************************}

Function Power(Base : Real; Exponent : Integer) : Real;
Function SinD(Angle : Real) : Real;
Function ArcTanD(Angle : Real) : Real;
Function CotD(Angle : Real) : Real;
Function TanD(Angle : Real) : Real;
Function CosD(Angle : Real) : Real;
Function Degrees(Angle : Real) : Real;
Function Radians(Angle : Real) : Real;
Procedure DMS_To_RAE( VAR LatRef,LonRef,HightRef,
                          LatCase,LonCase,HightCase,
                          ToAZ,FromAz,Rng,El : Real );

Implementation

{***************************************************************************

Quote
}

Function Radians(Angle : Real) : Real;

  Begin
    Radians := Angle * PiOver180   { Angle * Pi / 180 }
  End;
{***************************************************************************

Quote
}

Function Degrees(Angle : Real) : Real;

  Begin
    Degrees := Angle * PiUnder180  { Angle * 180 / Pi }
  End;
{***************************************************************************

Quote
}

Function CosD(Angle : Real) : Real;

  Begin
    CosD := Cos( Radians( Angle ) )
  End;
{***************************************************************************

Quote
}

Function TanD(Angle : Real) : Real;

  Begin
    TanD := Sin( Radians( Angle ) ) / Cos( Radians( Angle ) )
  End;
{***************************************************************************

Quote
}

Function CotD(Angle : Real) : Real;

  Begin
    CotD := Cos( Radians( Angle ) ) / Sin( Radians( Angle ) )
  End;
{***************************************************************************

Quote
}

Function ArcTanD(Angle : Real) : Real;

  Begin
    ArcTanD := Degrees(ArcTan( Angle ))
  End;
{***************************************************************************

Quote
}

Function SinD(Angle : Real) : Real;

  Begin
    SinD := Sin( Radians( Angle ) )
  End;

{***************************************************************************

Quote
}

Function Power(Base : Real; Exponent : Integer) : Real;

  Var
    BPower : Real;
    t      : Integer;

  Begin
    If (Exponent = 0) Then
      Power := 1
    Else
      Begin
        BPower := 1.0;
        For t := 1 To Exponent Do
          BPower := BPower * Base;
        Power := BPower
      End
  End;

{***************************************************************************

Quote
}

{**************************************************************************}
{***************************************************************************
*  LatRef    = INPUT Latitude of Refferance site In Degrees                *
*  LonRef    = INPUT Longitude of Refferance site In Degrees               *
*  HightRef  = INPUT MSL of Refferance site In Feet                        *
*  LatCase   = INPUT Latitude of Case site In Degrees                      *
*  LonCase   = INPUT Longitude of Case site In Degrees                     *
*  HightCase = INPUT AGL of Case site In Feet                              *
*  RNG       = OUTPUT Distance in feet from reff site to case site         *
*  ToAz      = OUTPUT Azimuth in Degrees from reff site to case site       *
*  FromAz    = OUTPUT Azimuth in degrees from case sith to reff site       *
*  El        = OUTPUT  Elevation Angel from reff site                      *
*                      to case site (5/6 earth curve)                      *
****************************************************************************

Quote
}

Procedure DMS_To_RAE( VAR LatRef,LonRef,HightRef,
                          LatCase,LonCase,HightCase,
                          ToAZ,FromAz,Rng,El : Real );

Const
  Goof = 0.0000000001;

VAR
  LonDiff, TMP1, TMP2,
  LatRefmLatcaseD2,
  LatRefPLatcaseD2     : REAL;

Begin

  If LatRef = LatCase Then LatRef := LatRef + Goof;
  If LonRef = LonCase Then LonRef := LonRef + Goof;
  If LonCase >LonRef Then
    LonDiff := LonCase - LonRef
  Else
    LonDiff := LonRef - LonCase;
  LatRefmLatcaseD2 := (LatRef-LatCase)/2;
  LatRefPLatcaseD2 := (LatRef+LatCase)/2;
  Tmp1 :=  ArcTanD( CotD(LonDiff/2)*SinD(LatRefMLatcaseD2)
          /CosD(LatRefPLatcaseD2) );
  Tmp2 :=  ArcTanD( CotD(LonDiff/2)*CosD(LatRefMLatcaseD2)
          /SinD(LatRefPLatcaseD2) );
  If LonRef>LonCase Then
    Begin
     ToAz := (tmp2+tmp1);
     FromAz := 360 - (Tmp2 - Tmp1);
    End
  Else
   Begin
     ToAz := 360 - (Tmp2 + Tmp1);
     FromAz := (Tmp2 - Tmp1);
   End;

  Rng :=((ArcTanD( TanD(LatRefMLatcaseD2)*
              SinD(tmp2)/SinD(tmp1)))*2)*60.00*6076.1;

  EL := ArcTanD(((HightCase-HightRef)-
            ((Power(RNG/6076.1,2)*5)/6))/(RNG));

End;
(***************************************************************************
**)

End.

Other Threads