diff options
author | jofret | 2008-05-27 13:10:46 +0000 |
---|---|---|
committer | jofret | 2008-05-27 13:10:46 +0000 |
commit | ee2741ddd49f06784e5fd80d04121655e6160d30 (patch) | |
tree | 69de1c2946f9fb61a5e2f6a835bbdb07444ffb54 /src/elementaryFunctions/sqrt/csqrts.c | |
parent | c6a864563a327f505908d680a3c54ef6d38e4591 (diff) | |
download | scilab2c-ee2741ddd49f06784e5fd80d04121655e6160d30.tar.gz scilab2c-ee2741ddd49f06784e5fd80d04121655e6160d30.tar.bz2 scilab2c-ee2741ddd49f06784e5fd80d04121655e6160d30.zip |
* Add Scilab square root algorithm.
* Need some further testing
Diffstat (limited to 'src/elementaryFunctions/sqrt/csqrts.c')
-rw-r--r-- | src/elementaryFunctions/sqrt/csqrts.c | 91 |
1 files changed, 89 insertions, 2 deletions
diff --git a/src/elementaryFunctions/sqrt/csqrts.c b/src/elementaryFunctions/sqrt/csqrts.c index f39b67f5..da7000e8 100644 --- a/src/elementaryFunctions/sqrt/csqrts.c +++ b/src/elementaryFunctions/sqrt/csqrts.c @@ -10,9 +10,96 @@ * */ +#include <math.h> #include "sqrt.h" +#include "lapack.h" +#include "abs.h" +#include "sign.h" +#include "pythag.h" + +#define _sign(a, b) b >=0 ? a : -a floatComplex csqrts(floatComplex in) { - /* FIXME : Dummy ... */ - return in; + float RMax = (float) getOverflowThreshold(); + float BRMin = 2.0f * (float) getUnderflowThreshold(); + + float RealIn = creals(in); + float ImgIn = cimags(in); + + float RealOut = 0; + float ImgOut = 0; + + if(RealIn == 0) + {/* pure imaginary case */ + if(dabss(ImgIn >= BRMin)) + RealOut = ssqrts(0.5f * sabss(ImgIn)); + else + RealOut = ssqrts(sabss(ImgIn)) * ssqrts(0.5); + + ImgOut = _sign(1, ImgIn) * RealOut; + } + else if( sabss(RealIn) <= RMax && sabss(ImgIn) <= RMax) + {/* standard case : a (not zero) and b are finite */ + float Temp = ssqrts(2.0f * (sabss(RealIn) + spythags(RealIn, ImgIn))); + /* overflow test */ + if(Temp > RMax) + {/* handle (spurious) overflow by scaling a and b */ + float RealTemp = RealIn / 16.0f; + float ImgTemp = ImgIn / 16.0f; + Temp = ssqrts(2.0f * (sabss(RealIn) + spythags(RealIn, ImgTemp))); + if(RealTemp >= 0) + { + RealOut = 2 * Temp; + ImgOut = 4 * ImgTemp / Temp; + } + else + { + RealOut = 4 * sabss(ImgIn) / Temp; + ImgOut = _sign(2, ImgIn) * Temp; + } + } + else if(RealIn >= 0) /* classic switch to get the stable formulas */ + { + RealOut = 0.5f * Temp; + ImgOut = ImgIn / Temp; + } + else + { + RealOut = sabss(ImgIn) / Temp; + ImgOut = _sign(0.5f, ImgIn) * Temp; + } + } + else + { + /* + //Here we treat the special cases where a and b are +- 00 or NaN. + //The following is the treatment recommended by the C99 standard + //with the simplification of returning NaN + i NaN if the + //the real part or the imaginary part is NaN (C99 recommends + //something more complicated) + */ + + if(isnan(RealIn) == 1 || isnan(ImgIn) == 1) + {/* got NaN + i NaN */ + RealOut = RealIn + ImgIn; + ImgOut = RealOut; + } + else if( dabss(ImgIn) > RMax) + {/* case a +- i oo -> result must be +oo +- i oo for all a (finite or not) */ + RealOut = sabss(ImgIn); + ImgOut = ImgIn; + } + else if(RealIn < -RMax) + {/* here a is -Inf and b is finite */ + RealOut = 0; + ImgOut = _sign(1, ImgIn) * sabss(RealIn); + } + else + {/* here a is +Inf and b is finite */ + RealOut = RealIn; + ImgOut = 0; + } + } + + return FloatComplex(RealOut, ImgOut); } |