Math.cos
func Math.cos(x: float64): float64Math.cos returns the cosine of x, in radians.
Details
Math.cosnever panics:NaNand both infinities giveNaN.
Basic examples
The cosine of a few angles, in radians:
Math.cos(0.0) // 1.0
Math.cos(Math.PI) // -1.0
Math.cos(1.0) // 0.5403023058681398Convert degrees to radians before the call:
let degrees = 180.0
Math.cos(degrees * Math.PI / 180.0) // -1.0Scope
Negative angles give the same cosine as positive ones:
Math.cos(-1.0) // 0.5403023058681398
Math.cos(-0.0) // 1.0Angles beyond a full turn wrap around:
Math.cos(2.0 * Math.PI) // 1.0
Math.cos(1e300) // -0.5753861119575491NaN and the infinities give NaN:
Math.cos(Math.NaN) // NaN
Math.cos(Math.Infinity) // NaN
Math.cos(-Math.Infinity) // NaNIn a pipeline, the angle is the argument:
Math.PI | Math.cos() // -1.0Pass it as a function value to map a list of angles:
[0.0, Math.PI].map(Math.cos) // [1.0, -1.0]Applications
Place points evenly on a circle of radius 2:
type Point = { x: float64, y: float64 }
func onCircle(radius: float64, angle: float64): Point {
Point{ x: radius * Math.cos(angle), y: radius * Math.sin(angle) }
}
for i in Iter.range(0, 4) {
let p = onCircle(2.0, float64(i) * Math.PI / 2.0)
println("${Math.round(p.x)}, ${Math.round(p.y)}")
}2.0, 0.0
0.0, 2.0
-2.0, 0.0
-0.0, -2.0The last x is a tiny negative number left by rounding in Math.PI, so Math.round gives -0.0.
Rotate a point about the origin by an angle:
type Point = { x: float64, y: float64 }
func rotate(p: Point, angle: float64): Point {
let c = Math.cos(angle)
let s = Math.sin(angle)
Point{ x: p.x * c - p.y * s, y: p.x * s + p.y * c }
}
let r = rotate(Point{ x: 1.0, y: 0.0 }, Math.PI)
r.x // -1.0
Math.abs(r.y) < 1e-12 // trueFind the third side of a triangle from two sides and the angle between them, by the law of cosines:
func thirdSide(a: float64, b: float64, angle: float64): float64 {
Math.sqrt(a * a + b * b - 2.0 * a * b * Math.cos(angle))
}
thirdSide(3.0, 4.0, Math.PI / 2.0) // 5.0Properties and relations
Cosine is even: Math.cos(-x) equals Math.cos(x):
for x in [0.5, 1.0, 2.0, 100.0] {
assert(Math.cos(-x) == Math.cos(x))
}The squares of the sine and the cosine add to 1, within rounding:
for x in [0.1, 1.0, 2.5, 10.0] {
let s = Math.sin(x)
let c = Math.cos(x)
assert(Math.abs(s * s + c * c - 1.0) < 1e-15)
}Math.acos undoes Math.cos for angles in 0.0 ..= Math.PI, within rounding:
for x in [0.0, 0.5, 1.0, 3.0] {
assert(Math.abs(Math.acos(Math.cos(x)) - x) < 1e-15)
}Possible issues
An angle in degrees is read as radians:
Math.cos(60.0) // -0.9524129804151563
Math.cos(60.0 * Math.PI / 180.0) // 0.5000000000000001Math.PI is not exactly π, so a cosine that is 0 in exact arithmetic comes out as a tiny number. Compare with a tolerance:
Math.cos(Math.PI / 2.0) // 6.123233995736757e-17
Math.cos(Math.PI / 2.0) == 0.0 // false
Math.abs(Math.cos(Math.PI / 2.0)) < 1e-12 // trueMath.acos returns an angle in 0.0 ..= Math.PI, so it does not undo Math.cos outside it:
Math.acos(Math.cos(4.0)) // 2.2831853071795862An integer is not a float64. An integer literal is always an int, so write 2.0:
Math.cos(2) // error: cannot use int where float64 is expectedFor an int variable, convert:
let n = 2
Math.cos(float64(n)) // -0.4161468365471424A float64 has no namespace of its own, so x.cos() does not resolve. Call Math.cos(x) or pipe the value:
let x = 1.0
x.cos() // error: float64 has no field or function cosSee also
Math.sin · Math.tan · Math.acos · Math.atan2 · Math.hypot · Math
