Reputation: 95
say I have this code:
a = 4//2
This returns me "2//1"
b = 4//3
This returns me "4//3"
I understand that it is returning the value of a and b in simplest form. But what then? I thought that this operator returns the value of division in integer form, taking away the reminder. But it does not seem this is what it is doing.
I actually have this code:
x=Fun(identity,0..4π)
d=domain(x)
B=[ldirichlet(d),lneumann(d),rneumann(d)]
D=Derivative(d)
κ = 0.33205733621519630
u0 = (1//2) * κ * x^2
I wanted to know what (1//2) here is. From what I had thought earlier, this should have been equal to zero, but that is not what is required here. Can please someone clarify what is happening here and how does the // operator works?
Upvotes: 3
Views: 145
Reputation: 42214
In Julia when you do not know something the first thing to do is to press ?
to go to the help REPL mode represented by help?>
prompt. After pressing ?
type the command you are curious about:
help?> //
search: //
//(num, den)
Divide two integers or rational numbers, giving a Rational result.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> 3 // 5
3//5
julia> (3 // 5) // (2 // 1)
3//10
One more additional usefull way to check what is going on in Julia is to use dump
:
julia> dump(2//4)
Rational{Int64}
num: Int64 1
den: Int64 2
Finally, following the comment by @DNF it is worth noting that there is the integer division operator ÷
(and a corresponding function div
) that computes x/y, truncated to an integer.:
julia> 13 ÷ 4
3
Upvotes: 3