14 #include "cln/rational.h"
24 const cl_R cl_hypot (const cl_R& a, const cl_R& b)
27 // Falls a=0: (abs b).
28 // Falls b=0: (abs a).
29 // Falls a und b beide rational sind:
30 // c:=a*a+b*b, liefere (sqrt c).
31 // Falls a oder b Floats sind:
32 // Falls einer von beiden rational ist, runde ihn zum selben Float-Typ
33 // wie der andere und führe das UP durch.
34 // Falls beide Floats sind, erweitere auf den genaueren, führe das UP
35 // durch und runde wieder auf den ungenaueren.
36 // Das Ergebnis ist ein Float >=0.
37 // UP: [a,b Floats vom selben Typ]
38 // a=0.0 -> liefere abs(b).
39 // b=0.0 -> liefere abs(a).
40 // e:=max(exponent(a),exponent(b)).
41 // a':=a/2^e bzw. 0.0 bei Underflowmöglichkeit (beim Skalieren a':=a/2^e
42 // oder beim Quadrieren a'*a': 2*(e-exponent(a))>exp_mid-exp_low-1
43 // d.h. exponent(b)-exponent(a)>floor((exp_mid-exp_low-1)/2) ).
44 // b':=b/2^e bzw. 0.0 bei Underflowmöglichkeit (beim Skalieren b':=b/2^e
45 // oder beim Quadrieren b'*b': 2*(e-exponent(b))>exp_mid-exp_low-1
46 // d.h. exponent(a)-exponent(b)>floor((exp_mid-exp_low-1)/2) ).
47 // c':=a'*a'+b'*b', c':=sqrt(c'), liefere 2^e*c'.
51 if (eq(a,0)) // a=0 -> (abs b)
56 return sqrt(square(a)+square(b));
59 // a rational, b Float
61 , return cl_hypot(cl_RA_to_SF(a),b);
62 , return cl_hypot(cl_RA_to_FF(a),b);
63 , return cl_hypot(cl_RA_to_DF(a),b);
64 , return cl_hypot(cl_RA_to_LF(a,TheLfloat(b)->len),b);
71 // a Float, b rational
72 if (eq(b,0)) // b=0 -> (abs a)
75 , return cl_hypot(a,cl_RA_to_SF(b));
76 , return cl_hypot(a,cl_RA_to_FF(b));
77 , return cl_hypot(a,cl_RA_to_DF(b));
78 , return cl_hypot(a,cl_RA_to_LF(b,TheLfloat(a)->len));
83 #ifndef CL_LF_PEDANTIC
84 GEN_F_OP2(a,b, cl_hypot, 1, 1, return);
86 GEN_F_OP2(a,b, cl_hypot, 1, 0, return);