473,722 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sine code for ANSI C

Hello
I downloaded glibc and tried looking for the code that implements the
sine function
i couldnt find the file.
i went to the math directory and found math.h.. i guess that needs to be
included for the sine function. but which .c file implements the
sine/cosine and other trig fns
thanks
Nov 14 '05 #1
143 8047
suri wrote:
I downloaded glibc and tried looking for the code that implements the
sine function
i couldnt find the file.
i went to the math directory and found math.h.. i guess that needs to be
included for the sine function. but which .c file implements the
sine/cosine and other trig fns


http://www.eskimo.com/~scs/C-faq/q14.3.html

Do you really want an implementation for sin?

Apparently, you are running an Intel-compatible processor which
comes with a sin instruction implemented in hardware.

Nov 14 '05 #2
No i knew that faq.
my question is where is the sine fn imlementation?
since u say its a native instruction set means that there is no code in
ansi C for implementing sine.?
well libm.a must surely have it. but where are the C files?

Nudge wrote:
suri wrote:

I downloaded glibc and tried looking for the code that implements the
sine function
i couldnt find the file.
i went to the math directory and found math.h.. i guess that needs to be
included for the sine function. but which .c file implements the
sine/cosine and other trig fns

http://www.eskimo.com/~scs/C-faq/q14.3.html

Do you really want an implementation for sin?

Apparently, you are running an Intel-compatible processor which
comes with a sin instruction implemented in hardware.

Nov 14 '05 #3
suri wrote:
No i knew that faq.
my question is where is the sine fn imlementation?
since u say its a native instruction set means that there is no code in
ansi C for implementing sine.?
Some platforms have hardware instructions that compute sin() and the
compiler will emit them, bypassing the libm library's implementation. This
is pretty much true for ia32 and ia64.
well libm.a must surely have it. but where are the C files?


For FreeBSD, check out /usr/src/lib/libm/common/{trig.h,sincos. c} [nasty
looking code, but it works fast.] You're almost better off grabbing a
calculus book and having a look at a Taylor series expansion if you truly
want to understand the math. Or look at "Numerical Recipes in C".
Nov 14 '05 #4

suri <hs***@usc.ed u> wrote in message
news:c7******** ****@ID-233334.news.uni-berlin.de...
I downloaded glibc and tried looking for the code that implements the
sine function
i couldnt find the file.
i went to the math directory and found math.h.. i guess that needs to be
included for the sine function. but which .c file implements the
sine/cosine and other trig fns


Here is one way, but not the most advanced way. It is fairly suitable for a
float (precision wise) but not for a double.

#include <iostream.h>
#include <iomanip.h>
#include <math.h>

// Source AMS 55, eqn 4.3.97. Handbook of Mathematical Functions, Pub by
U.S. Dept of Commerce
float sinx(float x)
{
static const float a[] =
{-.1666666664,.00 83333315,-.0001984090,.00 00027526,-.0000000239};
float xsq = x*x;
float temp = x*(1 + a[0]*xsq + a[1]*xsq*xsq + a[2]* xsq*xsq*xsq
+a[3]*xsq*xsq*xsq*xs q
+ a[4]*xsq*xsq*xsq*xs q*xsq);
return temp;
}
// ------------------
void test()
{
float x;
while(1)
{
cin >> x;
if(x<0. || x > (3.1416/2) )
{
cout << "Argument to sinx must be in range 0>= x <= pi/2 \n";
continue;
}
cout << sinx(x) << setw(12) << (float)sin(x) << endl;
}

The test code, but not the sinx() code, is in C++ which I suppose will lead
to some hissy fits since this is a C group. It is also a pre-standard
version of C++ so I suppose there will be some more hissy fits from that
quarter. So be it.

Nov 14 '05 #5
I do know the series expansion of sine i was just interested to know how
its implemented in the ansi C library. like how many terms of the
infinite series are included.

I have linux and use glibc. so i could find the file in the path u mentioned

-wombat- wrote:
suri wrote:
No i knew that faq.
my question is where is the sine fn imlementation?
since u say its a native instruction set means that there is no code in
ansi C for implementing sine.?

Some platforms have hardware instructions that compute sin() and the
compiler will emit them, bypassing the libm library's implementation. This
is pretty much true for ia32 and ia64.

well libm.a must surely have it. but where are the C files?

For FreeBSD, check out /usr/src/lib/libm/common/{trig.h,sincos. c} [nasty
looking code, but it works fast.] You're almost better off grabbing a
calculus book and having a look at a Taylor series expansion if you truly
want to understand the math. Or look at "Numerical Recipes in C".

Nov 14 '05 #6
im sorry i meant i *could not* find the file

suri wrote:
I do know the series expansion of sine i was just interested to know how
its implemented in the ansi C library. like how many terms of the
infinite series are included.

I have linux and use glibc. so i could find the file in the path u
mentioned

-wombat- wrote:
suri wrote:
No i knew that faq.
my question is where is the sine fn imlementation?
since u say its a native instruction set means that there is no code in
ansi C for implementing sine.?


Some platforms have hardware instructions that compute sin() and the
compiler will emit them, bypassing the libm library's implementation.
This
is pretty much true for ia32 and ia64.

well libm.a must surely have it. but where are the C files?


For FreeBSD, check out /usr/src/lib/libm/common/{trig.h,sincos. c} [nasty
looking code, but it works fast.] You're almost better off grabbing a
calculus book and having a look at a Taylor series expansion if you truly
want to understand the math. Or look at "Numerical Recipes in C".

Nov 14 '05 #7
On Sun, 02 May 2004 06:55:43 -0700, suri <hs***@usc.ed u> wrote in
comp.lang.c:

First, please don't top-post. Material you add in a reply goes AFTER
material you are quoting. Top-posting makes technical discussions
hard to follow and is considered rude in comp.lang.c.
I do know the series expansion of sine i was just interested to know how
its implemented in the ansi C library. like how many terms of the
infinite series are included.


There is no such thing as "the" ANSI C library. The C standard
defines the functions that must be provided, their interface, and
their results when used as defined. It does not specify an
implementation at all.

The standard library functions supplied with an implementation do not
even have to be written in C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
-wombat- wrote:
Some platforms have hardware instructions that compute sin() and
the compiler will emit them, bypassing the libm library's
implementation. This is pretty much true for ia32 and ia64.


<OT>

As far as I can tell, IA-64 has no such instruction :-)

Nov 14 '05 #9
suri wrote:
my question is where is the sine fn imlementation?
since u say its a native instruction set means that there
is no code in ansi C for implementing sine?
well libm.a must surely have it. but where are the C files?


Why don't you ask in a forum dedicated to the GNU libc?

You might try:

comp.os.linux.q uestions
comp.os.linux.d evelopment.apps
http://sources.redhat.com/glibc/

(Please do not top-post.)

Nov 14 '05 #10

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

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.