12 #include "cln/sfloat.h"
16 #define MAYBE_INLINE inline
17 #include "cl_SF_minusp.cc"
21 const cl_SF cl_hypot (const cl_SF& a, const cl_SF& b)
23 // a=0.0 -> liefere abs(b).
24 // b=0.0 -> liefere abs(a).
25 // e:=max(exponent(a),exponent(b)).
26 // a':=a/2^e bzw. 0.0 bei Underflowmöglichkeit (beim Skalieren a':=a/2^e
27 // oder beim Quadrieren a'*a': 2*(e-exponent(a))>exp_mid-exp_low-1
28 // d.h. exponent(b)-exponent(a)>floor((exp_mid-exp_low-1)/2) ).
29 // b':=b/2^e bzw. 0.0 bei Underflowmöglichkeit (beim Skalieren b':=b/2^e
30 // oder beim Quadrieren b'*b': 2*(e-exponent(b))>exp_mid-exp_low-1
31 // d.h. exponent(a)-exponent(b)>floor((exp_mid-exp_low-1)/2) ).
32 // c':=a'*a'+b'*b', c':=sqrt(c'), liefere 2^e*c'.
36 // Exponenten von a holen:
37 var uintL uexp = SF_uexp(a);
39 // a=0.0 -> liefere (abs b) :
40 return (minusp(b) ? -b : b);
41 a_exp = (sintL)(uexp - SF_exp_mid);
44 // Exponenten von b holen:
45 var uintL uexp = SF_uexp(b);
47 // b=0.0 -> liefere (abs a) :
48 return (minusp(a) ? -a : a);
49 b_exp = (sintL)(uexp - SF_exp_mid);
51 // Nun a_exp = float_exponent(a), b_exp = float_exponent(b).
52 var sintL e = (a_exp > b_exp ? a_exp : b_exp); // Maximum der Exponenten
53 // a und b durch 2^e dividieren:
54 var cl_SF na = (b_exp-a_exp > floor(SF_exp_mid-SF_exp_low-1,2) ? SF_0 : scale_float(a,-e));
55 var cl_SF nb = (a_exp-b_exp > floor(SF_exp_mid-SF_exp_low-1,2) ? SF_0 : scale_float(b,-e));
56 // c' := a'*a'+b'*b' berechnen:
57 var cl_SF nc = square(na) + square(nb);
58 return scale_float(sqrt(nc),e); // c' := sqrt(c'), 2^e*c'