Back to index

radiance  4R0+20100331
zeroes.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: zeroes.c,v 2.5 2003/07/17 09:21:29 schorsch Exp $";
00003 #endif
00004 /*
00005  *  zeroes.c - compute roots for various equations.
00006  *
00007  *  External symbols declared in standard.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 
00013 #include  <math.h>
00014 
00015 #include  "fvect.h"
00016 #include  "rtmath.h"
00017 
00018 int
00019 quadratic(r, a, b, c)              /* find real roots of quadratic equation */
00020 double  *r;                 /* roots in ascending order */
00021 double  a, b, c; 
00022 {
00023        double  disc;
00024        int  first;
00025 
00026        if (a < -FTINY)
00027               first = 1;
00028        else if (a > FTINY)
00029               first = 0;
00030        else if (fabs(b) > FTINY) { /* solve linearly */
00031               r[0] = -c/b;
00032               return(1);
00033        } else
00034               return(0);           /* equation is c == 0 ! */
00035               
00036        b *= 0.5;                   /* simplifies formula */
00037        
00038        disc = b*b - a*c;           /* discriminant */
00039 
00040        if (disc < -FTINY*FTINY)    /* no real roots */
00041               return(0);
00042 
00043        if (disc <= FTINY*FTINY) {  /* double root */
00044               r[0] = -b/a;
00045               return(1);
00046        }
00047        
00048        disc = sqrt(disc);
00049 
00050        r[first] = (-b - disc)/a;
00051        r[1-first] = (-b + disc)/a;
00052 
00053        return(2);
00054 }