C's trig functions

J

John Smith

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

Gregory Pietsch

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
 
P

Peter Nilsson

John said:
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);
 
K

Keith Thompson

John Smith said:
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)).
 
M

Martin Ambuhl

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

Thomas

??,
Keith Thompson said:
John Smith said:
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)).
 
J

John Smith

Keith said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top