cos
Compute cosine of parameter in radians.
Base.cos
— MethodMethods
julia> methods(cos, (Any,), [Base, Base.Math, Base.MathConstants, Base.MPFR])
# 8 methods for generic function "cos" from Base: [1] cos(x::BigFloat) @ Base.MPFR mpfr.jl:860 [2] cos(a::Float16) @ Base.Math math.jl:1511 [3] cos(::Irrational{:π}) @ Base.MathConstants mathconstants.jl:147 [4] cos(a::ComplexF16) @ Base.Math math.jl:1512 [5] cos(::Missing) @ Base.Math math.jl:1533 [6] cos(x::T) where T<:Union{Float32, Float64} @ Base.Math special/trig.jl:98 [7] cos(x::Real) @ Base.Math math.jl:1528 [8] cos(z::Complex{T}) where T @ complex.jl:912
Examples
julia> using UnicodePlots
julia> lineplot(0, 2π, cos)
┌────────────────────────────────────────┐ 1 │⠉⠓⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠊⠉⠀⠀⠀⠀│ cos(x) │⠀⠀⠈⠱⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠎⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠑⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⢇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠈⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ f(x) │⠤⠤⠤⠤⠤⠤⠤⠤⠼⡤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⢤⠧⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢆⠀⠀⠀⠀⠀⢀⡰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ -1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⢄⣀⣀⡤⠎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └────────────────────────────────────────┘ ⠀0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀7⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀x⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Real Numbers
julia> cos(0)
1.0
julia> cos(0.5*pi)
6.123233995736766e-17
julia> cos(pi)
-1.0
julia> pi
π = 3.1415926535897...
julia> cos.([0 0.5*pi pi 1.5*pi 2*pi])
1×5 Matrix{Float64}:
1.0 6.12323e-17 -1.0 -1.83697e-16 1.0
big float, cos(pi/2) == 0
:
julia> cos(pi/2)
6.123233995736766e-17
julia> cos(pi/big"2")
5.48458720489676038371065313197849010525379118254343975589502858496071344256677e-78
Complex
julia> cos(0+0im)
1.0 - 0.0im
plot real part
julia> using UnicodePlots
julia> cos_real(x, y) = real(cos(x + y*im))
cos_real (generic function with 1 method)
julia> surfaceplot(-2pi:0.01:2pi, -pi:0.01:pi, cos_real)
┌────────────────────────────────────────┐ 10 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ ┌──┐ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣶⡄⠀⠀⠀⠀⠀⠀⣿⣿⣿⡀⠀⠀⠀⠀⠀⠀⣰⠆⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣷⠀⠀⠀⠀⠀⢸⣿⣿⣿⣧⠀⠀⠀⠀⠀⢀⡿⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⢰⣦⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣆⠀⠀⠀⠀⣼⣿⣿⣿⣿⣷⣄⠀⠀⢀⣾⠃⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣆⠀⠀⢀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⢸⣿⡀⠀⠀⠀⢀⣿⣿⣿⣿⣿⣿⣷⣦⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⢻⣷⡀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠻⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⣶⣤⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠈⢻⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⡟⠙⠛⢿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢻⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⡿⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠈⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠿⠃⠀⠀⠀⠀⠀⠘⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠙⠿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⢀⡠⠼⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠊⠁⠀⠀⠀⠀⠉⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └──┘ └────────────────────────────────────────┘ -10
Tips
- Call
cospi
to computecos(x*pi)
See Also
cospi
, sincos
, cosd
, cosh
, acos
Extended Inputs
Matrix
With Array
like input:
julia> methods(cos, (Any,), [LinearAlgebra])
# 6 methods for generic function "cos" from Base: [1] cos(J::UniformScaling) @ LinearAlgebra /opt/hostedtoolcache/julia/1.11.2/x64/share/julia/stdlib/v1.11/LinearAlgebra/src/uniformscaling.jl:173 [2] cos(D::Diagonal) @ LinearAlgebra /opt/hostedtoolcache/julia/1.11.2/x64/share/julia/stdlib/v1.11/LinearAlgebra/src/diagonal.jl:802 [3] cos(A::Hermitian{var"#s5027", S} where {var"#s5027"<:Complex, S<:(AbstractMatrix{<:var"#s5027"})}) @ LinearAlgebra /opt/hostedtoolcache/julia/1.11.2/x64/share/julia/stdlib/v1.11/LinearAlgebra/src/symmetric.jl:714 [4] cos(A::Union{Hermitian{var"#s5028", S}, Symmetric{var"#s5028", S}} where {var"#s5028"<:Real, S}) @ LinearAlgebra /opt/hostedtoolcache/julia/1.11.2/x64/share/julia/stdlib/v1.11/LinearAlgebra/src/symmetric.jl:710 [5] cos(A::AbstractMatrix{<:Complex}) @ LinearAlgebra /opt/hostedtoolcache/julia/1.11.2/x64/share/julia/stdlib/v1.11/LinearAlgebra/src/dense.jl:1022 [6] cos(A::AbstractMatrix{<:Real}) @ LinearAlgebra /opt/hostedtoolcache/julia/1.11.2/x64/share/julia/stdlib/v1.11/LinearAlgebra/src/dense.jl:1015
Missing docstring for cos(A::AbstractMatrix)
. Check Documenter's build log for details.
julia> cos([1 2; 3 4])
2×2 Matrix{Float64}:
0.855423 -0.110876
-0.166315 0.689109
julia> cos.([1 2; 3 4])
2×2 Matrix{Float64}:
0.540302 -0.416147
-0.989992 -0.653644
julia> cos([1 2; 3 4]) == cos.([1 2; 3 4])
false
Tech Notes
cos(::Number)
: by pure juliacos(::BigFloat)
: by MPFR
Version History
Introduced in 2018 (1.0)