forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabs.go
52 lines (47 loc) · 874 Bytes
/
abs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright ©2018 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package quat
import "math"
// Abs returns the absolute value (also called the modulus) of q.
func Abs(q Number) float64 {
// Special cases.
switch {
case IsInf(q):
return math.Inf(1)
case IsNaN(q):
return math.NaN()
}
r, i, j, k := q.Real, q.Imag, q.Jmag, q.Kmag
if r < 0 {
r = -r
}
if i < 0 {
i = -i
}
if j < 0 {
j = -j
}
if k < 0 {
k = -k
}
if r < i {
r, i = i, r
}
if r < j {
r, j = j, r
}
if r < k {
r, k = k, r
}
if r == 0 {
return 0
}
i /= r
j /= r
k /= r
return r * math.Sqrt(1+i*i+j*j+k*k)
}