Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C's trig functions

Reply
Thread Tools

C's trig functions

 
 
John Smith
Guest
Posts: n/a
 
      04-25-2005
I just broke up with my girlfriend, so, to sublimate my sexual tensions,
I began reading this:

http://forensics.calcinfo.com/

This guy has developed a method that he uses to determine chip lineage
in old calculators (he needs a girlfriend too). Various chips will
produce various results when you make this calculation:

n = sin(cos(tan(atan(acos(asin(9.0)))))) in degrees.

With the Windows calculator I get the following results starting with
sin(9.0)in degree mode:

0.156434465040230869010105319467167
0.99999627274288502411751620501135
0.0174549998554886607913941409283485
0.99999627274288502411751620501135
0.156434465040230869010105319467167
9.00000000000000000000000000000003

I decided to try it with C's trig functions:

#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
#define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))

int main(void)
{
long double n;

n = sin(DEG2RAD(9.0));
printf("%.20Lf\n", n);
n = cos(DEG2RAD(n));
printf("%.20Lf\n", n);
n = tan(DEG2RAD(n));
printf("%.20Lf\n", n);
n = atan(DEG2RAD(n));
printf("%.20Lf\n", n);
n = acos(DEG2RAD(n));
printf("%.20Lf\n", n);
n = asin(DEG2RAD(n));
printf("%.20Lf\n", n);
/* final n = sin(cos(tan(atan(acos(asin(9.0)))))) */

return 0;
}

The program produces this output:

0.15643446504023086901
0.99999627274288502409
0.01745499985548866218
0.00030464720898864997
1.57079100969804273100
0.02741891042497898145

The first three values are OK, but when the inverse functions are
invoked it goes off the tracks. What's wrong?

 
Reply With Quote
 
 
 
 
Gregory Pietsch
Guest
Posts: n/a
 
      04-25-2005
Here's a quick check: Wouldn't the last three calls (atan, acos, asin)
be DEG2RAD(atan(n)), DEG2RAD(acos(n)), and DEG2RAD(asin(n)) instead of
the way you have them?

Happy debugging, Gregory Pietsch

 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      04-25-2005
John Smith wrote:
> I began reading this:
>
> http://forensics.calcinfo.com/
>
> With the Windows calculator I get the following results starting with


> sin(9.0)in degree mode:
>
> 0.156434465040230869010105319467167
> 0.99999627274288502411751620501135
> 0.0174549998554886607913941409283485
> 0.99999627274288502411751620501135
> 0.156434465040230869010105319467167
> 9.00000000000000000000000000000003
>
> I decided to try it with C's trig functions:
>
> #include <stdio.h>
> #include <math.h>
> #define PI 3.14159265358979323846
> #define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))
>
> int main(void)
> {
> long double n;
>
> n = sin(DEG2RAD(9.0));
> printf("%.20Lf\n", n);
> n = cos(DEG2RAD(n));
> printf("%.20Lf\n", n);
> n = tan(DEG2RAD(n));
> printf("%.20Lf\n", n);


Fine, to here.

> n = atan(DEG2RAD(n));
> printf("%.20Lf\n", n);
> n = acos(DEG2RAD(n));
> printf("%.20Lf\n", n);
> n = asin(DEG2RAD(n));
> printf("%.20Lf\n", n);


Try...

#define RAD2DEG(RAD) ((RAD)*180/PI)

n = RAD2DEG(atan(n));
printf("%.20Lf\n", n);
n = RAD2DEG(acos(n));
printf("%.20Lf\n", n);
n = RAD2DEG(asin(n));
printf("%.20Lf\n", n);

> /* final n = sin(cos(tan(atan(acos(asin(9.0)))))) */
>
> return 0;
> }


--
Peter

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-26-2005
John Smith <(E-Mail Removed)> writes:
> I just broke up with my girlfriend, so, to sublimate my sexual
> tensions, I began reading this:
>
> http://forensics.calcinfo.com/
>
> This guy has developed a method that he uses to determine chip lineage
> in old calculators (he needs a girlfriend too). Various chips will
> produce various results when you make this calculation:
>
> n = sin(cos(tan(atan(acos(asin(9.0)))))) in degrees.


What do you expect asin(9.0) to do? asin() expects an argument in the
range -1.0 to +1.0, and returns an angle (normally in radians); it
fails if its argument is outside that range. Converting asin()'s
argument from degrees to radians makes no mathematical sense; if you
want to work in degrees, you need to convert the *result* from radians
to degrees.

> With the Windows calculator I get the following results starting with
> sin(9.0)in degree mode:

[...]

Starting with sin(9.0) is inconsistent with the expression above. I
suspect what you mean is:

n = asin(acos(atan(tan(cos(sin(9.0))))))

(where 9.0 is in degrees).

As you know, unlike most interactive calculators, C's trig functions
have no "degree mode", which is why you need to do the conversions
manually. The C equivalent of sin(x), where x is expressed in
degrees, would be sin(DEG2RAD(x)). The C equivalent of asin(x), where
the result is to be expressed in degrees, would be RAD2DEG(asin(x)).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-26-2005
John Smith wrote the demented claim that:
> #define PI 3.14159265358979323846
> #define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))
>
> int main(void)
> {
> long double n;
>
> n = sin(DEG2RAD(9.0));
> printf("%.20Lf\n", n);
> n = cos(DEG2RAD(n));
> printf("%.20Lf\n", n);
> n = tan(DEG2RAD(n));
> printf("%.20Lf\n", n);
> n = atan(DEG2RAD(n));
> printf("%.20Lf\n", n);
> n = acos(DEG2RAD(n));
> printf("%.20Lf\n", n);
> n = asin(DEG2RAD(n));
> printf("%.20Lf\n", n);
> /* final n = sin(cos(tan(atan(acos(asin(9.0)))))) */

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Do you really thing that
asin(acos(atan(tan(cos(sin(x)))))
is the same operationally as
sin(cos(tan(atan(acos(asin(x)))))?
 
Reply With Quote
 
Thomas
Guest
Posts: n/a
 
      04-26-2005
??,
"Keith Thompson" <(E-Mail Removed)> ???? news:(E-Mail Removed)...
> John Smith <(E-Mail Removed)> writes:
> > I just broke up with my girlfriend, so, to sublimate my sexual
> > tensions, I began reading this:
> >
> > http://forensics.calcinfo.com/
> >
> > This guy has developed a method that he uses to determine chip lineage
> > in old calculators (he needs a girlfriend too). Various chips will
> > produce various results when you make this calculation:
> >
> > n = sin(cos(tan(atan(acos(asin(9.0)))))) in degrees.

>
> What do you expect asin(9.0) to do? asin() expects an argument in the
> range -1.0 to +1.0, and returns an angle (normally in radians); it
> fails if its argument is outside that range. Converting asin()'s
> argument from degrees to radians makes no mathematical sense; if you
> want to work in degrees, you need to convert the *result* from radians
> to degrees.
>
> > With the Windows calculator I get the following results starting with
> > sin(9.0)in degree mode:

> [...]
>
> Starting with sin(9.0) is inconsistent with the expression above. I
> suspect what you mean is:
>
> n = asin(acos(atan(tan(cos(sin(9.0))))))
>
> (where 9.0 is in degrees).
>
> As you know, unlike most interactive calculators, C's trig functions
> have no "degree mode", which is why you need to do the conversions
> manually. The C equivalent of sin(x), where x is expressed in
> degrees, would be sin(DEG2RAD(x)). The C equivalent of asin(x), where
> the result is to be expressed in degrees, would be RAD2DEG(asin(x)).
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed)

<http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*>

<http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-26-2005
"Thomas" <(E-Mail Removed)> writes:
> ??,
> "Keith Thompson" <(E-Mail Removed)> ???? news:(E-Mail Removed)...

[snip]

You wrote two question marks and then quoted my entire article.

Did you have a question?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      04-26-2005
Keith Thompson wrote:

> Starting with sin(9.0) is inconsistent with the expression above. I
> suspect what you mean is:
>
> n = asin(acos(atan(tan(cos(sin(9.0))))))


About two minutes after I posted this, I realized I had written it
backwards.
>
> (where 9.0 is in degrees).
>
> As you know, unlike most interactive calculators, C's trig functions
> have no "degree mode", which is why you need to do the conversions
> manually. The C equivalent of sin(x), where x is expressed in
> degrees, would be sin(DEG2RAD(x)). The C equivalent of asin(x), where
> the result is to be expressed in degrees, would be RAD2DEG(asin(x)).
>


I hate to look stupid, but thanks to you and others who pointed out my
mistakes.

JS
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: trig functions (in java and c++) kwikius Java 1 05-22-2008 06:56 PM
Re: trig functions (in java and c++) kwikius C++ 1 05-22-2008 06:56 PM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
OT: Tuesday Trig FrisbeeŽ MCSE 4 03-15-2006 06:01 AM
SortExpression makes SortCommand event not to trig =?Utf-8?B?UGF1bA==?= ASP .Net 0 09-22-2005 06:36 PM



Advertisments