[comp.lang.c] In search of cbrt

rcruz@paul.rutgers.edu (Rafael Cruz) (12/13/90)

I  plan  to  use  the Graphics Gems routines from the book by Andrew
Glassner on a PC under TC++ 1.0.  Most f the gems compile and run
without any problems.  However, the  routines  to  compute cubic  and
quartic roots make use of the library function cbrt().  This library
is in the Unix math library but TC doesn't seem to support it. 

Is the source code for cbrt() available in a site that supports
anonymous  ftp? Better yet, are the CEPHES routines by Mosher (I think
that's how you spell his name) available somewhere.    To  my
understanding,  his  library  provides  a function called cbrt() which
I assume is similar to the one available on Unix. 

Thanks in advance for any help you can provide.

----- 
Rafael 
rcruz@paul.rutgers.edu

craig@weedeater.math.yale.edu (Craig Kolb) (12/13/90)

In article <Dec.12.18.31.04.1990.3356@paul.rutgers.edu>
	rcruz@paul.rutgers.edu (Rafael Cruz) writes:

>However, the  routines  to  compute cubic  and
>quartic roots make use of the library function cbrt().  This library
>is in the Unix math library but TC doesn't seem to support it. 

Sad but true.  Many math libraries don't have a cbrt().

Eric Haines was kind enough to point out this problem
and to provide the fix that appears in the latest Graphics Gems
distribution on weedeater.math.yale.edu:~ftp/pub/GraphicsGems/src:

#ifdef NOCBRT
#define     cbrt(x)     ((x) > 0.0 ? pow((double)(x), 1.0/3.0) : \
                          ((x) < 0.0 ? -pow((double)-(x), 1.0/3.0) : 0.0))
#endif

Stick the above lines in the header of Roots3And4.c, define NOCBRT,
and you should be all set.

Craig

gwyn@smoke.brl.mil (Doug Gwyn) (12/13/90)

In article <Dec.12.18.31.04.1990.3356@paul.rutgers.edu> rcruz@paul.rutgers.edu (Rafael Cruz) writes:
>... make use of the library function cbrt().  This library
>is in the Unix math library but TC doesn't seem to support it. 

I don't know what version of UNIX you had in mind; it isn't in the
math library in most known UNIX environments.

However, if all it does is extract a cube-root, it's trivial to emulate:
	#include <math.h>
	#define cubrt(x) pow(x,1.0/3.0)

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (12/14/90)

In article <14724@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes:
> In article <Dec.12.18.31.04.1990.3356@paul.rutgers.edu> rcruz@paul.rutgers.edu (Rafael Cruz) writes:
> >... make use of the library function cbrt().

cbrt() is part of the Berkeley math library.

> However, if all it does is extract a cube-root, it's trivial to emulate:
> 	#include <math.h>
> 	#define cubrt(x) pow(x,1.0/3.0)

That doesn't quite do the job.  cbrt(-8.0) is -2.0, but pow(-8.0, 1.0/3.0)
prints "DOMAIN error" and returns 0.0.

-- 
The Marxists have merely _interpreted_ Marxism in various ways;
the point, however, is to _change_ it.		-- R. Hochhuth.

gwyn@smoke.brl.mil (Doug Gwyn) (12/15/90)

In article <4502@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>> However, if all it does is extract a cube-root, it's trivial to emulate:
>> 	#include <math.h>
>> 	#define cubrt(x) pow(x,1.0/3.0)
>That doesn't quite do the job.  cbrt(-8.0) is -2.0, but pow(-8.0, 1.0/3.0)
>prints "DOMAIN error" and returns 0.0.

Obviously.  And there are also complex cube roots, etc.  I assumed
that the operand was non-negative for simplicity, since it might be
known to be so in the particular application.