zero
zero(x)
Get the additive identity element for the type of x
(x
can also specify the type itself).
Examples
julia> zero(3)
0
julia> zero(3.14)
0.0
julia> zero(5//2)
0//1
julia> zero("hello")
""
julia> zero(Bool)
false
Common examples of zero
usage:
-
Get the zero value for a numeric type:
julia> zero(Int) 0 julia> zero(Float64) 0.0
-
Get the zero value for a custom type:
julia> struct Point{T} x::T y::T end julia> zero(Point{Int}) Point{Int64}(0, 0) julia> zero(Point{Float64}) Point{Float64}(0.0, 0.0)
-
Get the zero value for a specific instance:
julia> zero(5) 0 julia> zero(3.14) 0.0 julia> zero("hello") ""
Common mistake example:
julia> zero(Bool)
1
In this example, the mistake is using 1
as the output of zero(Bool)
. The correct value for the additive identity of Bool
is false
.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.