How Do I See math.h Implementations

T

Travis Parks

Hello:

I am sure this gets asked all the time, but how do I see the
implementations for math.h functions?

I am currently using GCC on Cygwin 1.7.7-1. All the headers have are a
bunch of #defines. I don't really care if these functions are written
in assembler or whatever, just so long as I can see how they work.

I have been Googling and it seems like some people believe the FPU is
implementing these functions in hardware. If that is the case, is
there a good resource for how these functions would be written in a
high-level language, like C?

Also, outside of the standard functions, is there a good open-source
library where I can find implementations for things like arbitrary
precision integers, rationals, complex numbers, statistics, calculus,
etc.

If you haven't guessed, I am trying to create a small repository of
algorithms for mathematics. It would be nice to learn how they worked
under-the-hood. I am trying to create a project where people like
myself can quickly find code. Nothing is more discouraging than
endless directory paths and hundreds of lines of macros.

Any help would be appreciated.

Thanks,
Travis Parks
 
C

Chïna Blüe Öyster Cult

Travis Parks said:
If you haven't guessed, I am trying to create a small repository of
algorithms for mathematics. It would be nice to learn how they worked
under-the-hood. I am trying to create a project where people like
myself can quickly find code. Nothing is more discouraging than
endless directory paths and hundreds of lines of macros.

You can look up ACM Collected Algorithms; Numerische Mathematik was working on a
Numeric Handbook years ago (I think that was abandonned), and you can look up
some of their code. There's also books like by Cody and Waite.

Good numerical algorithms involve some black magic.
 
L

luserXtrog

Hello:

I am sure this gets asked all the time, but how do I see the
implementations for math.h functions?

The Standard C Library, by P.J. Plauger.
$10 on amazon.

--
how do you pronounce Plauger?
Plo'-zher
Plo-zher'
Plo-zhay'
Plo'-ger
Plow'-ger
Plow'-jer
other?!
 
S

Seebs

I am sure this gets asked all the time, but how do I see the
implementations for math.h functions?

By looking in the library source.
I have been Googling and it seems like some people believe the FPU is
implementing these functions in hardware. If that is the case, is
there a good resource for how these functions would be written in a
high-level language, like C?

Tons of them.
Also, outside of the standard functions, is there a good open-source
library where I can find implementations for things like arbitrary
precision integers, rationals, complex numbers, statistics, calculus,
etc.

All of these things have been done repeatedly, and there are many good
open source libraries for big hunks of this. Have you tried searching
on likely terms? It seems hard to imagine that you wouldn't find gmp
if you searched for arbitrary-precision mathematics...
If you haven't guessed, I am trying to create a small repository of
algorithms for mathematics. It would be nice to learn how they worked
under-the-hood. I am trying to create a project where people like
myself can quickly find code. Nothing is more discouraging than
endless directory paths and hundreds of lines of macros.

I'm not sure you'll find that much added value in this -- the window
during which you don't know how to find the associated code for your
libraries is usually pretty narrow.

Note also that "open source" does not necessarily mean "free for the
taking" -- if you use code from other libraries, you may run into restrictions
on what you can do with it.

-s
 
M

Malcolm McLean

Hello:

I am sure this gets asked all the time, but how do I see the
implementations for math.h functions?
You've got two options

There's Plauger's book, "The Standard C library", which provides
implementations of the math library, or there's mine, "Basic
Algorithms" which does the same.

If you want code to cut and paste that will run efficiently, Plauger's
is the book of choice. My book is aimed at explaining the mathematics
behind the functions, so the code is designed to be teaching code
rather than production code.
 
W

Walter Banks

Travis said:
Hello:

I am sure this gets asked all the time, but how do I see the
implementations for math.h functions?

I am currently using GCC on Cygwin 1.7.7-1. All the headers have are a
bunch of #defines. I don't really care if these functions are written
in assembler or whatever, just so long as I can see how they work.

I have been Googling and it seems like some people believe the FPU is
implementing these functions in hardware. If that is the case, is
there a good resource for how these functions would be written in a
high-level language, like C?

Also, outside of the standard functions, is there a good open-source
library where I can find implementations for things like arbitrary
precision integers, rationals, complex numbers, statistics, calculus,
etc.

If you haven't guessed, I am trying to create a small repository of
algorithms for mathematics. It would be nice to learn how they worked
under-the-hood. I am trying to create a project where people like
myself can quickly find code. Nothing is more discouraging than
endless directory paths and hundreds of lines of macros.

There are two parts to your question. How the algorithms work
and the implementation. There are lots of online libraries that have
implemented math functions.

One of the best online collections of algorithms is at wolfram.

http://mathworld.wolfram.com/Algorithm.html

This page has most of the standard references. Use their search
to find more details than you ever want to know about any
function.

The classical compiler guy's must have is an old out of print book
by Hart, "Computer Approximations"

The last online reference that a lot of good information is
math.nist.gov

There is a ton of very good math information on this site.

Regards,


walter..
 
T

Tim Prince

By looking in the library source.
To amplify on that, there is a source install option for gcc in your
cygwin install menu. The newlib source may also be downloaded
independently. Obscurity was one of the design goals for the math
functions there. Most gcc users (other than on Windows) are using glibc,
which also may be downloaded.
Tons of them.

Yes, you can see how the built-in fpu math support used to be made
available in gcc by examining the mathinline.h supplied with many
gcc/glibc installations. Most of those are buggy, besides not taking
advantage of the instruction sets of the last decade. Then you can
check how current gcc implements builtin functions.
 
N

Nobody

I have been Googling and it seems like some people believe the FPU is
implementing these functions in hardware. If that is the case, is
there a good resource for how these functions would be written in a
high-level language, like C?

http://www.netlib.org/fdlibm/

There's also the GNU libc source code. However, this isn't particularly
easy to read, as it includes multiple implementations, varying from
generic C implementations which should work on any platform to versions
which take advantage of the FPU on various platforms.
Also, outside of the standard functions, is there a good open-source
library where I can find implementations for things like arbitrary
precision integers, rationals, complex numbers, statistics, calculus,
etc.

For arbitrary precision integers, floats and rationals, try GMP (aka
The GNU Multiple Precision Arithmetic Library). It doesn't
include explicit support for complex numbers.

For statistics, "R" is open-source: http://www.r-project.org/

For calculus (and symbolic computation generally), probably the most
feature-rich open-source project is maxima (which is written in Lisp).
If you haven't guessed, I am trying to create a small repository of
algorithms for mathematics. It would be nice to learn how they worked
under-the-hood. I am trying to create a project where people like
myself can quickly find code. Nothing is more discouraging than
endless directory paths and hundreds of lines of macros.

For low-level functionality, most available source code trades off
legibility against efficiency.
 
T

Travis Parks

There are two parts to your question. How the algorithms work
and the implementation. There are lots of online libraries that have
implemented math functions.

One of the best online collections of algorithms is at wolfram.

http://mathworld.wolfram.com/Algorithm.html

This page has most of the standard references. Use their search
to find more details than you ever want to know about any
function.

The classical compiler guy's must have is an old out of print book
by Hart, "Computer Approximations"

I wonder why "classics" like these go out of print. Perhaps we take C
libraries too much for granted.
 
T

Travis Parks

You can look up ACM Collected Algorithms; Numerische Mathematik was working on a
Numeric Handbook years ago (I think that was abandonned), and you can look up
some of their code. There's also books like by Cody and Waite.

Looking for this book, I found another called "Elementary Functions:
Algorithms and Implementation". I am sure if I was a mathematician,
I'd would want to read either of these books with a lot more
enthusiasm.
 
O

osmium

Travis said:
I wonder why "classics" like these go out of print. Perhaps we take C
libraries too much for granted.

I have Hart but there is an even older one I have always wanted and never
even seen, C.J. Hastings, Approximations for Digital Computers - or
something like that.

This is not just a C problem, it applies to all books. The librarians I
have talked to have a firm belief that a newer book is a better book, they
have an active policy of getting rid of old books. My local public library
just did an $18M renovation, same shelf space it had before. It is now
airier and has an atrium and a nicer coffee shop, there is no room for
future growth. Some of that money should have been used for an underground
storage - not self service - area for less often used books.
 
K

Keith Thompson

osmium said:
I have Hart but there is an even older one I have always wanted and never
even seen, C.J. Hastings, Approximations for Digital Computers - or
something like that.
[...]

amazon.com shows 5 used copies of the 1955 edition and 2 used copies of
the 1962 edition.
 
L

luserXtrog

Awesome. I might have to invest in this puppy.

I just got mine in the mail a week ago.
It's not nearly as dry as the title might suggest.
He goes through each module of the library alphabetically,
and describes what it's for, how to use it, and how to write it.
Full source code (but not free, although it seems likely the
licence fee is reasonable).
 
L

luserXtrog

I pronounce it "Bill".  :)

It's plaw (rhymes with "claw") grrr (like a tiger).

Internationalization strikes again!
I was sure it was German or French.
I didn't even consider American :)
 
G

Geoff

I just got mine in the mail a week ago.
It's not nearly as dry as the title might suggest.
He goes through each module of the library alphabetically,
and describes what it's for, how to use it, and how to write it.
Full source code (but not free, although it seems likely the
licence fee is reasonable).

More importantly, how to test it.
 
L

lawrence.jones

luserXtrog said:
I just got mine in the mail a week ago.
It's not nearly as dry as the title might suggest.

Plauger is a very good writer. In the old days of the ANSI C committee,
Bill was the secretary. It's the only time I've ever looked forward to
reading meeting minutes!
 

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,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top