-
Notifications
You must be signed in to change notification settings - Fork 4
/
cosPi.go
54 lines (47 loc) · 1.07 KB
/
cosPi.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
53
54
/* Copyright (C) 2017 Philipp Benner
*
* Code ported from boost (boost.org).
* boost/math/special_functions/cos_pi.hpp
*/
// Copyright (c) 2007 John Maddock
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
package special
/* -------------------------------------------------------------------------- */
//import "fmt"
import "math"
/* -------------------------------------------------------------------------- */
func CosPi(x float64) float64 {
// cos of pi*x:
invert := false
if math.Abs(x) < 0.25 {
return math.Cos(math.Pi * x)
}
if x < 0 {
x = -x
}
rem := math.Floor(x)
if int(rem) & 1 != 0 {
invert = !invert
}
rem = x - rem
if rem > 0.5 {
rem = 1 - rem
invert = !invert
}
if rem == 0.5 {
return 0
}
if rem > 0.25 {
rem = 0.5 - rem
rem = math.Sin(math.Pi * rem)
} else {
rem = math.Cos(math.Pi * rem)
}
if invert {
return -rem
} else {
return rem
}
}