473,406 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C's trig functions

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?

Nov 14 '05 #1
7 16305
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

Nov 14 '05 #2
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

Nov 14 '05 #3
John Smith <JS****@mail.net> 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) ks***@mib.org <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.
Nov 14 '05 #4
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)))))?
Nov 14 '05 #5
??,
"Keith Thompson" <ks***@mib.org> ???? news:ln************@nuthaus.mib.org...
John Smith <JS****@mail.net> 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) ks***@mib.org

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

Nov 14 '05 #6
"Thomas" <yj***@263.net> writes:
??,
"Keith Thompson" <ks***@mib.org> ???? news:ln************@nuthaus.mib.org...

[snip]

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

Did you have a question?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 14 '05 #7
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
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
2
by: Jon Rea | last post by:
Im looking to do some heavy geomentry based number crunching involving a lot of sin and cos calculations. Performing these operations takes up a lot of computer time, we have calculated ~100...
3
by: Cary West | last post by:
Hello all, having a small problem with a trig routine using python math module. This code is designed to convert geodetic coordinates to lambert conformal conic coordinates. It implements the...
7
by: Mark Healey | last post by:
Do the trig functions in math.h work in degrees, radians or what? For some reason it doesn't say which in "man math.h" IIRC the arctan of a slope gives the angle. So, shouldn't atanf((float)1)...
6
by: Laserson | last post by:
Hi ! Can anybody give a code that performs calculating of sine function... I need it for report
37
by: Razii | last post by:
On Wed, 30 Apr 2008 08:00:38 -0700 (PDT), Isaac Gouy <igouy2@yahoo.comwrote: This time I am going to demonstrate a very serious problem with the shootout site. The algorithms used by C,...
6
by: Marco | last post by:
One of the things I like about C++ is that it doesn't force you to create fake "objects" in the OO sense. We had a debate at the office about the "right" way to encapsulate related utility...
4
by: benbridle38 | last post by:
Hi, dont really know much about programming or anything like this. But basicly in my program i am trying to write the following formula: http://img8.imageshack.us/img8/4379/fomula.jpg...
0
by: Aaron Lawrence | last post by:
The company I work for currently uses a number of excel workbooks for asset tracking, job reports, ect. This system is very clumsy and requires a bunch of redundant data entry. I started a pet...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.