v / vlib / math
Raw file | 25 loc (24 sloc) | 520 bytes | Latest commit hash 120f31b4d
1module math
2
3// hypot returns the hypotenuse of the triangle give two sides
4pub fn hypot(x f64, y f64) f64 {
5 if is_inf(x, 0) || is_inf(y, 0) {
6 return inf(1)
7 }
8 if is_nan(x) || is_nan(y) {
9 return nan()
10 }
11 mut result := 0.0
12 if x != 0.0 || y != 0.0 {
13 abs_x := abs(x)
14 abs_y := abs(y)
15 min, max := minmax(abs_x, abs_y)
16 rat := min / max
17 root_term := sqrt(1.0 + rat * rat)
18 if max < max_f64 / root_term {
19 result = max * root_term
20 } else {
21 panic('overflow in hypot_e function')
22 }
23 }
24 return result
25}