Home »
Julia
Literal zero and one in Julia
Julia | Literal zero and one: In this tutorial, we are going to learn to get the literal zero and one through zero() and one() functions in Julia programming language.
Submitted by IncludeHelp, on March 28, 2020
Julia | Literal zero and one
In Julia, if we need to find the literal 0 and 1 corresponding to a specific data type we need to use zero() and one() functions. Julia provides these two library functions for this purpose.
zero() and one() functions accept a type/value and returns the literal 0 and 1 corresponding to a specific data type, type of the given value/variable.
Syntax:
zero(x)
one(x)
Here, x is the data type or value or variable.
Example 1: Finding literal 0 and 1 of given data type
# Julia example of
# zero() and one() functions
println("zero(Int64): ", zero(Int64))
println("zero(Float32): ", zero(Float32))
println("zero(Float64): ", zero(Float64))
println("zero(Bool): ", zero(Bool))
println("one(Int64): ", one(Int64))
println("one(Float32): ", one(Float32))
println("one(Float64): ", one(Float64))
println("one(Bool): ", one(Bool))
Output
zero(Int64): 0
zero(Float32): 0.0
zero(Float64): 0.0
zero(Bool): false
one(Int64): 1
one(Float32): 1.0
one(Float64): 1.0
one(Bool): true
Example 2: Finding literal 0 and 1 of given value type
# Julia example of
# zero() and one() functions
println("zero(10): ", zero(10))
println("zero(10.23): ", zero(10.23))
println("zero(true): ", zero(true))
println("zero(-10): ", zero(-10))
println("zero(-10.23): ", zero(-10.23))
println("one(10): ", one(10))
println("one(10.23): ", one(10.23))
println("one(true): ", one(true))
println("one(-10): ", one(-10))
println("one(-10.23): ", one(-10.23))
Output
zero(10): 0
zero(10.23): 0.0
zero(true): false
zero(-10): 0
zero(-10.23): 0.0
one(10): 1
one(10.23): 1.0
one(true): true
one(-10): 1
one(-10.23): 1.0
Example 3: Finding literal 0 and 1 of given variable type
# Julia example of
# zero() and one() functions
a = 10
b = 10.23
c = true
d = -10
e = -10.23
println("zero(a): ", zero(a))
println("zero(b): ", zero(b))
println("zero(c): ", zero(c))
println("zero(d): ", zero(d))
println("zero(e): ", zero(e))
println("one(a): ", one(a))
println("one(b): ", one(b))
println("one(c): ", one(c))
println("one(d): ", one(d))
println("one(e): ", one(e))
Output
zero(a): 0
zero(b): 0.0
zero(c): false
zero(d): 0
zero(e): 0.0
one(a): 1
one(b): 1.0
one(c): true
one(d): 1
one(e): 1.0
Reference: Integers and Floating-Point Numbers