Home »
Julia
Rational Numbers in Julia
Julia | Rational Numbers: In this tutorial, we are going to learn about the Rational Numbers in Julia programming language, how to represent and use exact ratios of integers?
Submitted by IncludeHelp, on March 30, 2020
Julia | Rational Numbers
Julia programming language supports a rational number type to represent the exact ratios of the integer numbers.
To define a rational number, we use the // operator.
Syntax:
2//3
Example:
# Rational Numbers in Julia
println(1//2)
println(1//2 + 2//3)
println("typeof(1//2): ", typeof(1//2))
println()
# other representation
println("1//2: ", 1//2)
println("2//4: ", 2//4)
println("8//2: ", 8//2)
println()
println("-1//2: ", -1//2)
println("-2//4: ", -2//4)
println("-8//2: ", -8//2)
println()
println("1//-2: ", 1//-2)
println("2//-4: ", 2//-4)
println("8//-2: ", 8//-2)
println()
println("-1//-2: ", -1//-2)
println("-2//-4: ", -2//-4)
println("-8//-2: ", -8//-2)
println()
Output
1//2
7//6
typeof(1//2): Rational{Int64}
1//2: 1//2
2//4: 1//2
8//2: 4//1
-1//2: -1//2
-2//4: -1//2
-8//2: -4//1
1//-2: -1//2
2//-4: -1//2
8//-2: -4//1
-1//-2: 1//2
-2//-4: 1//2
-8//-2: 4//1