Providing a Marhs Library

D

David Wade

Folks,

Having spent some considerable effort in sorting out the floating point
code in the I370 port pf GCC, I would now like a floating point library to
go with it. From what I have seen most implementations use "Computer
Approximations" by "P Hart" (hope I got that right) for all the constants.
Unfortunately my local library can't get me a copy. The IEE library in
London does not seem to have a copy. Any other suggestions on a good place
to start? I have looked at the FAQ but that only seems to have extensions.
Any thoughts on a good way to proceed,,,

Dave Wade
 
F

Flash Gordon

David said:
Folks,

Having spent some considerable effort in sorting out the floating point
code in the I370 port pf GCC, I would now like a floating point library to
go with it. From what I have seen most implementations use "Computer
Approximations" by "P Hart" (hope I got that right) for all the constants.
Unfortunately my local library can't get me a copy. The IEE library in
London does not seem to have a copy. Any other suggestions on a good place
to start? I have looked at the FAQ but that only seems to have extensions.
Any thoughts on a good way to proceed,,,

I suggest you look for our very own P.J.Plauger's book:
P.J. Plauger, The Standard C Library, Prentice Hall, 1992, ISBN
0-13-131509-9

It was actually listed in the Bibliography of the FAQ at
http://c-faq.com/sx2/index.html

You might find some of the other references useful.

You might also find having a copy of the C standard to hand useful,
information on where you can get the various versions can be found here
http://clc-wiki.net/wiki/Basics_Of_The_C_Standard
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
D

David Wade

I suggest you look for our very own P.J.Plauger's book:
P.J. Plauger, The Standard C Library, Prentice Hall, 1992, ISBN
0-13-131509-9

It was actually listed in the Bibliography of the FAQ at
http://c-faq.com/sx2/index.html

You might find some of the other references useful.

You might also find having a copy of the C standard to hand useful,
information on where you can get the various versions can be found here
http://clc-wiki.net/wiki/Basics_Of_The_C_Standard
--

Both these tell you about using the library, not about implementing it....
 
O

osmium

David Wade said:
Having spent some considerable effort in sorting out the floating point
code in the I370 port pf GCC, I would now like a floating point library to
go with it. From what I have seen most implementations use "Computer
Approximations" by "P Hart" (hope I got that right) for all the constants.
Unfortunately my local library can't get me a copy. The IEE library in
London does not seem to have a copy. Any other suggestions on a good place
to start? I have looked at the FAQ but that only seems to have extensions.
Any thoughts on a good way to proceed,,,

No, it's John Hart. Lucky me (I guess), I have a copy. I think AMS55 would
be a decent place to start. It's title is _Handbook of Mathematic
Functions_ and it's hard to imagine a decent technical library that doesn't
have a copy. It's published by the US Dept of Commerce. The book I usually
see referenced is _Numerical Approximations_ (from memory) by Hastings,
which I don't have. Ideally, P.J.Plauger is your best source (after all,
he is up on the current state of the art) and he visits here. But I could
understand if he didn't want to mentor you.
 
F

Flash Gordon

David said:
Both these tell you about using the library, not about implementing it....

Wrong. To quote from Amazon
http://www.amazon.com/gp/product/01...ance&n=283155&tagActionCode=freebsdprimeiropA
about PJP's book:
| Book Description
| Prentice Hall's most important C programming title in years. A
| companion volume to Kernighan & Ritchie's C PROGRAMMING LANGUAGE. A
| collection of reusable functions (code for building data structures,
| code for performing math functions and scientific calculations, etc.)
| which will save C programmers time and money especially when working
| on large programming projects. The C Library is part of the ANSI
| (American National Standard Institute) for the C Language. This new
| book contains the complete code for the library...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It is also very useful to have the definition of what you are trying to
implement, hence my suggestion of the standard.

Please snip peoples sigs unless you are commenting on them.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
J

Jordan Abel

Folks,

Having spent some considerable effort in sorting out the floating point
code in the I370 port pf GCC, I would now like a floating point library to
go with it. From what I have seen most implementations use "Computer
Approximations" by "P Hart" (hope I got that right) for all the constants.
Unfortunately my local library can't get me a copy. The IEE library in
London does not seem to have a copy. Any other suggestions on a good place
to start? I have looked at the FAQ but that only seems to have extensions.
Any thoughts on a good way to proceed,,,

Dave Wade

There is an open-source (BSD-ish, but with less license verbage) math
library [including all functions declared in math.h] that originated
with SunPro - It does require [as far as I can tell] IEEE floating
point, and the compiler has to handle primitives such as division,
multiplication, and so on.

No idea where to find it, except for of course my own system's
/usr/src/lib/msun/src

Or there's always glibc.
 
D

David Wade

Jordan Abel said:
Folks,

Having spent some considerable effort in sorting out the floating point
code in the I370 port pf GCC, I would now like a floating point library to
go with it. From what I have seen most implementations use "Computer
Approximations" by "P Hart" (hope I got that right) for all the constants.
Unfortunately my local library can't get me a copy. The IEE library in
London does not seem to have a copy. Any other suggestions on a good place
to start? I have looked at the FAQ but that only seems to have extensions.
Any thoughts on a good way to proceed,,,

Dave Wade

There is an open-source (BSD-ish, but with less license verbage) math
library [including all functions declared in math.h] that originated
with SunPro - It does require [as far as I can tell] IEEE floating
point, and the compiler has to handle primitives such as division,
multiplication, and so on.

Thats a major problem as I370 does not have IEEE float....
 
D

David Wade

Flash Gordon said:
it....

Wrong. To quote from Amazon
http://www.amazon.com/gp/product/01...ance&n=283155&tagActionCode=freebsdprimeiropA
about PJP's book:
| Book Description
| Prentice Hall's most important C programming title in years. A
| companion volume to Kernighan & Ritchie's C PROGRAMMING LANGUAGE. A
| collection of reusable functions (code for building data structures,
| code for performing math functions and scientific calculations, etc.)
| which will save C programmers time and money especially when working
| on large programming projects. The C Library is part of the ANSI
| (American National Standard Institute) for the C Language. This new
| book contains the complete code for the library...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Arghhhh! How did I miss that. I can't even say that the description in
amazon.co.uk is different.
That looks a really usefull starting point...
It is also very useful to have the definition of what you are trying to
implement, hence my suggestion of the standard.

I agree, but the standard is pretty brief on the maths functions. Most only
warrent a one liner. However at it does have the range values that are
valid...

Thanks,
Dave
 
D

David Wade

osmium said:
No, it's John Hart. Lucky me (I guess), I have a copy. I think AMS55 would
be a decent place to start. It's title is _Handbook of Mathematic
Functions_ and it's hard to imagine a decent technical library that doesn't
have a copy. It's published by the US Dept of Commerce.

Oddly I can't see this in the IEE library...
The book I usually
see referenced is _Numerical Approximations_ (from memory) by Hastings,

Do you mean "Approximations for digital computers" That is in the IEE
library...
which I don't have. Ideally, P.J.Plauger is your best source (after all,
he is up on the current state of the art) and he visits here. But I could
understand if he didn't want to mentor you.

Any way some stuff to go on here....
 
R

Rod Pemberton

David Wade said:
Folks,

Having spent some considerable effort in sorting out the floating point
code in the I370 port pf GCC, I would now like a floating point library to
go with it. From what I have seen most implementations use "Computer
Approximations" by "P Hart" (hope I got that right) for all the constants.
Unfortunately my local library can't get me a copy. The IEE library in
London does not seem to have a copy. Any other suggestions on a good place
to start? I have looked at the FAQ but that only seems to have extensions.
Any thoughts on a good way to proceed,,,

Dave Wade

Have you looked at Sun Microsystems freely distributable math library,
fdlibm?

You could also use the math library SLATEC. You'll need to convert them
from Fortran to C using F2C.

Rod Pemberton

Netlib's main pages:
http://netlib.bell-labs.com/netlib/master/readme.html
http://netlib.bell-labs.com/netlib/master/index.html

Direct links to fdlibm,f2c,slatec:
http://netlib.bell-labs.com/netlib/fdlibm/index.html
http://netlib.bell-labs.com/netlib/f2c/index.html
http://netlib.bell-labs.com/netlib/slatec/index.html

They might also be available here:
http://www.netlib.org/slatec
http://www.netlib.org/fdlibm
http://www.netlib.org/f2c
 
D

Dave

Rod said:
Have you looked at Sun Microsystems freely distributable math library,
fdlibm?

You could also use the math library SLATEC. You'll need to convert them
from Fortran to C using F2C.

Rod Pemberton

Netlib's main pages:
http://netlib.bell-labs.com/netlib/master/readme.html
http://netlib.bell-labs.com/netlib/master/index.html

Direct links to fdlibm,f2c,slatec:
http://netlib.bell-labs.com/netlib/fdlibm/index.html
http://netlib.bell-labs.com/netlib/f2c/index.html
http://netlib.bell-labs.com/netlib/slatec/index.html

They might also be available here:
http://www.netlib.org/slatec
http://www.netlib.org/fdlibm
http://www.netlib.org/f2c

Rod,

The only problem with fdlibm is that some funcions (especially the LOG
functions) only work properley with binary float, whereas IBM float is
Hex floating point. (that is the exponent is a power of 16, not of
two). This means that code that multiplys or divides by two by
manipulating the exponent directly does not work. I guess its fixable
though, and the code does have pretty good explainations of what is
happening, and good examples of suitable constants for use in the
polynomial approximations, which are too a greater accuracy than those
in AMS55.

Dave.

P.S. SALTEC appears to assume you already have basic trig :-(
 
P

pete

Dave wrote:
The only problem with fdlibm is that some funcions (especially the LOG
functions) only work properley with binary float, whereas IBM float is
Hex floating point. (that is the exponent is a power of 16, not of
two). This means that code that multiplys or divides by two by
manipulating the exponent directly does not work. I guess its fixable
though, and the code does have pretty good explainations of what is
happening, and good examples of suitable constants for use in the
polynomial approximations, which are too a greater accuracy than those
in AMS55.

Sometimes I write math functions, when I have nothing else to do.

/* BEGIN constants.c output */

sq_rt(2) is 1.414214
l_og(2) is 0.693147
e_xp(1) is 2.718282
pi is 3.141593

sq_rt(2) * sq_rt(2) - 2.0 is -4.440892e-016
sqrt(2) * sqrt(2) - 2.0 is 4.440892e-016

pow(e_xp(1), l_og(2)) - 2.0 is -2.220446e-016
pow(exp(1), log(2)) - 2.0 is -2.220446e-016

3.1415926535897932384626433832795028841971693993751 - fs_pi()
is 0.000000e+000

/* END constants.c output */

/* BEGIN constants.c */

#include <stdio.h>
#include <float.h>
#include <errno.h>
#include <math.h>

double sq_rt(double x);
double l_og(double x);
double e_xp(double x);
double fs_pi(void);

int main(void)
{
puts("\n/* BEGIN constants.c output */\n");
printf("sq_rt(2) is %f\n", sq_rt(2));
printf(" l_og(2) is %f\n", l_og(2));
printf(" e_xp(1) is %f\n", e_xp(1));
printf(" pi is %f\n\n", fs_pi( ));

printf("sq_rt(2) * sq_rt(2) - 2.0 is %e\n",
sq_rt(2) * sq_rt(2) - 2.0);
printf(" sqrt(2) * sqrt(2) - 2.0 is %e\n\n",
sqrt(2) * sqrt(2) - 2.0);
printf("pow(e_xp(1), l_og(2)) - 2.0 is %e\n",
pow(e_xp(1), l_og(2)) - 2.0);
printf(" pow(exp(1), log(2)) - 2.0 is %e\n\n",
pow(exp(1), log(2)) - 2.0);
printf("3.1415926535897932384626433832795028841971693993751"
" - fs_pi()\nis %e\n",
3.1415926535897932384626433832795028841971693993751
- fs_pi());
puts("\n/* END constants.c output */");
return 0;
}

double sq_rt(double x)
{
int n;
double a, b;

if (DBL_MAX >= x && x > 0) {
for (n = 0; x > 2; x /= 4) {
++n;
}
while (0.5 > x) {
--n;
x *= 4;
}
a = x;
b = (1 + x) / 2;
do {
x = b;
b = (a / x + x) / 2;
} while (x > b);
while (n > 0) {
x *= 2;
--n;
}
while (0 > n) {
x /= 2;
++n;
}
} else {
if (x != 0) {
errno = EDOM;
x = HUGE_VAL;
}
}
return x;
}

double l_og(double x)
{
int n;
double a, b, c, epsilon;
static double A, B, C;

if (DBL_MAX >= x && x > 0) {
if (1 > A) {
A = sq_rt(2);
B = A / 2;
C = l_og(A);
}
for (n = 0; x > A; x /= 2) {
++n;
}
while (B > x) {
--n;
x *= 2;
}
a = (x - 1) / (x + 1);
x = C * n + a;
c = a * a;
n = 1;
epsilon = DBL_EPSILON * x;
if (0 > a) {
if (epsilon > 0) {
epsilon = -epsilon;
}
do {
n += 2;
a *= c;
b = a / n;
x += b;
} while (epsilon > b);
} else {
if (0 > epsilon) {
epsilon = -epsilon;
}
do {
n += 2;
a *= c;
b = a / n;
x += b;
} while (b > epsilon);
}
x *= 2;
} else {
errno = x != 0 ? EDOM : ERANGE;
x = -HUGE_VAL;
}
return x;
}

double e_xp(double x)
{
unsigned n, square;
double b, e;
static double x_max;

if (1 > x_max) {
x_max = l_og(DBL_MAX);
}
if (x_max >= x && x >= -x_max) {
for (square = 0; x > 1; x /= 2) {
++square;
}
while (-1 > x) {
++square;
x /= 2;
}
e = b = n = 1;
do {
b /= n++;
b *= x;
e += b;
b /= n++;
b *= x;
e += b;
} while (b > DBL_EPSILON / 4);
while (square-- != 0) {
e *= e;
}
} else {
errno = x > DBL_MAX || -DBL_MAX > x ? EDOM : ERANGE;
e = 0 > x ? 0 : HUGE_VAL;
}
return e;
}

double fs_pi(void)
{
unsigned n;
double p, a, b;

p = 0;
n = 1;
a = 3;
do {
a /= 9;
b = a / n;
n += 2;
a /= 9;
b -= a / n;
n += 2;
p += b;
} while (b > DBL_EPSILON / 4);
n = 1;
a = 2;
do {
a /= 4;
b = a / n;
n += 2;
a /= 4;
b -= a / n;
n += 2;
p += b;
} while (b > DBL_EPSILON / 2);
return 4 * p;
}

/* END constants.c */
 
D

David Wade

Rod Pemberton said:
<snip>

Pete,

Those functions are nice. Martin Baute has said he could use some help
with math functions. He's writing a Public Domain C library hosted on
Sourceforge:

They will work, but they are a bit on the slow side when running on an
emulated mainframe...
I have been using the library from

http://sourceforge.net/projects/pdos

Which again is missing any maths...
 
P

pete

David said:
They will work, but they are a bit on the slow side when running on an
emulated mainframe...

I'm not quite sure why I wrote them.
But if you remove all the statements with errno,
and change all the instances of HUGE_VAL to DBL_MAX,
then you have portable freestanding code.
 
D

Dik T. Winter

> The only problem with fdlibm is that some funcions (especially the LOG
> functions) only work properley with binary float, whereas IBM float is
> Hex floating point. (that is the exponent is a power of 16, not of
> two).

You should look at W.J. Cody and W. Waite. It goes in full detail over
the implementation of the elementary functions, and it is not binary
biased. (Although I do not like their implementation of some of the
functions.)

They also give programs (in Fortran) that test the functions.
 

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
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top