Board index » delphi » Sin, Cos, Tan Error in delphi 3 C/S

Sin, Cos, Tan Error in delphi 3 C/S

Hi, I have problems with the functions: Sin(X), Cos(X) and Tan(X).

When I use them the result is in radians and that is ok. But when I try to
convert the result into Deg or Grad I get results that do not match the
results on my pocket calculator.

Does anyone know anything about this problem. ????

I use RadToDeg and RadToGrad functions.....

Finn Panvig
fpan...@get2net.dk

 

Re:Sin, Cos, Tan Error in delphi 3 C/S


Im Artikel <4RC22.203$JY4.1...@news.get2net.dk>, "Finn Panvig"
<fpan...@get2net.dk> schreibt:

Quote
>When I use them the result is in radians and that is ok. But when I try to
>convert the result into Deg or Grad I get results that do not match the
>results on my pocket calculator.

>Does anyone know anything about this problem. ????

Give us a code-sample !

Regards, Torsten.

Re:Sin, Cos, Tan Error in delphi 3 C/S


Ok, here is a sample:

I put a lable on a form and write the following small code:

label1.caption := FloatToStr(Sin(5));

This returns: -0,9589242746631 and this is ok because delphi returns result
in radians.

But if I write the following code:

label1.caption := FloatToStr(RadToDeg(Sin(5)));

it returns: -54,9423138108416

And this is wrong because making the same calculation on my calculator or in
windows calculator the result is:

0,08715574274766

And if I write the following code:

label1.caption := FloatToStr(RadToGrad(Sin(5)));

The result is:  -61,0470153453795

Again wrong, and this goes with Cos as well

Therefore Tangent calculations will also be wrong because
Tangent(x) = Sin(x) / Cos(x).

Remeber that as long as I want to use Sin and Cos as radians (delphi
default) it is ok....

Funny enough I dont get any bad calculaitions when ever I try to calc
ArcusSine or ArcusCosine.

Simple because I have right calc's in Sin(X) I have wondering if Delphi's
RadToGrad and RadToDeg is using a bad formel.

Finn Panvig
fpan...@get2net.dk

TBeekhuis skrev i meddelelsen
19981112134649.18118.00000...@ngol01.aol.com>...

Quote

>Im Artikel <4RC22.203$JY4.1...@news.get2net.dk>, "Finn Panvig"
><fpan...@get2net.dk> schreibt:

>>When I use them the result is in radians and that is ok. But when I try to
>>convert the result into Deg or Grad I get results that do not match the
>>results on my pocket calculator.

>>Does anyone know anything about this problem. ????

>Give us a code-sample !

>Regards, Torsten.

Re:Sin, Cos, Tan Error in delphi 3 C/S


JRS:  In article <4RC22.203$JY4.1...@news.get2net.dk> of Thu, 12 Nov
1998 16:08:49 in news:comp.lang.pascal.delphi.misc, Finn Panvig

Quote
<fpan...@get2net.dk> wrote:
>Hi, I have problems with the functions: Sin(X), Cos(X) and Tan(X).

>When I use them the result is in radians and that is ok. But when I try to
>convert the result into Deg or Grad I get results that do not match the
>results on my pocket calculator.

Perhaps you are confused.  The arguments, X, should be in Radians; but
the results are pure numbers not in angular measure.

( HELP ArcCos was wrong in BP7; is it OK in D1..D4 ? )

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v4.00    MIME.
  Web <URL: http://www.merlyn.demon.co.uk/> - TP/BP/&c. FAQqish topics & links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.

Re:Sin, Cos, Tan Error in delphi 3 C/S


Quote
Finn Panvig wrote in message ...
>label1.caption := FloatToStr(Sin(5));

>This returns: -0,9589242746631 and this is ok because delphi returns result
>in radians.

The result of SIN is NOT in radians.
The INPUT to sin, cos, tan is in radians.  The output is a just a number.
For SIN and COS the OUTPUT ranges from -1 to +1.  For TAN the
output ranges from -infinity to +infinity.

Quote
>But if I write the following code:

>label1.caption := FloatToStr(RadToDeg(Sin(5)));

>it returns: -54,9423138108416

All RadToDeg(X) does is X*180/PI
So this answer is -0,9589242746631 * 180 / PI = what you have above

Quote
>And this is wrong because making the same calculation on my calculator or in

No, the above answer is correct as you reported.

Quote
>windows calculator the result is:
>0,08715574274766

You did this on your calculator in "Degrees" mode.
SIN(5 degrees) is 0.87155...
SIN(5 radians) is the value you showed above

Do you want SIN( DegToRad(5) ) ?  This is the SIN(5 degrees).

DegToRad(5) converts 5 degrees to the appropriate number of radians, which
is the correct input for the Delphi Sin function.  You've got to apply the functions
in the correct order to get the correct answer.

Quote
>And if I write the following code:

>label1.caption := FloatToStr(RadToGrad(Sin(5)));
>The result is:  -61,0470153453795

RadToGrad(X) = 200*X/PI
So, 200*-0,9589242746631 /PI  is exactly what you reported.
The Delphi math really is correct!

(By the way, if you have Delphi 3 C/S you can do a Start | Find |
File or Folders in the Program Files/Borland/Delphi 3/Source directory
and find these functions in the math unit.)

Quote
>Simple because I have right calc's in Sin(X) I have wondering if Delphi's
>RadToGrad and RadToDeg is using a bad formel.

I think Delphi is doing the math correctly.

efg
_________________________________
efg's Computer Lab:       www.efg2.com/lab

Earl F. Glynn     E-Mail:  EarlGl...@att.net
Overland Park, KS  USA

Re:Sin, Cos, Tan Error in delphi 3 C/S


Well, whatever what kind of pocket calculator I use, the result differs from
delphi. But other calculaters result's is allways the same.

Earl F. Glynn skrev i meddelelsen <72g2a8$...@bgtnsc02.worldnet.att.net>...

Quote
>Finn Panvig wrote in message ...
>>label1.caption := FloatToStr(Sin(5));

>>This returns: -0,9589242746631 and this is ok because delphi returns
result
>>in radians.

>The result of SIN is NOT in radians.
>The INPUT to sin, cos, tan is in radians.  The output is a just a number.
>For SIN and COS the OUTPUT ranges from -1 to +1.  For TAN the
>output ranges from -infinity to +infinity.

>>But if I write the following code:

>>label1.caption := FloatToStr(RadToDeg(Sin(5)));

>>it returns: -54,9423138108416

>All RadToDeg(X) does is X*180/PI
>So this answer is -0,9589242746631 * 180 / PI = what you have above

>>And this is wrong because making the same calculation on my calculator or
in

>No, the above answer is correct as you reported.

>>windows calculator the result is:
>>0,08715574274766

>You did this on your calculator in "Degrees" mode.
>SIN(5 degrees) is 0.87155...
>SIN(5 radians) is the value you showed above

>Do you want SIN( DegToRad(5) ) ?  This is the SIN(5 degrees).

>DegToRad(5) converts 5 degrees to the appropriate number of radians, which
>is the correct input for the Delphi Sin function.  You've got to apply the
functions
>in the correct order to get the correct answer.

>>And if I write the following code:

>>label1.caption := FloatToStr(RadToGrad(Sin(5)));
>>The result is:  -61,0470153453795

>RadToGrad(X) = 200*X/PI
>So, 200*-0,9589242746631 /PI  is exactly what you reported.
>The Delphi math really is correct!

>(By the way, if you have Delphi 3 C/S you can do a Start | Find |
>File or Folders in the Program Files/Borland/Delphi 3/Source directory
>and find these functions in the math unit.)

>>Simple because I have right calc's in Sin(X) I have wondering if Delphi's
>>RadToGrad and RadToDeg is using a bad formel.

>I think Delphi is doing the math correctly.

>efg
>_________________________________
>efg's Computer Lab:       www.efg2.com/lab

>Earl F. Glynn     E-Mail:  EarlGl...@att.net
>Overland Park, KS  USA

Re:Sin, Cos, Tan Error in delphi 3 C/S


Quote
"Finn Panvig" <fpan...@get2net.dk> wrote:
>Well, whatever what kind of pocket calculator I use, the result differs from
>delphi. But other calculaters result's is allways the same.

Did you read Earl's detailed and quite correct reply?  The Delphi code you
showed us does not perform the same operation you performed on your pocket
calculator.  That's why the results differ.

When your pocket calculator is in degrees mode:
  sin(5) = 0.087155...
When your pocket calculator is in radins mode:
  sin(5) = -0.95892...

What you're doing is altering the meaning of "5", not the meaning of "sin".
"sin" accepts an angle, which is commonly expressed in units of "degrees",
"radians" and "grads".  It is the INPUT to "sin" which has units and needs
converting; the OUTPUT is a unitless number with no units.  It is
essentially a ratio.

In your code, you tried to do this:

  label1.caption := FloatToStr(RadToDeg(Sin(5)))

The Sin function in Delphi ALWAYS expects radians.  So, your statement
computes the sine of 5 radians, and multiplies that unitless result by 57.
This produces nonsense.

I presume what you want to do is to compute the sine of 5 degrees.  Since
the Sin function always expects radians, what you have to do is convert
those degrees to radians, and pass that converted result to Sin:

  label1.caption := FloatToStr(Sin(DegToRad(5)));

THAT should produce a number very close to 0.087155, which should match
your calculator.

- Tim Roberts, t...@probo.com
  Providenza & Boekelheide, Inc.

Other Threads