]> www.ginac.de Git - cln.git/blob - src/complex/algebraic/cl_SF_hypot.cc
Initial revision
[cln.git] / src / complex / algebraic / cl_SF_hypot.cc
1 // cl_hypot().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_C.h"
8
9
10 // Implementation.
11
12 #include "cl_sfloat.h"
13 #include "cl_SF.h"
14
15 #undef MAYBE_INLINE
16 #define MAYBE_INLINE inline
17 #include "cl_SF_minusp.cc"
18
19 const cl_SF cl_hypot (const cl_SF& a, const cl_SF& b)
20 {
21 //  a=0.0 -> liefere abs(b).
22 //  b=0.0 -> liefere abs(a).
23 //  e:=max(exponent(a),exponent(b)).
24 //  a':=a/2^e bzw. 0.0 bei Underflowmöglichkeit (beim Skalieren a':=a/2^e
25 //      oder beim Quadrieren a'*a':  2*(e-exponent(a))>exp_mid-exp_low-1
26 //      d.h. exponent(b)-exponent(a)>floor((exp_mid-exp_low-1)/2) ).
27 //  b':=b/2^e bzw. 0.0 bei Underflowmöglichkeit (beim Skalieren b':=b/2^e
28 //      oder beim Quadrieren b'*b':  2*(e-exponent(b))>exp_mid-exp_low-1
29 //      d.h. exponent(a)-exponent(b)>floor((exp_mid-exp_low-1)/2) ).
30 //  c':=a'*a'+b'*b', c':=sqrt(c'), liefere 2^e*c'.
31         var sintL a_exp;
32         var sintL b_exp;
33         {
34                 // Exponenten von a holen:
35                 var uintL uexp = SF_uexp(a);
36                 if (uexp == 0)
37                         // a=0.0 -> liefere (abs b) :
38                         return (minusp(b) ? -b : b);
39                 a_exp = (sintL)(uexp - SF_exp_mid);
40         }
41         {
42                 // Exponenten von b holen:
43                 var uintL uexp = SF_uexp(b);
44                 if (uexp == 0)
45                         // b=0.0 -> liefere (abs a) :
46                         return (minusp(a) ? -a : a);
47                 b_exp = (sintL)(uexp - SF_exp_mid);
48         }
49         // Nun a_exp = float_exponent(a), b_exp = float_exponent(b).
50         var sintL e = (a_exp > b_exp ? a_exp : b_exp); // Maximum der Exponenten
51         // a und b durch 2^e dividieren:
52         var cl_SF na = (b_exp-a_exp > floor(SF_exp_mid-SF_exp_low-1,2) ? SF_0 : scale_float(a,-e));
53         var cl_SF nb = (a_exp-b_exp > floor(SF_exp_mid-SF_exp_low-1,2) ? SF_0 : scale_float(b,-e));
54         // c' := a'*a'+b'*b' berechnen:
55         var cl_SF nc = square(na) + square(nb);
56         return scale_float(sqrt(nc),e); // c' := sqrt(c'), 2^e*c'
57 }