C's trig functions

Discussion in 'C Programming' started by John Smith, Apr 26, 2005.

1. John SmithGuest

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

int main(void)
{
long double n;

printf("%.20Lf\n", n);
printf("%.20Lf\n", n);
printf("%.20Lf\n", n);
printf("%.20Lf\n", n);
printf("%.20Lf\n", 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?

John Smith, Apr 26, 2005

2. Gregory PietschGuest

Here's a quick check: Wouldn't the last three calls (atan, acos, asin)
the way you have them?

Happy debugging, Gregory Pietsch

Gregory Pietsch, Apr 26, 2005

3. Peter NilssonGuest

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...

printf("%.20Lf\n", n);
printf("%.20Lf\n", n);
printf("%.20Lf\n", n);

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

--
Peter

Peter Nilsson, Apr 26, 2005
4. Keith ThompsonGuest

John Smith <> 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.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Apr 26, 2005
5. Martin AmbuhlGuest

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)))))?

Martin Ambuhl, Apr 26, 2005
6. ThomasGuest

??,
"Keith Thompson" <> ???? news:...
> John Smith <> 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.ghoti.net/~kst>
> San Diego Supercomputer Center <*>

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

Thomas, Apr 26, 2005
7. Keith ThompsonGuest

"Thomas" <> writes:
> ??,
> "Keith Thompson" <> ???? news:...

[snip]

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

Did you have a question?

--
Keith Thompson (The_Other_Keith) <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.

Keith Thompson, Apr 26, 2005
8. John SmithGuest

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

John Smith, Apr 26, 2005