@@ -146,36 +146,51 @@ sqrt(x::Float32) = box(Float32,sqrt_llvm(unbox(Float32,x)))
146146sqrt (x:: Real ) = sqrt (float (x))
147147@vectorize_1arg Number sqrt
148148
149- hypot (x:: Real , y:: Real ) = hypot (promote (float (x), float (y))... )
150- function hypot {T<:AbstractFloat} (x:: T , y:: T )
151- x = abs (x)
152- y = abs (y)
153- if x < y
154- x, y = y, x
149+ """
150+ hypot(x, y)
151+
152+ Compute the hypotenuse ``\\ sqrt{x^2+y^2}`` avoiding overflow and underflow.
153+ """
154+ hypot (x:: Number , y:: Number ) = hypot (promote (x, y)... )
155+ function hypot {T<:Number} (x:: T , y:: T )
156+ ax = abs (x)
157+ ay = abs (y)
158+ if ax < ay
159+ ax, ay = ay, ax
155160 end
156- if x == 0
157- r = y / one (x )
161+ if ax == 0
162+ r = ay / one (ax )
158163 else
159- r = y/ x
160- if isnan (r)
161- isinf (x) && return x
162- isinf (y) && return y
163- return r
164- end
164+ r = ay / ax
165+ end
166+
167+ rr = ax * sqrt (1 + r * r)
168+
169+ # Use type of rr to make sure that return type is the same for
170+ # all branches
171+ if isnan (r)
172+ isinf (ax) && return oftype (rr, Inf )
173+ isinf (ay) && return oftype (rr, Inf )
174+ return oftype (rr, r)
175+ else
176+ return rr
165177 end
166- x * sqrt (one (r)+ r* r)
167178end
179+ @vectorize_2arg Number hypot
180+
181+ """
182+ hypot(x...)
183+
184+ Compute the hypotenuse ``\\ sqrt{\\ sum x_i}`` avoiding overflow and underflow.
185+ """
186+ hypot (x:: Number... ) = vecnorm (x)
168187
169188atan2 (y:: Real , x:: Real ) = atan2 (promote (float (y),float (x))... )
170189atan2 {T<:AbstractFloat} (y:: T , x:: T ) = Base. no_op_err (" atan2" , T)
171190
172- for f in (:atan2 , :hypot )
173- @eval begin
174- ($ f)(y:: Float64 , x:: Float64 ) = ccall (($ (string (f)),libm), Float64, (Float64, Float64,), y, x)
175- ($ f)(y:: Float32 , x:: Float32 ) = ccall (($ (string (f," f" )),libm), Float32, (Float32, Float32), y, x)
176- @vectorize_2arg Number $ f
177- end
178- end
191+ atan2 (y:: Float64 , x:: Float64 ) = ccall ((:atan2 ,libm), Float64, (Float64, Float64,), y, x)
192+ atan2 (y:: Float32 , x:: Float32 ) = ccall ((:atan2f ,libm), Float32, (Float32, Float32), y, x)
193+ @vectorize_2arg Number atan2
179194
180195max {T<:AbstractFloat} (x:: T , y:: T ) = ifelse ((y > x) | (signbit (y) < signbit (x)),
181196 ifelse (isnan (y), x, y), ifelse (isnan (x), y, x))
0 commit comments